[utils]: Clean up debounce helper function (#29123)

This commit is contained in:
Jay Harris
2025-05-21 12:46:16 +12:00
committed by GitHub
parent 82253a9ffa
commit 9314028a47
14 changed files with 29 additions and 44 deletions
@@ -1,10 +0,0 @@
/* Copyright (c) 2025 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */
import { debounce } from '$web-common/debounce'
export function debounceListener<T>(listener: (data: T) => void) {
return debounce(listener, 10)
}
@@ -7,7 +7,7 @@ import { loadTimeData } from '$web-common/loadTimeData'
import { SponsoredRichMediaAdEventHandler } from 'gen/brave/components/ntp_background_images/browser/mojom/ntp_background_images.mojom.m.js'
import { NewTabPageProxy } from './new_tab_page_proxy'
import { Store } from '../lib/store'
import { debounceListener } from './debounce_listener'
import { debounce } from '$web-common/debounce'
import { BackgroundState, BackgroundActions, getCurrentBackground } from '../models/backgrounds'
export function initializeBackgrounds(
@@ -64,13 +64,13 @@ export function initializeBackgrounds(
}
newTabProxy.addListeners({
onBackgroundsUpdated: debounceListener(async () => {
onBackgroundsUpdated: debounce(async () => {
await Promise.all([
updateCustomBackgrounds(),
updateSelectedBackground(),
])
updateCurrentBackground()
})
}, 10)
})
async function loadData() {
@@ -5,8 +5,8 @@
import { NewTabPageProxy } from './new_tab_page_proxy'
import { Store } from '../lib/store'
import { debounceListener } from './debounce_listener'
import { NewTabState, NewTabActions } from '../models/new_tab'
import { debounce } from '$web-common/debounce'
export function initializeNewTab(store: Store<NewTabState>): NewTabActions {
const newTabProxy = NewTabPageProxy.getInstance()
@@ -42,9 +42,9 @@ export function initializeNewTab(store: Store<NewTabState>): NewTabActions {
}
newTabProxy.addListeners({
onClockStateUpdated: debounceListener(updateClockPrefs),
onShieldsStatsUpdated: debounceListener(updateShieldsStats),
onTalkStateUpdated: debounceListener(updateTalkPrefs)
onClockStateUpdated: debounce(updateClockPrefs, 10),
onShieldsStatsUpdated: debounce(updateShieldsStats, 10),
onTalkStateUpdated: debounce(updateTalkPrefs, 10)
})
async function loadData() {
@@ -9,7 +9,7 @@ import { externalWalletFromExtensionData } from '../../../../components/brave_re
import { NewTabPageProxy } from './new_tab_page_proxy'
import { Store } from '../lib/store'
import { Optional } from '../lib/optional'
import { debounceListener } from './debounce_listener'
import { debounce } from '$web-common/debounce'
import {
RewardsState,
@@ -73,7 +73,7 @@ export function initializeRewards(store: Store<RewardsState>): RewardsActions {
}
newTabProxy.addListeners({
onRewardsStateUpdated: debounceListener(updatePrefs)
onRewardsStateUpdated: debounce(updatePrefs, 10)
})
rewardsProxy.callbackRouter.onRewardsStateUpdated.addListener(loadData)
@@ -9,7 +9,7 @@ import { loadTimeData } from '$web-common/loadTimeData'
import { SearchBoxProxy } from './search_box_proxy'
import { NewTabPageProxy } from './new_tab_page_proxy'
import { Store } from '../lib/store'
import { debounceListener } from './debounce_listener'
import { debounce } from '$web-common/debounce'
import {
SearchState,
@@ -112,7 +112,7 @@ export function initializeSearch(store: Store<SearchState>): SearchActions {
})
newTabProxy.addListeners({
onSearchStateUpdated: debounceListener(updatePrefs)
onSearchStateUpdated: debounce(updatePrefs, 10)
})
async function loadData() {
@@ -6,7 +6,7 @@
import { NewTabPageProxy } from './new_tab_page_proxy'
import { TopSitesState, TopSitesActions, TopSitesListKind } from '../models/top_sites'
import { Store } from '../lib/store'
import { debounceListener } from './debounce_listener'
import { debounce } from '$web-common/debounce'
export function initializeTopSites(
store: Store<TopSitesState>): TopSitesActions {
@@ -46,7 +46,7 @@ export function initializeTopSites(
}
newTabProxy.addListeners({
onTopSitesUpdated: debounceListener(loadData)
onTopSitesUpdated: debounce(loadData, 10)
})
document.addEventListener('visibilitychange', () => {
@@ -7,8 +7,8 @@ import { loadTimeData } from '$web-common/loadTimeData'
import * as mojom from 'gen/brave/components/brave_vpn/common/mojom/brave_vpn.mojom.m'
import { NewTabPageProxy } from './new_tab_page_proxy'
import { Store } from '../lib/store'
import { debounceListener } from './debounce_listener'
import { VPNState, VPNActions, defaultVPNActions, ConnectionState } from '../models/vpn'
import { debounce } from '$web-common/debounce'
export function initializeVPN(store: Store<VPNState>): VPNActions {
if (!loadTimeData.getBoolean('vpnFeatureEnabled')) {
@@ -63,7 +63,7 @@ export function initializeVPN(store: Store<VPNState>): VPNActions {
}
newTabProxy.addListeners({
onVPNStateUpdated: debounceListener(updatePrefs)
onVPNStateUpdated: debounce(updatePrefs, 10)
})
const vpnServiceObserver = new mojom.ServiceObserverReceiver({
@@ -9,7 +9,7 @@ import { types } from '../constants/adblock_types'
// Utils
import * as storage from '../storage'
import { debounce } from '../../common/debounce'
import { debounce } from '$web-common/debounce'
const updateCustomFilters = debounce((customFilters: string) => {
chrome.send('brave_adblock.updateCustomFilters', [customFilters])
+1 -1
View File
@@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */
import { debounce } from '../common/debounce'
import { debounce } from '$web-common/debounce'
const keyName = 'adblock-data'
@@ -3,7 +3,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
import * as React from 'react'
import { debounce } from '../../common/debounce'
import { debounce } from '$web-common/debounce'
const overflowScrollableRegex = /(auto)|(scroll)/g
const isScrollable = (element: Element) => {
@@ -29,7 +29,7 @@ export const newTabReducers = (state: NewTab.State | undefined, action: any) =>
state = rewardsReducer(state, action)
state = stackWidgetReducer(state, action)
if (state !== startingState) {
if (state && state !== startingState) {
storage.debouncedSave(state)
}
@@ -4,7 +4,7 @@
// You can obtain one at https://mozilla.org/MPL/2.0/.
// Utils
import { debounce } from '../../common/debounce'
import { debounce } from '$web-common/debounce'
const oldkeyName = 'grid-sites-data-v1'
const newkeyName = 'grid-sites-data-v2'
const defaultSuperReferralTopSitesKeyName = 'default-super-referral-top-sites'
@@ -47,7 +47,7 @@ export const load = (): NewTab.GridSitesState => {
// Saving the state is useful so that something will show when opening
// a new tab. There is a delay before MostVisitedInfoChanged() is called.
// Using `sessionStorage` won't persist to disk.
export const debouncedSave = debounce<NewTab.GridSitesState>((data: NewTab.GridSitesState) => {
export const debouncedSave = debounce((data: NewTab.GridSitesState) => {
if (data) {
window.sessionStorage.setItem(newkeyName, JSON.stringify(data))
}
@@ -187,7 +187,7 @@ export const load = (): NewTab.State => {
return cleanData(state)
}
export const debouncedSave = debounce<NewTab.State>((data: NewTab.State) => {
export const debouncedSave = debounce((data: NewTab.State) => {
if (data) {
// TODO(petemill): This should be of type NewTab.PersistantState, and first
// fix errors related to properties which shouldn't be defined as persistant
+7 -12
View File
@@ -1,17 +1,12 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */
// Copyright (c) 2018 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
'use strict'
export const debounce = function <T>(fn: (data: T) => void, bufferInterval: number, ...args: any[]) {
export const debounce = function <T extends unknown[]>(fn: (...args: T) => void, bufferInterval?: number) {
let timeout: any
return (...args2: any[]) => {
return (...args: T) => {
clearTimeout(timeout)
let a: string[] = args || []
if (args2 && args2.constructor === Array) {
a = a.concat(args2)
}
timeout = setTimeout(fn.apply.bind(fn, this, a), bufferInterval)
timeout = setTimeout(fn.apply.bind(fn, this, args), bufferInterval)
}
}