@@ -46,18 +46,26 @@ export class CopilotStateModel extends Disposable {
4646 }
4747 }
4848
49- set ( pullRequestModel : PullRequestModel , status : CopilotPRStatus ) : void {
50- const key = this . makeKey ( pullRequestModel . remote . owner , pullRequestModel . remote . repositoryName , pullRequestModel . number ) ;
51- const currentStatus = this . _states . get ( key ) ;
52- if ( currentStatus ?. status === status ) {
53- return ;
49+ set ( statuses : { pullRequestModel : PullRequestModel , status : CopilotPRStatus } [ ] ) : void {
50+ const changedModels : PullRequestModel [ ] = [ ] ;
51+ const changedKeys : string [ ] = [ ] ;
52+ for ( const { pullRequestModel, status } of statuses ) {
53+ const key = this . makeKey ( pullRequestModel . remote . owner , pullRequestModel . remote . repositoryName , pullRequestModel . number ) ;
54+ const currentStatus = this . _states . get ( key ) ;
55+ if ( currentStatus ?. status === status ) {
56+ continue ;
57+ }
58+ this . _states . set ( key , { item : pullRequestModel , status } ) ;
59+ changedModels . push ( pullRequestModel ) ;
60+ changedKeys . push ( key ) ;
5461 }
55- this . _states . set ( key , { item : pullRequestModel , status } ) ;
5662 if ( this . _isInitialized ) {
57- this . _showNotification . add ( key ) ;
58- this . _onDidChangeNotifications . fire ( pullRequestModel ? [ pullRequestModel ] : [ ] ) ;
63+ changedKeys . forEach ( key => this . _showNotification . add ( key ) ) ;
64+ this . _onDidChangeNotifications . fire ( changedModels ) ;
65+ }
66+ if ( changedModels . length > 0 ) {
67+ this . _onDidChangeStates . fire ( ) ;
5968 }
60- this . _onDidChangeStates . fire ( ) ;
6169 }
6270
6371 get ( owner : string , repo : string , prNumber : number ) : CopilotPRStatus {
@@ -86,13 +94,42 @@ export class CopilotStateModel extends Disposable {
8694 get isInitialized ( ) : boolean {
8795 return this . _isInitialized ;
8896 }
97+
98+ getCounts ( ) : { total : number ; inProgress : number ; error : number } {
99+ let inProgressCount = 0 ;
100+ let errorCount = 0 ;
101+
102+ for ( const state of this . _states . values ( ) ) {
103+ if ( state . status === CopilotPRStatus . Started ) {
104+ inProgressCount ++ ;
105+ } else if ( state . status === CopilotPRStatus . Failed ) {
106+ errorCount ++ ;
107+ }
108+ }
109+
110+ return {
111+ total : this . _states . size ,
112+ inProgress : inProgressCount ,
113+ error : errorCount
114+ } ;
115+ }
89116}
90117
91118export class CopilotPRWatcher extends Disposable {
92119
93120 constructor ( private readonly _reposManager : RepositoriesManager , private readonly _model : CopilotStateModel ) {
94121 super ( ) ;
122+ if ( this . _reposManager . folderManagers . length === 0 ) {
123+ const initDisposable = this . _reposManager . onDidChangeAnyGitHubRepository ( ( ) => {
124+ initDisposable . dispose ( ) ;
125+ this . _initialize ( ) ;
126+ } ) ;
127+ } else {
128+ this . _initialize ( ) ;
129+ }
130+ }
95131
132+ private _initialize ( ) {
96133 this . _getStateChanges ( ) ;
97134 this . _pollForChanges ( ) ;
98135 this . _register ( this . _reposManager . onDidChangeAnyPullRequests ( ( ) => this . _getStateChanges ( ) ) ) ;
@@ -138,6 +175,7 @@ export class CopilotPRWatcher extends Disposable {
138175 const unseenKeys : Set < string > = new Set ( this . _model . keys ( ) ) ;
139176 let initialized = 0 ;
140177
178+ const changes : { pullRequestModel : PullRequestModel , status : CopilotPRStatus } [ ] = [ ] ;
141179 for ( const folderManager of this . _reposManager . folderManagers ) {
142180 // It doesn't matter which repo we use since the query will specify the owner/repo.
143181 const githubRepository = folderManager . gitHubRepositories [ 0 ] ;
@@ -161,14 +199,15 @@ export class CopilotPRWatcher extends Disposable {
161199 prNumber : pr . number ,
162200 status : latestEvent
163201 } ) ;
164- this . _model . set ( pr , latestEvent ) ;
202+ changes . push ( { pullRequestModel : pr , status : latestEvent } ) ;
165203 }
166204 }
167205
168206 for ( const key of unseenKeys ) {
169207 this . _model . deleteKey ( key ) ;
170208 }
171209 }
210+ this . _model . set ( changes ) ;
172211 if ( ! this . _model . isInitialized ) {
173212 if ( ( initialized === this . _reposManager . folderManagers . length ) && ( this . _reposManager . folderManagers . length > 0 ) ) {
174213 this . _model . setInitialized ( ) ;
0 commit comments