diff --git a/src/vs/base/common/decorators.ts b/src/vs/base/common/decorators.ts index 33ea5f02dabc8d0688c5ab0d5b8acc104d3353d3..080ac3dc295eba3547f9afc0ca9465fd06fe2289 100644 --- a/src/vs/base/common/decorators.ts +++ b/src/vs/base/common/decorators.ts @@ -62,12 +62,23 @@ export function memoize(target: any, key: string, descriptor: any) { }; } -export function debounce(delay: number): Function { +export interface IDebouceReducer { + (previousValue: T, ...args: any[]): T; +} + +export function debounce(delay: number, reducer?: IDebouceReducer, initialValue?: T): Function { return createDecorator((fn, key) => { const timerKey = `$debounce$${key}`; + let result = initialValue; return function (this: any, ...args: any[]) { clearTimeout(this[timerKey]); + + if (reducer) { + result = reducer(result, ...args); + args = [result]; + } + this[timerKey] = setTimeout(() => fn.apply(this, args), delay); }; });