From 5937341a86e52f16d7fc7a5643600a4558f400ff Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Tue, 30 Aug 2022 01:54:36 -0400 Subject: [PATCH] Fixes more perf issues loading PRs in nodes --- src/views/nodes/branchNode.ts | 50 +++++++++++++++++++++++++--------------- src/views/nodes/commitNode.ts | 2 +- src/views/nodes/worktreeNode.ts | 51 ++++++++++++++++++++++++++--------------- 3 files changed, 66 insertions(+), 37 deletions(-) diff --git a/src/views/nodes/branchNode.ts b/src/views/nodes/branchNode.ts index c25e40f..0b7e3b5 100644 --- a/src/views/nodes/branchNode.ts +++ b/src/views/nodes/branchNode.ts @@ -13,7 +13,8 @@ import type { GitUser } from '../../git/models/user'; import { gate } from '../../system/decorators/gate'; import { debug, log } from '../../system/decorators/log'; import { map } from '../../system/iterable'; -import { getSettledValue } from '../../system/promise'; +import type { Deferred } from '../../system/promise'; +import { defer, getSettledValue } from '../../system/promise'; import { pad } from '../../system/string'; import type { BranchesView } from '../branchesView'; import type { CommitsView } from '../commitsView'; @@ -141,6 +142,8 @@ export class BranchNode let pullRequest; + let onCompleted: Deferred | undefined; + if ( this.view.config.pullRequests.enabled && this.view.config.pullRequests.showForBranches && @@ -148,26 +151,36 @@ export class BranchNode ) { pullRequest = this.getState('pullRequest'); if (pullRequest === undefined && this.getState('pendingPullRequest') === undefined) { - void this.getAssociatedPullRequest( - branch, - this.root ? { include: [PullRequestState.Open, PullRequestState.Merged] } : undefined, - ).then(pr => { - // If we found a pull request, insert it into the children cache (if loaded) and refresh the node - if (pr != null && this._children != null) { - this._children.splice( - this._children[0] instanceof CompareBranchNode ? 1 : 0, - 0, - new PullRequestNode(this.view, this, pr, branch), + onCompleted = defer(); + + queueMicrotask(() => { + void this.getAssociatedPullRequest( + branch, + this.root ? { include: [PullRequestState.Open, PullRequestState.Merged] } : undefined, + ).then(pr => { + onCompleted?.cancel(); + + // If we found a pull request, insert it into the children cache (if loaded) and refresh the node + if (pr != null && this._children != null) { + this._children.splice( + this._children[0] instanceof CompareBranchNode ? 1 : 0, + 0, + new PullRequestNode(this.view, this, pr, branch), + ); + } + + // Refresh this node to show a spinner while the pull request is loading + this.view.triggerNodeChange(this); + }); + + // If we are showing the node, then refresh this node to show a spinner while the pull request is loading + if (!this.splatted) { + void onCompleted?.promise.then( + () => this.view.triggerNodeChange(this), + () => {}, ); } - this.view.triggerNodeChange(this); }); - - // If we are showing the node, then refresh this node to show a spinner while the pull request is loading - if (!this.splatted) { - queueMicrotask(() => this.view.triggerNodeChange(this)); - return []; - } } } @@ -314,6 +327,7 @@ export class BranchNode } this._children = children; + setTimeout(() => onCompleted?.fulfill(), 0); } return this._children; diff --git a/src/views/nodes/commitNode.ts b/src/views/nodes/commitNode.ts index b973691..a80eec2 100644 --- a/src/views/nodes/commitNode.ts +++ b/src/views/nodes/commitNode.ts @@ -135,8 +135,8 @@ export class CommitNode extends ViewRefNode onCompleted?.fulfill(), 1); } return this._children; diff --git a/src/views/nodes/worktreeNode.ts b/src/views/nodes/worktreeNode.ts index d242cad..de097ed 100644 --- a/src/views/nodes/worktreeNode.ts +++ b/src/views/nodes/worktreeNode.ts @@ -11,7 +11,8 @@ import type { GitWorktree } from '../../git/models/worktree'; import { gate } from '../../system/decorators/gate'; import { debug } from '../../system/decorators/log'; import { map } from '../../system/iterable'; -import { getSettledValue } from '../../system/promise'; +import type { Deferred} from '../../system/promise'; +import { defer, getSettledValue } from '../../system/promise'; import { pad } from '../../system/string'; import type { RepositoriesView } from '../repositoriesView'; import type { WorktreesView } from '../worktreesView'; @@ -64,7 +65,9 @@ export class WorktreeNode extends ViewNode | undefined; if ( branch != null && @@ -72,26 +75,37 @@ export class WorktreeNode extends ViewNode { - // If we found a pull request, insert it into the children cache (if loaded) and refresh the node - if (pr != null && this._children != null) { - this._children.splice( - this._children[0] instanceof CompareBranchNode ? 1 : 0, - 0, - new PullRequestNode(this.view, this, pr, branch), + onCompleted = defer(); + + queueMicrotask(() => { + void this.getAssociatedPullRequest(branch, { + include: [PullRequestState.Open, PullRequestState.Merged], + }).then(pr => { + onCompleted?.cancel(); + + // If we found a pull request, insert it into the children cache (if loaded) and refresh the node + if (pr != null && this._children != null) { + this._children.splice( + this._children[0] instanceof CompareBranchNode ? 1 : 0, + 0, + new PullRequestNode(this.view, this, pr, branch), + ); + } + + // Refresh this node to show a spinner while the pull request is loading + this.view.triggerNodeChange(this); + }); + + // If we are showing the node, then refresh this node to show a spinner while the pull request is loading + if (!this.splatted) { + void onCompleted?.promise.then( + () => this.view.triggerNodeChange(this), + () => {}, ); } - this.view.triggerNodeChange(this); }); - - // If we are showing the node, then refresh this node to show a spinner while the pull request is loading - if (!this.splatted) { - queueMicrotask(() => this.view.triggerNodeChange(this)); - return []; - } } } @@ -165,6 +179,7 @@ export class WorktreeNode extends ViewNode onCompleted?.fulfill(), 0); } return this._children;