@@ -25,11 +25,20 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
25
25
private readonly _onDidChangeProjects = new EventEmitter < ProjectArray | undefined > ( ) ;
26
26
public readonly onDidChangeProjects = this . _onDidChangeProjects . event ;
27
27
28
+ /**
29
+ * Fires the onDidChangeProjects event if updateBool is true.
30
+ */
31
+ private fireDidChangeProjects ( updateBool : boolean , projects : ProjectArray ) {
32
+ if ( updateBool ) {
33
+ this . _onDidChangeProjects . fire ( projects ) ;
34
+ }
35
+ }
36
+
28
37
// Debounce the updateProjects method to avoid excessive update calls
29
38
private readonly updateDebounce = createSimpleDebounce ( 100 , ( ) => this . updateProjects ( ) ) ;
30
39
31
40
initialize ( ) : void {
32
- this . add ( this . getInitialProjects ( ) ) ;
41
+ this . add ( this . getInitialProjects ( ) , true ) ;
33
42
this . disposables . push (
34
43
this . _onDidChangeProjects ,
35
44
new Disposable ( ( ) => this . _projects . clear ( ) ) ,
@@ -95,7 +104,7 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
95
104
* then updates the internal _projects map to reflect the current state and
96
105
* fires the onDidChangeProjects event if there are any changes.
97
106
*/
98
- private updateProjects ( ) : void {
107
+ private updateProjects ( updateBool : boolean = true ) : void {
99
108
const newProjects : ProjectArray = this . getInitialProjects ( ) ;
100
109
const existingProjects = Array . from ( this . _projects . values ( ) ) ;
101
110
@@ -112,7 +121,7 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
112
121
projectsToAdd . forEach ( ( w ) => this . _projects . set ( w . uri . toString ( ) , w ) ) ;
113
122
114
123
if ( projectsToRemove . length > 0 || projectsToAdd . length > 0 ) {
115
- this . _onDidChangeProjects . fire ( Array . from ( this . _projects . values ( ) ) ) ;
124
+ this . fireDidChangeProjects ( updateBool , Array . from ( this . _projects . values ( ) ) ) ;
116
125
}
117
126
}
118
127
@@ -124,7 +133,7 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
124
133
return new PythonProjectsImpl ( name , uri , options ) ;
125
134
}
126
135
127
- async add ( projects : PythonProject | ProjectArray ) : Promise < void > {
136
+ async add ( projects : PythonProject | ProjectArray , updateBool : boolean = true ) : Promise < void > {
128
137
const _projects = Array . isArray ( projects ) ? projects : [ projects ] ;
129
138
if ( _projects . length === 0 ) {
130
139
return ;
@@ -153,21 +162,21 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
153
162
// handles adding the project to this._projects map
154
163
return this . _projects . set ( currProject . uri . toString ( ) , currProject ) ;
155
164
} ) ;
156
- this . _onDidChangeProjects . fire ( Array . from ( this . _projects . values ( ) ) ) ;
165
+ this . fireDidChangeProjects ( updateBool , Array . from ( this . _projects . values ( ) ) ) ;
157
166
158
167
if ( edits . length > 0 ) {
159
168
await addPythonProjectSetting ( edits ) ;
160
169
}
161
170
}
162
171
163
- remove ( projects : PythonProject | ProjectArray ) : void {
172
+ remove ( projects : PythonProject | ProjectArray , updateBool : boolean = true ) : void {
164
173
const _projects = Array . isArray ( projects ) ? projects : [ projects ] ;
165
174
if ( _projects . length === 0 ) {
166
175
return ;
167
176
}
168
177
169
178
_projects . forEach ( ( w ) => this . _projects . delete ( w . uri . toString ( ) ) ) ;
170
- this . _onDidChangeProjects . fire ( Array . from ( this . _projects . values ( ) ) ) ;
179
+ this . fireDidChangeProjects ( updateBool , Array . from ( this . _projects . values ( ) ) ) ;
171
180
}
172
181
173
182
/**
@@ -188,8 +197,8 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
188
197
return ;
189
198
}
190
199
191
- // Remove the old project
192
- this . remove ( project ) ;
200
+ // Remove the old project, do not fire event yet
201
+ this . remove ( project , false ) ;
193
202
194
203
// Prepare new values
195
204
const name = newName ?? project . name ;
@@ -200,9 +209,12 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
200
209
iconPath : newOptions ?. iconPath ?? ( project as PythonProjectsImpl ) . iconPath ,
201
210
} ;
202
211
203
- // Create and add the new project
212
+ // Create and add the new project, do not fire event yet
204
213
const updatedProject = this . create ( name , uri , options ) ;
205
- await this . add ( updatedProject ) ;
214
+ await this . add ( updatedProject , false ) ;
215
+
216
+ // Now trigger the update event only once
217
+ this . updateDebounce . trigger ( ) ;
206
218
}
207
219
208
220
getProjects ( uris ?: Uri [ ] ) : ReadonlyArray < PythonProject > {
0 commit comments