1// Copyright 2024 The Pigweed Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); you may not 4// use this file except in compliance with the License. You may obtain a copy of 5// the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12// License for the specific language governing permissions and limitations under 13// the License. 14 15export function throttle<T extends unknown[]>( 16 func: (...args: T) => void, 17 wait: number, 18): (...args: T) => void { 19 let timeout: ReturnType<typeof setTimeout> | null = null; 20 let lastArgs: T | null = null; 21 let lastCallTime: number | null = null; 22 23 const invokeFunction = (args: T) => { 24 lastCallTime = Date.now(); 25 func(...args); 26 timeout = null; 27 }; 28 29 return function (...args: T) { 30 const now = Date.now(); 31 const remainingWait = lastCallTime ? wait - (now - lastCallTime) : 0; 32 33 lastArgs = args; 34 35 if (remainingWait <= 0 || remainingWait > wait) { 36 if (timeout) { 37 clearTimeout(timeout); 38 timeout = null; 39 } 40 invokeFunction(args); 41 } else if (!timeout) { 42 timeout = setTimeout(() => { 43 invokeFunction(lastArgs as T); 44 }, remainingWait); 45 } 46 }; 47} 48