@@ -25,6 +25,8 @@ import type { Subscription } from '../../plus/gk/account/subscription';
25
25
import type { SubscriptionChangeEvent } from '../../plus/gk/account/subscriptionService' ;
26
26
import { getLaunchpadSummary } from '../../plus/launchpad/utils' ;
27
27
import { showRepositoryPicker } from '../../quickpicks/repositoryPicker' ;
28
+ import type { Deferrable } from '../../system/function' ;
29
+ import { debounce } from '../../system/function' ;
28
30
import { map } from '../../system/iterable' ;
29
31
import { getSettledValue } from '../../system/promise' ;
30
32
import { executeActionCommand , executeCommand , registerCommand } from '../../system/vscode/command' ;
@@ -73,7 +75,7 @@ const emptyDisposable = Object.freeze({
73
75
} ,
74
76
} ) ;
75
77
76
- type RepositorySubscription = { repo : Repository ; subscription : Disposable } ;
78
+ type RepositorySubscription = { repo : Repository ; subscription ? : Disposable } ;
77
79
type RepositoryBranchData = {
78
80
repo : Repository ;
79
81
branches : GitBranch [ ] ;
@@ -94,7 +96,7 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
94
96
this . _disposable = Disposable . from (
95
97
this . container . git . onDidChangeRepositories ( this . onRepositoriesChanged , this ) ,
96
98
! workspace . isTrusted
97
- ? workspace . onDidGrantWorkspaceTrust ( this . notifyDidChangeRepositories , this )
99
+ ? workspace . onDidGrantWorkspaceTrust ( ( ) => this . notifyDidChangeRepositories ( ) , this )
98
100
: emptyDisposable ,
99
101
this . container . subscription . onDidChange ( this . onSubscriptionChanged , this ) ,
100
102
onDidChangeContext ( this . onContextChanged , this ) ,
@@ -308,8 +310,13 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
308
310
return this . getState ( ) ;
309
311
}
310
312
311
- onReloaded ( ) {
313
+ onRefresh ( ) {
314
+ this . resetBranchOverview ( ) ;
312
315
this . notifyDidChangeRepositories ( ) ;
316
+ }
317
+
318
+ onReloaded ( ) {
319
+ this . onRefresh ( ) ;
313
320
this . notifyDidChangeProgress ( ) ;
314
321
}
315
322
@@ -321,6 +328,17 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
321
328
}
322
329
}
323
330
331
+ onVisibilityChanged ( visible : boolean ) {
332
+ if ( ! visible ) {
333
+ this . stopRepositorySubscription ( ) ;
334
+
335
+ return ;
336
+ }
337
+
338
+ this . resumeRepositorySubscription ( ) ;
339
+ this . notifyDidChangeRepositories ( true ) ;
340
+ }
341
+
324
342
private onTogglePreviewEnabled ( isEnabled ?: boolean ) {
325
343
if ( isEnabled === undefined ) {
326
344
isEnabled = ! this . getPreviewEnabled ( ) ;
@@ -444,12 +462,16 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
444
462
const repo = this . getSelectedRepository ( ) ;
445
463
if ( repo == null ) return undefined ;
446
464
447
- const branchesAndWorktrees = await this . getBranchesData ( repo ) ;
465
+ const forceRepo = this . _invalidateOverview === 'repo' ;
466
+ const forceWip = this . _invalidateOverview !== undefined ;
467
+ const branchesAndWorktrees = await this . getBranchesData ( repo , forceRepo ) ;
448
468
const overviewBranches = await getOverviewBranches (
449
469
branchesAndWorktrees ,
450
470
this . container ,
451
471
this . _overviewBranchFilter ,
472
+ forceWip ? { forceActive : true } : undefined ,
452
473
) ;
474
+ this . _invalidateOverview = undefined ;
453
475
if ( overviewBranches == null ) return undefined ;
454
476
455
477
const result : GetOverviewResponse = {
@@ -476,7 +498,7 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
476
498
}
477
499
478
500
if ( this . _repositorySubscription != null ) {
479
- this . _repositorySubscription . subscription . dispose ( ) ;
501
+ this . _repositorySubscription . subscription ? .dispose ( ) ;
480
502
this . _repositorySubscription = undefined ;
481
503
}
482
504
if ( repo != null ) {
@@ -489,10 +511,41 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
489
511
return repo ;
490
512
}
491
513
514
+ private stopRepositorySubscription ( ) {
515
+ if ( this . _repositorySubscription != null ) {
516
+ this . _repositorySubscription . subscription ?. dispose ( ) ;
517
+ this . _repositorySubscription . subscription = undefined ;
518
+ }
519
+ }
520
+
521
+ private resumeRepositorySubscription ( force = false ) {
522
+ if ( this . _repositorySubscription == null ) {
523
+ return ;
524
+ }
525
+
526
+ if ( force || this . _repositorySubscription . subscription == null ) {
527
+ this . _repositorySubscription . subscription ?. dispose ( ) ;
528
+ this . _repositorySubscription . subscription = undefined ;
529
+ this . _repositorySubscription . subscription = this . subscribeToRepository ( this . _repositorySubscription . repo ) ;
530
+ }
531
+ }
532
+
533
+ private resetBranchOverview ( ) {
534
+ this . _repositoryBranches . clear ( ) ;
535
+
536
+ if ( ! this . host . visible ) {
537
+ this . stopRepositorySubscription ( ) ;
538
+ return ;
539
+ }
540
+
541
+ this . resumeRepositorySubscription ( true ) ;
542
+ }
543
+
492
544
private subscribeToRepository ( repo : Repository ) : Disposable {
493
545
return Disposable . from (
546
+ // TODO: advanced confiugration for the watchFileSystem timing
494
547
repo . watchFileSystem ( 1000 ) ,
495
- repo . onDidChangeFileSystem ( ( ) => this . onWipChanged ( repo ) ) ,
548
+ repo . onDidChangeFileSystem ( ( ) => this . onOverviewRepoChanged ( 'wip' ) ) ,
496
549
repo . onDidChange ( e => {
497
550
if (
498
551
e . changed (
@@ -505,15 +558,23 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
505
558
RepositoryChangeComparisonMode . Any ,
506
559
)
507
560
) {
508
- this . onWipChanged ( repo ) ;
561
+ this . onOverviewRepoChanged ( ' repo' ) ;
509
562
}
510
563
} ) ,
511
564
) ;
512
565
}
513
566
514
- private onWipChanged ( _repo : Repository ) {
515
- this . _invalidateRepositoryBranches = true ;
516
- void this . host . notify ( DidChangeRepositoryWip , undefined ) ;
567
+ private onOverviewRepoChanged ( scope : 'repo' | 'wip' ) {
568
+ if ( this . _invalidateOverview !== 'repo' ) {
569
+ this . _invalidateOverview = scope ;
570
+ }
571
+ if ( ! this . host . visible ) return ;
572
+
573
+ if ( scope === 'wip' ) {
574
+ void this . host . notify ( DidChangeRepositoryWip , undefined ) ;
575
+ } else {
576
+ this . notifyDidChangeRepositories ( ) ;
577
+ }
517
578
}
518
579
519
580
private getSelectedRepository ( ) {
@@ -524,9 +585,9 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
524
585
return this . _repositorySubscription ?. repo ;
525
586
}
526
587
527
- private _invalidateRepositoryBranches = true ;
588
+ private _invalidateOverview : 'repo' | 'wip' | undefined ;
528
589
private readonly _repositoryBranches : Map < string , RepositoryBranchData > = new Map ( ) ;
529
- private async getBranchesData ( repo : Repository , force = this . _invalidateRepositoryBranches ) {
590
+ private async getBranchesData ( repo : Repository , force = false ) {
530
591
if ( force || ! this . _repositoryBranches . has ( repo . path ) ) {
531
592
const worktrees = ( await repo . git . getWorktrees ( ) ) ?? [ ] ;
532
593
const worktreesByBranch = groupWorktreesByBranch ( worktrees ) ;
@@ -539,7 +600,6 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
539
600
540
601
const branches = getSettledValue ( branchesResult ) ?. values ?? [ ] ;
541
602
542
- this . _invalidateRepositoryBranches = false ;
543
603
this . _repositoryBranches . set ( repo . path , {
544
604
repo : repo ,
545
605
branches : branches ,
@@ -587,9 +647,22 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
587
647
} ) ;
588
648
}
589
649
590
- private notifyDidChangeRepositories ( ) {
650
+ private notifyDidChangeRepositoriesCore ( ) {
591
651
void this . host . notify ( DidChangeRepositories , this . getRepositoriesState ( ) ) ;
592
652
}
653
+ private _notifyDidChangeRepositoriesDebounced : Deferrable < ( ) => void > | undefined = undefined ;
654
+ private notifyDidChangeRepositories ( immediate = false ) {
655
+ if ( immediate ) {
656
+ this . notifyDidChangeRepositoriesCore ( ) ;
657
+ return ;
658
+ }
659
+
660
+ if ( this . _notifyDidChangeRepositoriesDebounced == null ) {
661
+ this . _notifyDidChangeRepositoriesDebounced = debounce ( this . notifyDidChangeRepositoriesCore . bind ( this ) , 500 ) ;
662
+ }
663
+
664
+ this . _notifyDidChangeRepositoriesDebounced ( ) ;
665
+ }
593
666
594
667
private notifyDidChangeProgress ( ) {
595
668
void this . host . notify ( DidChangeWalkthroughProgress , {
@@ -745,7 +818,8 @@ const thresholdValues: Record<OverviewStaleThreshold | OverviewRecentThreshold,
745
818
async function getOverviewBranches (
746
819
branchesData : RepositoryBranchData ,
747
820
container : Container ,
748
- options : OverviewFilters ,
821
+ filters : OverviewFilters ,
822
+ options ?: { forceActive ?: boolean } ,
749
823
) : Promise < GetOverviewBranches | undefined > {
750
824
const { branches, worktreesByBranch } = branchesData ;
751
825
if ( branches . length === 0 ) return undefined ;
@@ -756,22 +830,29 @@ async function getOverviewBranches(
756
830
stale : [ ] ,
757
831
} ;
758
832
833
+ let repoStatusPromise : Promise < GitStatus | undefined > | undefined ;
759
834
const prPromises = new Map < string , Promise < PullRequest | undefined > > ( ) ;
760
835
const statusPromises = new Map < string , Promise < GitStatus | undefined > > ( ) ;
761
836
const contributorPromises = new Map < string , Promise < BranchContributorOverview | undefined > > ( ) ;
762
837
763
838
const now = Date . now ( ) ;
764
- const recentThreshold = now - thresholdValues [ options . recent . threshold ] ;
839
+ const recentThreshold = now - thresholdValues [ filters . recent . threshold ] ;
765
840
766
841
for ( const branch of branches ) {
767
842
const wt = worktreesByBranch . get ( branch . id ) ;
768
843
const worktree : GetOverviewBranch [ 'worktree' ] = wt ? { name : wt . name , uri : wt . uri . toString ( ) } : undefined ;
769
844
770
845
const timestamp = branch . date ?. getTime ( ) ;
771
846
if ( branch . current || wt ?. opened ) {
847
+ const forceOptions = options ?. forceActive ? { force : true } : undefined ;
772
848
prPromises . set ( branch . id , branch . getAssociatedPullRequest ( { avatarSize : 16 } ) ) ;
773
849
if ( wt != null ) {
774
- statusPromises . set ( branch . id , wt . getStatus ( ) ) ;
850
+ statusPromises . set ( branch . id , wt . getStatus ( forceOptions ) ) ;
851
+ } else {
852
+ if ( repoStatusPromise === undefined ) {
853
+ repoStatusPromise = container . git . getStatus ( branch . repoPath ) ;
854
+ }
855
+ statusPromises . set ( branch . id , repoStatusPromise ) ;
775
856
}
776
857
contributorPromises . set ( branch . id , container . git . getBranchContributorOverview ( branch . repoPath , branch . ref ) ) ;
777
858
@@ -811,8 +892,8 @@ async function getOverviewBranches(
811
892
}
812
893
}
813
894
814
- if ( options ?. stale ?. show === true ) {
815
- const staleThreshold = now - thresholdValues [ options . stale . threshold ] ;
895
+ if ( filters ?. stale ?. show === true ) {
896
+ const staleThreshold = now - thresholdValues [ filters . stale . threshold ] ;
816
897
sortBranches ( branches , {
817
898
missingUpstream : true ,
818
899
orderBy : 'date:asc' ,
@@ -861,6 +942,18 @@ async function getOverviewBranches(
861
942
}
862
943
}
863
944
945
+ await enrichOverviewBranches ( overviewBranches , prPromises , statusPromises , contributorPromises ) ;
946
+
947
+ return overviewBranches ;
948
+ }
949
+
950
+ // FIXME: support partial enrichment
951
+ async function enrichOverviewBranches (
952
+ overviewBranches : GetOverviewBranches ,
953
+ prPromises : Map < string , Promise < PullRequest | undefined > > ,
954
+ statusPromises : Map < string , Promise < GitStatus | undefined > > ,
955
+ contributorPromises : Map < string , Promise < BranchContributorOverview | undefined > > ,
956
+ ) {
864
957
const [ prResults , statusResults , contributorResults ] = await Promise . allSettled ( [
865
958
Promise . allSettled ( map ( prPromises , ( [ id , pr ] ) => pr . then < [ string , PullRequest | undefined ] > ( pr => [ id , pr ] ) ) ) ,
866
959
Promise . allSettled (
@@ -941,6 +1034,4 @@ async function getOverviewBranches(
941
1034
branch . contributors = contributors ;
942
1035
}
943
1036
}
944
-
945
- return overviewBranches ;
946
1037
}
0 commit comments