mirror of
https://github.com/Microsoft/vscode
synced 2024-08-28 13:29:44 +00:00
add Event.debouncedListener
as replacement for debounce
, https://github.com/microsoft/vscode/issues/123487
This commit is contained in:
parent
de4d6c2517
commit
f41273cd78
|
@ -119,16 +119,45 @@ export namespace Event {
|
|||
return emitter.event;
|
||||
}
|
||||
|
||||
export function debouncedListener<T, O = T>(event: Event<T>, listener: (data: O) => any, merge: (last: O | undefined, event: T) => O, delay: number = 100, leading: boolean = false): IDisposable {
|
||||
|
||||
let output: O | undefined = undefined;
|
||||
let handle: any = undefined;
|
||||
let numDebouncedCalls = 0;
|
||||
|
||||
return event(cur => {
|
||||
numDebouncedCalls++;
|
||||
output = merge(output, cur);
|
||||
|
||||
if (leading && !handle) {
|
||||
listener(output);
|
||||
output = undefined;
|
||||
}
|
||||
|
||||
clearTimeout(handle);
|
||||
handle = setTimeout(() => {
|
||||
const _output = output;
|
||||
output = undefined;
|
||||
handle = undefined;
|
||||
if (!leading || numDebouncedCalls > 1) {
|
||||
listener(_output!);
|
||||
}
|
||||
|
||||
numDebouncedCalls = 0;
|
||||
}, delay);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated DO NOT use, this leaks memory
|
||||
* @deprecated this leaks memory, {@link debouncedListener} or {@link DebounceEmitter} instead
|
||||
*/
|
||||
export function debounce<T>(event: Event<T>, merge: (last: T | undefined, event: T) => T, delay?: number, leading?: boolean, leakWarningThreshold?: number): Event<T>;
|
||||
/**
|
||||
* @deprecated DO NOT use, this leaks memory
|
||||
* @deprecated this leaks memory, {@link debouncedListener} or {@link DebounceEmitter} instead
|
||||
*/
|
||||
export function debounce<I, O>(event: Event<I>, merge: (last: O | undefined, event: I) => O, delay?: number, leading?: boolean, leakWarningThreshold?: number): Event<O>;
|
||||
/**
|
||||
* @deprecated DO NOT use, this leaks memory
|
||||
* @deprecated this leaks memory, {@link debouncedListener} or {@link DebounceEmitter} instead
|
||||
*/
|
||||
export function debounce<I, O>(event: Event<I>, merge: (last: O | undefined, event: I) => O, delay: number = 100, leading = false, leakWarningThreshold?: number): Event<O> {
|
||||
|
||||
|
|
|
@ -504,11 +504,12 @@ class NotebookEditorManager implements IWorkbenchContribution {
|
|||
|
||||
// OPEN notebook editor for models that have turned dirty without being visible in an editor
|
||||
type E = IResolvedNotebookEditorModel;
|
||||
this._disposables.add(Event.debounce<E, E[]>(
|
||||
this._disposables.add(Event.debouncedListener<E, E[]>(
|
||||
this._notebookEditorModelService.onDidChangeDirty,
|
||||
this._openMissingDirtyNotebookEditors.bind(this),
|
||||
(last, current) => !last ? [current] : [...last, current],
|
||||
100
|
||||
)(this._openMissingDirtyNotebookEditors, this));
|
||||
));
|
||||
|
||||
// CLOSE notebook editor for models that have no more serializer
|
||||
this._disposables.add(notebookService.onWillRemoveViewType(e => {
|
||||
|
|
Loading…
Reference in a new issue