Files
brave-core/components/common/throttle.ts
T
Pete Miller cf23301704 New Tab Page: Allow page to scroll before Today kicks in and fix backdrop-filter
Also, tighten style for indicator.
Overcomes a bug where a backdrop-filter element's ancestor which has a filter must also have a background. When this bug is fixed then this element won't need the background. Before this commit, the Widget title backgrounds would not correctly have backdrop filter because their ancestor had a blur and not also a background (it came from a higher up ancestor).
2020-11-07 20:58:50 -08:00

44 lines
1.3 KiB
TypeScript

// Copyright (c) 2020 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 http://mozilla.org/MPL/2.0/.
export default function throttle (fn: Function, bufferTimeMs: number, callAtEnd: boolean = false) {
let hasCalled = false
let pendingCallArgs: any[] | null = null
return function (...callArgs: any[]) {
if (!hasCalled) {
pendingCallArgs = null
hasCalled = true
fn(...callArgs)
// Do not allow function to be called for x seconds
setTimeout(function () {
hasCalled = false
if (pendingCallArgs) {
fn(...callArgs)
pendingCallArgs = null
}
}, bufferTimeMs)
} else {
// Save args so we can call after interval
pendingCallArgs = callArgs
}
}
}
export function requestAnimationFrameThrottle (fn: Function) {
let hasPendingCall = false
return function (...callArgs: any[]) {
if (!hasPendingCall) {
hasPendingCall = true
window.requestAnimationFrame(() => {
hasPendingCall = false
fn(...callArgs)
})
} else {
// We are waiting for animation frame,
// skip this extra call
}
}
}