Skip to content

Commit 5937341

Browse files
committed
Fixes more perf issues loading PRs in nodes
1 parent 0e568e9 commit 5937341

File tree

3 files changed

+66
-37
lines changed

3 files changed

+66
-37
lines changed

src/views/nodes/branchNode.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import type { GitUser } from '../../git/models/user';
1313
import { gate } from '../../system/decorators/gate';
1414
import { debug, log } from '../../system/decorators/log';
1515
import { map } from '../../system/iterable';
16-
import { getSettledValue } from '../../system/promise';
16+
import type { Deferred } from '../../system/promise';
17+
import { defer, getSettledValue } from '../../system/promise';
1718
import { pad } from '../../system/string';
1819
import type { BranchesView } from '../branchesView';
1920
import type { CommitsView } from '../commitsView';
@@ -141,33 +142,45 @@ export class BranchNode
141142

142143
let pullRequest;
143144

145+
let onCompleted: Deferred<void> | undefined;
146+
144147
if (
145148
this.view.config.pullRequests.enabled &&
146149
this.view.config.pullRequests.showForBranches &&
147150
(branch.upstream != null || branch.remote)
148151
) {
149152
pullRequest = this.getState('pullRequest');
150153
if (pullRequest === undefined && this.getState('pendingPullRequest') === undefined) {
151-
void this.getAssociatedPullRequest(
152-
branch,
153-
this.root ? { include: [PullRequestState.Open, PullRequestState.Merged] } : undefined,
154-
).then(pr => {
155-
// If we found a pull request, insert it into the children cache (if loaded) and refresh the node
156-
if (pr != null && this._children != null) {
157-
this._children.splice(
158-
this._children[0] instanceof CompareBranchNode ? 1 : 0,
159-
0,
160-
new PullRequestNode(this.view, this, pr, branch),
154+
onCompleted = defer<void>();
155+
156+
queueMicrotask(() => {
157+
void this.getAssociatedPullRequest(
158+
branch,
159+
this.root ? { include: [PullRequestState.Open, PullRequestState.Merged] } : undefined,
160+
).then(pr => {
161+
onCompleted?.cancel();
162+
163+
// If we found a pull request, insert it into the children cache (if loaded) and refresh the node
164+
if (pr != null && this._children != null) {
165+
this._children.splice(
166+
this._children[0] instanceof CompareBranchNode ? 1 : 0,
167+
0,
168+
new PullRequestNode(this.view, this, pr, branch),
169+
);
170+
}
171+
172+
// Refresh this node to show a spinner while the pull request is loading
173+
this.view.triggerNodeChange(this);
174+
});
175+
176+
// If we are showing the node, then refresh this node to show a spinner while the pull request is loading
177+
if (!this.splatted) {
178+
void onCompleted?.promise.then(
179+
() => this.view.triggerNodeChange(this),
180+
() => {},
161181
);
162182
}
163-
this.view.triggerNodeChange(this);
164183
});
165-
166-
// If we are showing the node, then refresh this node to show a spinner while the pull request is loading
167-
if (!this.splatted) {
168-
queueMicrotask(() => this.view.triggerNodeChange(this));
169-
return [];
170-
}
171184
}
172185
}
173186

@@ -314,6 +327,7 @@ export class BranchNode
314327
}
315328

316329
this._children = children;
330+
setTimeout(() => onCompleted?.fulfill(), 0);
317331
}
318332

319333
return this._children;

src/views/nodes/commitNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ export class CommitNode extends ViewRefNode<ViewsWithCommits | FileHistoryView,
135135
children.splice(0, 0, new PullRequestNode(this.view as ViewsWithCommits, this, pullRequest, commit));
136136
}
137137

138-
onCompleted?.fulfill();
139138
this._children = children;
139+
setTimeout(() => onCompleted?.fulfill(), 1);
140140
}
141141

142142
return this._children;

src/views/nodes/worktreeNode.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import type { GitWorktree } from '../../git/models/worktree';
1111
import { gate } from '../../system/decorators/gate';
1212
import { debug } from '../../system/decorators/log';
1313
import { map } from '../../system/iterable';
14-
import { getSettledValue } from '../../system/promise';
14+
import type { Deferred} from '../../system/promise';
15+
import { defer, getSettledValue } from '../../system/promise';
1516
import { pad } from '../../system/string';
1617
import type { RepositoriesView } from '../repositoriesView';
1718
import type { WorktreesView } from '../worktreesView';
@@ -64,34 +65,47 @@ export class WorktreeNode extends ViewNode<WorktreesView | RepositoriesView, Sta
6465
if (this._children == null) {
6566
const branch = this._branch;
6667

67-
const pullRequest = this.getState('pullRequest');
68+
let pullRequest;
69+
70+
let onCompleted: Deferred<void> | undefined;
6871

6972
if (
7073
branch != null &&
7174
this.view.config.pullRequests.enabled &&
7275
this.view.config.pullRequests.showForBranches &&
7376
(branch.upstream != null || branch.remote)
7477
) {
78+
pullRequest = this.getState('pullRequest');
7579
if (pullRequest === undefined && this.getState('pendingPullRequest') === undefined) {
76-
void this.getAssociatedPullRequest(branch, {
77-
include: [PullRequestState.Open, PullRequestState.Merged],
78-
}).then(pr => {
79-
// If we found a pull request, insert it into the children cache (if loaded) and refresh the node
80-
if (pr != null && this._children != null) {
81-
this._children.splice(
82-
this._children[0] instanceof CompareBranchNode ? 1 : 0,
83-
0,
84-
new PullRequestNode(this.view, this, pr, branch),
80+
onCompleted = defer<void>();
81+
82+
queueMicrotask(() => {
83+
void this.getAssociatedPullRequest(branch, {
84+
include: [PullRequestState.Open, PullRequestState.Merged],
85+
}).then(pr => {
86+
onCompleted?.cancel();
87+
88+
// If we found a pull request, insert it into the children cache (if loaded) and refresh the node
89+
if (pr != null && this._children != null) {
90+
this._children.splice(
91+
this._children[0] instanceof CompareBranchNode ? 1 : 0,
92+
0,
93+
new PullRequestNode(this.view, this, pr, branch),
94+
);
95+
}
96+
97+
// Refresh this node to show a spinner while the pull request is loading
98+
this.view.triggerNodeChange(this);
99+
});
100+
101+
// If we are showing the node, then refresh this node to show a spinner while the pull request is loading
102+
if (!this.splatted) {
103+
void onCompleted?.promise.then(
104+
() => this.view.triggerNodeChange(this),
105+
() => {},
85106
);
86107
}
87-
this.view.triggerNodeChange(this);
88108
});
89-
90-
// If we are showing the node, then refresh this node to show a spinner while the pull request is loading
91-
if (!this.splatted) {
92-
queueMicrotask(() => this.view.triggerNodeChange(this));
93-
return [];
94-
}
95109
}
96110
}
97111

@@ -165,6 +179,7 @@ export class WorktreeNode extends ViewNode<WorktreesView | RepositoriesView, Sta
165179
}
166180

167181
this._children = children;
182+
setTimeout(() => onCompleted?.fulfill(), 0);
168183
}
169184

170185
return this._children;

0 commit comments

Comments
 (0)