@@ -46,18 +46,26 @@ export class CopilotStateModel extends Disposable {
46
46
}
47
47
}
48
48
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 ) ;
54
61
}
55
- this . _states . set ( key , { item : pullRequestModel , status } ) ;
56
62
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 ( ) ;
59
68
}
60
- this . _onDidChangeStates . fire ( ) ;
61
69
}
62
70
63
71
get ( owner : string , repo : string , prNumber : number ) : CopilotPRStatus {
@@ -86,13 +94,42 @@ export class CopilotStateModel extends Disposable {
86
94
get isInitialized ( ) : boolean {
87
95
return this . _isInitialized ;
88
96
}
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
+ }
89
116
}
90
117
91
118
export class CopilotPRWatcher extends Disposable {
92
119
93
120
constructor ( private readonly _reposManager : RepositoriesManager , private readonly _model : CopilotStateModel ) {
94
121
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
+ }
95
131
132
+ private _initialize ( ) {
96
133
this . _getStateChanges ( ) ;
97
134
this . _pollForChanges ( ) ;
98
135
this . _register ( this . _reposManager . onDidChangeAnyPullRequests ( ( ) => this . _getStateChanges ( ) ) ) ;
@@ -138,6 +175,7 @@ export class CopilotPRWatcher extends Disposable {
138
175
const unseenKeys : Set < string > = new Set ( this . _model . keys ( ) ) ;
139
176
let initialized = 0 ;
140
177
178
+ const changes : { pullRequestModel : PullRequestModel , status : CopilotPRStatus } [ ] = [ ] ;
141
179
for ( const folderManager of this . _reposManager . folderManagers ) {
142
180
// It doesn't matter which repo we use since the query will specify the owner/repo.
143
181
const githubRepository = folderManager . gitHubRepositories [ 0 ] ;
@@ -161,14 +199,15 @@ export class CopilotPRWatcher extends Disposable {
161
199
prNumber : pr . number ,
162
200
status : latestEvent
163
201
} ) ;
164
- this . _model . set ( pr , latestEvent ) ;
202
+ changes . push ( { pullRequestModel : pr , status : latestEvent } ) ;
165
203
}
166
204
}
167
205
168
206
for ( const key of unseenKeys ) {
169
207
this . _model . deleteKey ( key ) ;
170
208
}
171
209
}
210
+ this . _model . set ( changes ) ;
172
211
if ( ! this . _model . isInitialized ) {
173
212
if ( ( initialized === this . _reposManager . folderManagers . length ) && ( this . _reposManager . folderManagers . length > 0 ) ) {
174
213
this . _model . setInitialized ( ) ;
0 commit comments