Skip to content

Commit

Permalink
#31191 Use deboune event instead of delayer
Browse files Browse the repository at this point in the history
  • Loading branch information
sandy081 committed Jul 24, 2017
1 parent 9387f8f commit 57d7d9b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
35 changes: 13 additions & 22 deletions src/vs/workbench/api/node/extHostTreeViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import { localize } from 'vs/nls';
import * as vscode from 'vscode';
import URI from 'vs/base/common/uri';
import { distinct } from 'vs/base/common/arrays';
import { debounceEvent } from 'vs/base/common/event';
import { TPromise } from 'vs/base/common/winjs.base';
import { Disposable } from 'vs/base/common/lifecycle';
import { ExtHostTreeViewsShape, MainThreadTreeViewsShape } from './extHost.protocol';
import { ITreeItem, TreeViewItemHandleArg } from 'vs/workbench/parts/views/common/views';
import { TreeItemCollapsibleState } from './extHostTypes';
import { ExtHostCommands, CommandsConverter } from 'vs/workbench/api/node/extHostCommands';
import { asWinJsPromise, Delayer } from 'vs/base/common/async';
import { asWinJsPromise } from 'vs/base/common/async';

type TreeItemHandle = number;

Expand Down Expand Up @@ -77,14 +79,11 @@ class ExtHostTreeView<T> extends Disposable {
private itemHandlesMap: Map<T, TreeItemHandle> = new Map<T, TreeItemHandle>();
private extChildrenElementsMap: Map<T, T[]> = new Map<T, T[]>();

private refreshDelayer: Delayer<void> = new Delayer<void>(200);
private itemsToRefresh: TreeItemHandle[] = [];

constructor(private viewId: string, private dataProvider: vscode.TreeDataProvider<T>, private proxy: MainThreadTreeViewsShape, private commands: CommandsConverter) {
super();
this.proxy.$registerView(viewId);
if (dataProvider.onDidChangeTreeData) {
this._register(dataProvider.onDidChangeTreeData(element => this._refresh(element)));
this._register(debounceEvent<T, T[]>(dataProvider.onDidChangeTreeData, (last, current) => last ? [...last, current] : [current], 200)(elements => this._refresh(elements)));
}
}

Expand Down Expand Up @@ -113,27 +112,19 @@ class ExtHostTreeView<T> extends Disposable {
return this.extElementsMap.get(treeItemHandle);
}

private _refresh(element: T): void {
if (this.itemsToRefresh && element) {
const itemHandle = this.itemHandlesMap.get(element);
if (itemHandle) {
if (this.itemsToRefresh.indexOf(itemHandle) === -1) {
this.itemsToRefresh.push(itemHandle);
this.refreshDelayer.trigger(() => this._doRefresh());
}
}
private _refresh(elements: T[]): void {
const hasRoot = elements.some(element => !element);
if (hasRoot) {
this.proxy.$refresh(this.viewId, []);
} else {
// Indicates that root has to be refreshed
this.itemsToRefresh = null;
this.refreshDelayer.trigger(() => this._doRefresh());
const itemHandles = distinct(elements.map(element => this.itemHandlesMap.get(element))
.filter(itemHandle => !!itemHandle));
if (itemHandles.length) {
this.proxy.$refresh(this.viewId, itemHandles);
}
}
}

private _doRefresh(): void {
this.proxy.$refresh(this.viewId, this.itemsToRefresh || []);
this.itemsToRefresh = [];
}

private processAndMapElements(elements: T[]): TPromise<ITreeItem[]> {
if (elements && elements.length) {
return TPromise.join(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ suite('ExtHostConfiguration', function () {
onDidChangeTreeData.fire('a');
});

test('refresh calls are throttled on unknown elements', function (done) {
target.onRefresh.event(actuals => {
assert.deepEqual([1, 2], actuals);
done();
});

onDidChangeTreeData.fire('a');
onDidChangeTreeData.fire('b');
onDidChangeTreeData.fire('g');
onDidChangeTreeData.fire('a');
});

test('refresh calls are throttled on unknown elements and root', function (done) {
target.onRefresh.event(actuals => {
assert.equal(0, actuals.length);
done();
});

onDidChangeTreeData.fire('a');
onDidChangeTreeData.fire('b');
onDidChangeTreeData.fire('g');
onDidChangeTreeData.fire('');
});

test('refresh calls are throttled on elements and root', function (done) {
target.onRefresh.event(actuals => {
assert.equal(0, actuals.length);
Expand Down

0 comments on commit 57d7d9b

Please sign in to comment.