Skip to content

Commit c99fef2

Browse files
committed
Add Event.accumulate helper
1 parent 07aacf8 commit c99fef2

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/vs/base/common/event.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ export namespace Event {
7070
return debounce<unknown, void>(event, () => void 0, 0, undefined, undefined, disposable);
7171
}
7272

73+
/**
74+
* Debounces an event, firing after some delay (default=0) with an array of all event original objects.
75+
*
76+
* *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
77+
* event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
78+
* returned event causes this utility to leak a listener on the original event.
79+
*/
80+
export function accumulate<T>(event: Event<T>, delay: number = 0, disposable?: DisposableStore): Event<T[]> {
81+
return Event.debounce<T, T[]>(event, (last, e) => {
82+
if (!last) {
83+
return [e];
84+
}
85+
last.push(e);
86+
return last;
87+
}, delay, undefined, undefined, disposable);
88+
}
89+
7390
/**
7491
* Given an event, returns another event which only fires once.
7592
*/

src/vs/workbench/browser/parts/editor/editorStatus.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -683,13 +683,7 @@ export class EditorStatus extends Disposable implements IWorkbenchContribution {
683683
}));
684684

685685
// Hook Listener for content changes
686-
this.activeEditorListeners.add(Event.debounce<IModelContentChangedEvent, IModelContentChangedEvent[]>(activeCodeEditor.onDidChangeModelContent, (last, e) => {
687-
if (!last) {
688-
return [e];
689-
}
690-
last.push(e);
691-
return last;
692-
}, 0)(e => {
686+
this.activeEditorListeners.add(Event.accumulate(activeCodeEditor.onDidChangeModelContent, this._store)(e => {
693687
this.onEOLChange(activeCodeEditor);
694688
this.currentProblemStatus.update(activeCodeEditor);
695689

0 commit comments

Comments
 (0)