Skip to content

Commit

Permalink
Batch/defer history service navigation events (#163845)
Browse files Browse the repository at this point in the history
* Batch/defer history service navigation events

Part of #161622

* Pass store through Event.accumulate

* Remove duplicate function

* Action feedback

* 💄

Co-authored-by: Benjamin Pasero <[email protected]>
Co-authored-by: Benjamin Pasero <[email protected]>
  • Loading branch information
3 people authored Nov 16, 2022
1 parent 3444a91 commit 2d51cea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
34 changes: 17 additions & 17 deletions src/vs/base/common/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,6 @@ export namespace Event {
return debounce<unknown, void>(event, () => void 0, 0, undefined, undefined, disposable);
}

/**
* Debounces an event, firing after some delay (default=0) with an array of all event original objects.
*
* *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
* event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
* returned event causes this utility to leak a listener on the original event.
*/
export function accumulate<T>(event: Event<T>, delay: number = 0, disposable?: DisposableStore): Event<T[]> {
return Event.debounce<T, T[]>(event, (last, e) => {
if (!last) {
return [e];
}
last.push(e);
return last;
}, delay, undefined, undefined, disposable);
}

/**
* Given an event, returns another event which only fires once.
*/
Expand Down Expand Up @@ -260,6 +243,23 @@ export namespace Event {
return emitter.event;
}

/**
* Debounces an event, firing after some delay (default=0) with an array of all event original objects.
*
* *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
* event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
* returned event causes this utility to leak a listener on the original event.
*/
export function accumulate<T>(event: Event<T>, delay: number = 0, disposable?: DisposableStore): Event<T[]> {
return Event.debounce<T, T[]>(event, (last, e) => {
if (!last) {
return [e];
}
last.push(e);
return last;
}, delay, undefined, undefined, disposable);
}

/**
* *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
* event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
Expand Down
13 changes: 9 additions & 4 deletions src/vs/workbench/services/history/browser/historyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,12 @@ export class HistoryService extends Disposable implements IHistoryService {
this.handleActiveEditorChange(activeEditorGroup, activeEditorPane);

// Listen to selection changes if the editor pane
// is having a selection concept.
// is having a selection concept. We use `accumulate`
// on the event to reduce the pressure on the editor
// to reduce input latency.

if (isEditorPaneWithSelection(activeEditorPane)) {
this.activeEditorListeners.add(activeEditorPane.onDidChangeSelection(e => this.handleActiveEditorSelectionChangeEvent(activeEditorGroup, activeEditorPane, e)));
this.activeEditorListeners.add(Event.accumulate(activeEditorPane.onDidChangeSelection)(e => this.handleActiveEditorSelectionChangeEvents(activeEditorGroup, activeEditorPane, e)));
}

// Context keys
Expand Down Expand Up @@ -198,8 +201,10 @@ export class HistoryService extends Disposable implements IHistoryService {
this.handleActiveEditorChangeInNavigationStacks(group, editorPane);
}

private handleActiveEditorSelectionChangeEvent(group: IEditorGroup, editorPane: IEditorPaneWithSelection, event: IEditorPaneSelectionChangeEvent): void {
this.handleActiveEditorSelectionChangeInNavigationStacks(group, editorPane, event);
private handleActiveEditorSelectionChangeEvents(group: IEditorGroup, editorPane: IEditorPaneWithSelection, events: IEditorPaneSelectionChangeEvent[]): void {
for (const event of events) {
this.handleActiveEditorSelectionChangeInNavigationStacks(group, editorPane, event);
}
}

private move(event: FileOperationEvent): void {
Expand Down

0 comments on commit 2d51cea

Please sign in to comment.