From 9314028a471dfd30847a129e6ca3c17d06b9b470 Mon Sep 17 00:00:00 2001 From: Jay Harris Date: Wed, 21 May 2025 12:46:16 +1200 Subject: [PATCH] [utils]: Clean up debounce helper function (#29123) --- .../webui/debounce_listener.ts | 10 ---------- .../webui/webui_backgrounds.ts | 6 +++--- .../webui/webui_new_tab.ts | 8 ++++---- .../webui/webui_rewards.ts | 4 ++-- .../webui/webui_search.ts | 4 ++-- .../webui/webui_top_sites.ts | 4 ++-- .../webui/webui_vpn.ts | 4 ++-- .../reducers/adblock_reducer.ts | 2 +- components/brave_adblock_ui/storage.ts | 2 +- .../brave_new_tab_ui/helpers/scrolling.ts | 2 +- components/brave_new_tab_ui/reducers/index.ts | 2 +- .../storage/grid_sites_storage.ts | 4 ++-- .../storage/new_tab_storage.ts | 2 +- components/common/debounce.ts | 19 +++++++------------ 14 files changed, 29 insertions(+), 44 deletions(-) delete mode 100644 browser/resources/brave_new_tab_page_refresh/webui/debounce_listener.ts diff --git a/browser/resources/brave_new_tab_page_refresh/webui/debounce_listener.ts b/browser/resources/brave_new_tab_page_refresh/webui/debounce_listener.ts deleted file mode 100644 index 3958c6f983b..00000000000 --- a/browser/resources/brave_new_tab_page_refresh/webui/debounce_listener.ts +++ /dev/null @@ -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(listener: (data: T) => void) { - return debounce(listener, 10) -} diff --git a/browser/resources/brave_new_tab_page_refresh/webui/webui_backgrounds.ts b/browser/resources/brave_new_tab_page_refresh/webui/webui_backgrounds.ts index 3dbf3c9d09a..236b9f8514a 100644 --- a/browser/resources/brave_new_tab_page_refresh/webui/webui_backgrounds.ts +++ b/browser/resources/brave_new_tab_page_refresh/webui/webui_backgrounds.ts @@ -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() { diff --git a/browser/resources/brave_new_tab_page_refresh/webui/webui_new_tab.ts b/browser/resources/brave_new_tab_page_refresh/webui/webui_new_tab.ts index 9244d82d73e..a603f379358 100644 --- a/browser/resources/brave_new_tab_page_refresh/webui/webui_new_tab.ts +++ b/browser/resources/brave_new_tab_page_refresh/webui/webui_new_tab.ts @@ -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): NewTabActions { const newTabProxy = NewTabPageProxy.getInstance() @@ -42,9 +42,9 @@ export function initializeNewTab(store: Store): 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() { diff --git a/browser/resources/brave_new_tab_page_refresh/webui/webui_rewards.ts b/browser/resources/brave_new_tab_page_refresh/webui/webui_rewards.ts index 5a53081c6b2..e046311522b 100644 --- a/browser/resources/brave_new_tab_page_refresh/webui/webui_rewards.ts +++ b/browser/resources/brave_new_tab_page_refresh/webui/webui_rewards.ts @@ -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): RewardsActions { } newTabProxy.addListeners({ - onRewardsStateUpdated: debounceListener(updatePrefs) + onRewardsStateUpdated: debounce(updatePrefs, 10) }) rewardsProxy.callbackRouter.onRewardsStateUpdated.addListener(loadData) diff --git a/browser/resources/brave_new_tab_page_refresh/webui/webui_search.ts b/browser/resources/brave_new_tab_page_refresh/webui/webui_search.ts index d7d98fd6ae6..84a273eb1b6 100644 --- a/browser/resources/brave_new_tab_page_refresh/webui/webui_search.ts +++ b/browser/resources/brave_new_tab_page_refresh/webui/webui_search.ts @@ -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): SearchActions { }) newTabProxy.addListeners({ - onSearchStateUpdated: debounceListener(updatePrefs) + onSearchStateUpdated: debounce(updatePrefs, 10) }) async function loadData() { diff --git a/browser/resources/brave_new_tab_page_refresh/webui/webui_top_sites.ts b/browser/resources/brave_new_tab_page_refresh/webui/webui_top_sites.ts index 3fc0835c934..59a5779d064 100644 --- a/browser/resources/brave_new_tab_page_refresh/webui/webui_top_sites.ts +++ b/browser/resources/brave_new_tab_page_refresh/webui/webui_top_sites.ts @@ -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): TopSitesActions { @@ -46,7 +46,7 @@ export function initializeTopSites( } newTabProxy.addListeners({ - onTopSitesUpdated: debounceListener(loadData) + onTopSitesUpdated: debounce(loadData, 10) }) document.addEventListener('visibilitychange', () => { diff --git a/browser/resources/brave_new_tab_page_refresh/webui/webui_vpn.ts b/browser/resources/brave_new_tab_page_refresh/webui/webui_vpn.ts index 774a1fabc10..691791aa949 100644 --- a/browser/resources/brave_new_tab_page_refresh/webui/webui_vpn.ts +++ b/browser/resources/brave_new_tab_page_refresh/webui/webui_vpn.ts @@ -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): VPNActions { if (!loadTimeData.getBoolean('vpnFeatureEnabled')) { @@ -63,7 +63,7 @@ export function initializeVPN(store: Store): VPNActions { } newTabProxy.addListeners({ - onVPNStateUpdated: debounceListener(updatePrefs) + onVPNStateUpdated: debounce(updatePrefs, 10) }) const vpnServiceObserver = new mojom.ServiceObserverReceiver({ diff --git a/components/brave_adblock_ui/reducers/adblock_reducer.ts b/components/brave_adblock_ui/reducers/adblock_reducer.ts index a117ea1219c..5d8e44c4fd3 100644 --- a/components/brave_adblock_ui/reducers/adblock_reducer.ts +++ b/components/brave_adblock_ui/reducers/adblock_reducer.ts @@ -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]) diff --git a/components/brave_adblock_ui/storage.ts b/components/brave_adblock_ui/storage.ts index fed497f6fef..f61737322dc 100644 --- a/components/brave_adblock_ui/storage.ts +++ b/components/brave_adblock_ui/storage.ts @@ -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' diff --git a/components/brave_new_tab_ui/helpers/scrolling.ts b/components/brave_new_tab_ui/helpers/scrolling.ts index 0963b45ba40..7083492de6e 100644 --- a/components/brave_new_tab_ui/helpers/scrolling.ts +++ b/components/brave_new_tab_ui/helpers/scrolling.ts @@ -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) => { diff --git a/components/brave_new_tab_ui/reducers/index.ts b/components/brave_new_tab_ui/reducers/index.ts index 2c8894a87ea..b3dc2a46093 100644 --- a/components/brave_new_tab_ui/reducers/index.ts +++ b/components/brave_new_tab_ui/reducers/index.ts @@ -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) } diff --git a/components/brave_new_tab_ui/storage/grid_sites_storage.ts b/components/brave_new_tab_ui/storage/grid_sites_storage.ts index 64d9624937c..9355aa25369 100644 --- a/components/brave_new_tab_ui/storage/grid_sites_storage.ts +++ b/components/brave_new_tab_ui/storage/grid_sites_storage.ts @@ -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((data: NewTab.GridSitesState) => { +export const debouncedSave = debounce((data: NewTab.GridSitesState) => { if (data) { window.sessionStorage.setItem(newkeyName, JSON.stringify(data)) } diff --git a/components/brave_new_tab_ui/storage/new_tab_storage.ts b/components/brave_new_tab_ui/storage/new_tab_storage.ts index 197e4375a77..20cf17cd80d 100644 --- a/components/brave_new_tab_ui/storage/new_tab_storage.ts +++ b/components/brave_new_tab_ui/storage/new_tab_storage.ts @@ -187,7 +187,7 @@ export const load = (): NewTab.State => { return cleanData(state) } -export const debouncedSave = debounce((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 diff --git a/components/common/debounce.ts b/components/common/debounce.ts index 0b466f4d023..06e53f12a7f 100644 --- a/components/common/debounce.ts +++ b/components/common/debounce.ts @@ -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 (fn: (data: T) => void, bufferInterval: number, ...args: any[]) { +export const debounce = function (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) } }