Skip to content

Commit

Permalink
Switches to use Promises.timeout for cleaner impl
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Jul 19, 2019
1 parent c076be0 commit 3c4461a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 52 deletions.
3 changes: 1 addition & 2 deletions src/views/nodes/compareBranchNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ export class CompareBranchNode extends ViewNode<RepositoriesView> {
this.getCommitsQuery.bind(this),
{
expand: false,
includeDescription: false,
querying: true
includeDescription: false
}
),
new ResultsFilesNode(
Expand Down
3 changes: 1 addition & 2 deletions src/views/nodes/compareResultsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ export class CompareResultsNode extends SubscribeableViewNode<CompareView> {
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)
Expand Down
50 changes: 27 additions & 23 deletions src/views/nodes/resultsCommitsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -25,11 +25,11 @@ export class ResultsCommitsNode extends ViewNode<ViewWithFiles> implements Pagea
public readonly repoPath: string,
private _label: string,
private readonly _commitsQuery: (maxCount: number | undefined) => Promise<CommitsQueryResults>,
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 {
Expand Down Expand Up @@ -65,33 +65,37 @@ export class ResultsCommitsNode extends ViewNode<ViewWithFiles> implements Pagea
}

async getTreeItem(): Promise<TreeItem> {
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;
Expand All @@ -100,7 +104,7 @@ export class ResultsCommitsNode extends ViewNode<ViewWithFiles> 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;
Expand Down
32 changes: 14 additions & 18 deletions src/views/nodes/resultsFilesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -61,29 +61,26 @@ export class ResultsFilesNode extends ViewNode<ViewWithFiles> {
}

async getTreeItem(): Promise<TreeItem> {
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;

Expand All @@ -99,7 +96,6 @@ export class ResultsFilesNode extends ViewNode<ViewWithFiles> {
}

private _filesQueryResults: Promise<FilesQueryResults> | undefined;
private _querying = true;

private getFilesQueryResults() {
if (this._filesQueryResults === undefined) {
Expand Down
6 changes: 2 additions & 4 deletions src/views/nodes/searchResultsCommitsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ export class SearchResultsCommitsNode extends ResultsCommitsNode {
public readonly search: string,
public readonly searchBy: GitRepoSearchBy,
label: string,
commitsQuery: (maxCount: number | undefined) => Promise<CommitsQueryResults>,
_querying = true
commitsQuery: (maxCount: number | undefined) => Promise<CommitsQueryResults>
) {
super(view, parent, repoPath, label, commitsQuery, {
expand: true,
includeDescription: true,
querying: _querying
includeDescription: true
});

this._instanceId = instanceId++;
Expand Down
5 changes: 2 additions & 3 deletions src/views/searchView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ export class SearchView extends ViewBase<SearchNode> {
search,
searchBy,
`results for ${typeof options.label === 'string' ? options.label : options.label.label}`,
searchQueryFn,
true
searchQueryFn
)
);
}
Expand All @@ -152,7 +151,7 @@ export class SearchView extends ViewBase<SearchNode> {
});

return this.addResults(
new SearchResultsCommitsNode(this, this._root!, repoPath, search, searchBy, label, searchQueryFn, false)
new SearchResultsCommitsNode(this, this._root!, repoPath, search, searchBy, label, searchQueryFn)
);
}

Expand Down

0 comments on commit 3c4461a

Please sign in to comment.