From 3c4461abc02202df9338d44c5857c96aade94421 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Thu, 18 Jul 2019 22:15:06 -0400 Subject: [PATCH] Switches to use Promises.timeout for cleaner impl --- src/views/nodes/compareBranchNode.ts | 3 +- src/views/nodes/compareResultsNode.ts | 3 +- src/views/nodes/resultsCommitsNode.ts | 50 ++++++++++++++++------------- src/views/nodes/resultsFilesNode.ts | 32 ++++++++---------- src/views/nodes/searchResultsCommitsNode.ts | 6 ++-- src/views/searchView.ts | 5 ++- 6 files changed, 47 insertions(+), 52 deletions(-) diff --git a/src/views/nodes/compareBranchNode.ts b/src/views/nodes/compareBranchNode.ts index f692294..7892d9c 100644 --- a/src/views/nodes/compareBranchNode.ts +++ b/src/views/nodes/compareBranchNode.ts @@ -49,8 +49,7 @@ export class CompareBranchNode extends ViewNode { this.getCommitsQuery.bind(this), { expand: false, - includeDescription: false, - querying: true + includeDescription: false } ), new ResultsFilesNode( diff --git a/src/views/nodes/compareResultsNode.ts b/src/views/nodes/compareResultsNode.ts index 874ff5c..e71c4aa 100644 --- a/src/views/nodes/compareResultsNode.ts +++ b/src/views/nodes/compareResultsNode.ts @@ -60,8 +60,7 @@ export class CompareResultsNode extends SubscribeableViewNode { this.getCommitsQuery.bind(this), { expand: false, - includeDescription: true, - querying: true + includeDescription: true } ), new ResultsFilesNode(this.view, this, this.uri.repoPath!, this._ref1.ref, this._ref2.ref) diff --git a/src/views/nodes/resultsCommitsNode.ts b/src/views/nodes/resultsCommitsNode.ts index 0e74666..ce6ffd6 100644 --- a/src/views/nodes/resultsCommitsNode.ts +++ b/src/views/nodes/resultsCommitsNode.ts @@ -2,7 +2,7 @@ import { TreeItem, TreeItemCollapsibleState } from 'vscode'; import { Container } from '../../container'; import { GitLog, GitUri } from '../../git/gitService'; -import { debug, gate, Iterables } from '../../system'; +import { debug, gate, Iterables, Promises } from '../../system'; import { ViewWithFiles } from '../viewBase'; import { CommitNode } from './commitNode'; import { ShowMoreNode } from './common'; @@ -25,11 +25,11 @@ export class ResultsCommitsNode extends ViewNode implements Pagea public readonly repoPath: string, private _label: string, private readonly _commitsQuery: (maxCount: number | undefined) => Promise, - private readonly _options: { expand?: boolean; includeDescription?: boolean; querying?: boolean } = {} + private readonly _options: { expand?: boolean; includeDescription?: boolean } = {} ) { super(GitUri.fromRepoPath(repoPath), view, parent); - this._options = { expand: true, includeDescription: true, querying: true, ..._options }; + this._options = { expand: true, includeDescription: true, ..._options }; } get id(): string { @@ -65,33 +65,37 @@ export class ResultsCommitsNode extends ViewNode implements Pagea } async getTreeItem(): Promise { - let state; let label; let log; - if (this._options.querying) { - // Need to use Collapsed before we have results or the item won't show up in the view until the children are awaited - state = TreeItemCollapsibleState.Collapsed; - label = this._label; - - this.getCommitsQueryResults().then(({ log }) => { - this._options.querying = false; - if (log != null) { - this.maxCount = log.maxCount; - } + let state; - this.triggerChange(false); - }); - } - else { - ({ label, log } = await this.getCommitsQueryResults()); + try { + ({ label, log } = await Promises.timeout(this.getCommitsQueryResults(), 100)); if (log != null) { this.maxCount = log.maxCount; } - state = this._options.expand ? TreeItemCollapsibleState.Expanded : TreeItemCollapsibleState.Collapsed; - if (log == null || log.count === 0) { - state = TreeItemCollapsibleState.None; + state = + log == null || log.count === 0 + ? TreeItemCollapsibleState.None + : this._options.expand + ? TreeItemCollapsibleState.Expanded + : TreeItemCollapsibleState.Collapsed; + } + catch (ex) { + if (ex instanceof Promises.TimeoutError) { + ex.promise.then(({ log }: CommitsQueryResults) => { + if (log != null) { + this.maxCount = log.maxCount; + } + + this.triggerChange(false); + }); } + + // Need to use Collapsed before we have results or the item won't show up in the view until the children are awaited + // https://github.com/microsoft/vscode/issues/54806 & https://github.com/microsoft/vscode/issues/62214 + state = TreeItemCollapsibleState.Collapsed; } let description; @@ -100,7 +104,7 @@ export class ResultsCommitsNode extends ViewNode implements Pagea description = (repo && repo.formattedName) || this.repoPath; } - const item = new TreeItem(label, state); + const item = new TreeItem(label || this._label, state); item.contextValue = this.type; item.description = description; item.id = this.id; diff --git a/src/views/nodes/resultsFilesNode.ts b/src/views/nodes/resultsFilesNode.ts index 775d34b..238361a 100644 --- a/src/views/nodes/resultsFilesNode.ts +++ b/src/views/nodes/resultsFilesNode.ts @@ -4,7 +4,7 @@ import { TreeItem, TreeItemCollapsibleState } from 'vscode'; import { ViewFilesLayout } from '../../configuration'; import { Container } from '../../container'; import { GitFile, GitUri } from '../../git/gitService'; -import { Arrays, debug, gate, Iterables, Strings } from '../../system'; +import { Arrays, debug, gate, Iterables, Promises, Strings } from '../../system'; import { ViewWithFiles } from '../viewBase'; import { FileNode, FolderNode } from './folderNode'; import { ResultsFileNode } from './resultsFileNode'; @@ -61,29 +61,26 @@ export class ResultsFilesNode extends ViewNode { } async getTreeItem(): Promise { - let state; let label; let diff; - if (this._querying) { - // Need to use Collapsed before we have results or the item won't show up in the view until the children are awaited - state = TreeItemCollapsibleState.Collapsed; - label = 'files changed'; + let state; - this.getFilesQueryResults().then(_ => { - this._querying = false; - this.triggerChange(false); - }); + try { + ({ label, diff } = await Promises.timeout(this.getFilesQueryResults(), 100)); + state = + diff == null || diff.length === 0 ? TreeItemCollapsibleState.None : TreeItemCollapsibleState.Expanded; } - else { - ({ label, diff } = await this.getFilesQueryResults()); - - state = TreeItemCollapsibleState.Expanded; - if (diff == null || diff.length === 0) { - state = TreeItemCollapsibleState.None; + catch (ex) { + if (ex instanceof Promises.TimeoutError) { + ex.promise.then(() => this.triggerChange(false)); } + + // Need to use Collapsed before we have results or the item won't show up in the view until the children are awaited + // https://github.com/microsoft/vscode/issues/54806 & https://github.com/microsoft/vscode/issues/62214 + state = TreeItemCollapsibleState.Collapsed; } - const item = new TreeItem(label, state); + const item = new TreeItem(label || 'files changed', state); item.contextValue = ResourceType.ResultsFiles; item.id = this.id; @@ -99,7 +96,6 @@ export class ResultsFilesNode extends ViewNode { } private _filesQueryResults: Promise | undefined; - private _querying = true; private getFilesQueryResults() { if (this._filesQueryResults === undefined) { diff --git a/src/views/nodes/searchResultsCommitsNode.ts b/src/views/nodes/searchResultsCommitsNode.ts index 6016217..ca4e469 100644 --- a/src/views/nodes/searchResultsCommitsNode.ts +++ b/src/views/nodes/searchResultsCommitsNode.ts @@ -19,13 +19,11 @@ export class SearchResultsCommitsNode extends ResultsCommitsNode { public readonly search: string, public readonly searchBy: GitRepoSearchBy, label: string, - commitsQuery: (maxCount: number | undefined) => Promise, - _querying = true + commitsQuery: (maxCount: number | undefined) => Promise ) { super(view, parent, repoPath, label, commitsQuery, { expand: true, - includeDescription: true, - querying: _querying + includeDescription: true }); this._instanceId = instanceId++; diff --git a/src/views/searchView.ts b/src/views/searchView.ts index 37769a1..d9be68c 100644 --- a/src/views/searchView.ts +++ b/src/views/searchView.ts @@ -125,8 +125,7 @@ export class SearchView extends ViewBase { search, searchBy, `results for ${typeof options.label === 'string' ? options.label : options.label.label}`, - searchQueryFn, - true + searchQueryFn ) ); } @@ -152,7 +151,7 @@ export class SearchView extends ViewBase { }); return this.addResults( - new SearchResultsCommitsNode(this, this._root!, repoPath, search, searchBy, label, searchQueryFn, false) + new SearchResultsCommitsNode(this, this._root!, repoPath, search, searchBy, label, searchQueryFn) ); }