@@ -11,7 +11,8 @@ import type { GitWorktree } from '../../git/models/worktree';
1111import { gate } from '../../system/decorators/gate' ;
1212import { debug } from '../../system/decorators/log' ;
1313import { map } from '../../system/iterable' ;
14- import { getSettledValue } from '../../system/promise' ;
14+ import type { Deferred } from '../../system/promise' ;
15+ import { defer , getSettledValue } from '../../system/promise' ;
1516import { pad } from '../../system/string' ;
1617import type { RepositoriesView } from '../repositoriesView' ;
1718import 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