@@ -25,11 +25,20 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
2525 private readonly _onDidChangeProjects = new EventEmitter < ProjectArray | undefined > ( ) ;
2626 public readonly onDidChangeProjects = this . _onDidChangeProjects . event ;
2727
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+
2837 // Debounce the updateProjects method to avoid excessive update calls
2938 private readonly updateDebounce = createSimpleDebounce ( 100 , ( ) => this . updateProjects ( ) ) ;
3039
3140 initialize ( ) : void {
32- this . add ( this . getInitialProjects ( ) ) ;
41+ this . add ( this . getInitialProjects ( ) , true ) ;
3342 this . disposables . push (
3443 this . _onDidChangeProjects ,
3544 new Disposable ( ( ) => this . _projects . clear ( ) ) ,
@@ -95,7 +104,7 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
95104 * then updates the internal _projects map to reflect the current state and
96105 * fires the onDidChangeProjects event if there are any changes.
97106 */
98- private updateProjects ( ) : void {
107+ private updateProjects ( updateBool : boolean = true ) : void {
99108 const newProjects : ProjectArray = this . getInitialProjects ( ) ;
100109 const existingProjects = Array . from ( this . _projects . values ( ) ) ;
101110
@@ -112,7 +121,7 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
112121 projectsToAdd . forEach ( ( w ) => this . _projects . set ( w . uri . toString ( ) , w ) ) ;
113122
114123 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 ( ) ) ) ;
116125 }
117126 }
118127
@@ -124,7 +133,7 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
124133 return new PythonProjectsImpl ( name , uri , options ) ;
125134 }
126135
127- async add ( projects : PythonProject | ProjectArray ) : Promise < void > {
136+ async add ( projects : PythonProject | ProjectArray , updateBool : boolean = true ) : Promise < void > {
128137 const _projects = Array . isArray ( projects ) ? projects : [ projects ] ;
129138 if ( _projects . length === 0 ) {
130139 return ;
@@ -153,21 +162,21 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
153162 // handles adding the project to this._projects map
154163 return this . _projects . set ( currProject . uri . toString ( ) , currProject ) ;
155164 } ) ;
156- this . _onDidChangeProjects . fire ( Array . from ( this . _projects . values ( ) ) ) ;
165+ this . fireDidChangeProjects ( updateBool , Array . from ( this . _projects . values ( ) ) ) ;
157166
158167 if ( edits . length > 0 ) {
159168 await addPythonProjectSetting ( edits ) ;
160169 }
161170 }
162171
163- remove ( projects : PythonProject | ProjectArray ) : void {
172+ remove ( projects : PythonProject | ProjectArray , updateBool : boolean = true ) : void {
164173 const _projects = Array . isArray ( projects ) ? projects : [ projects ] ;
165174 if ( _projects . length === 0 ) {
166175 return ;
167176 }
168177
169178 _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 ( ) ) ) ;
171180 }
172181
173182 /**
@@ -188,8 +197,8 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
188197 return ;
189198 }
190199
191- // Remove the old project
192- this . remove ( project ) ;
200+ // Remove the old project, do not fire event yet
201+ this . remove ( project , false ) ;
193202
194203 // Prepare new values
195204 const name = newName ?? project . name ;
@@ -200,9 +209,12 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
200209 iconPath : newOptions ?. iconPath ?? ( project as PythonProjectsImpl ) . iconPath ,
201210 } ;
202211
203- // Create and add the new project
212+ // Create and add the new project, do not fire event yet
204213 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 ( ) ;
206218 }
207219
208220 getProjects ( uris ?: Uri [ ] ) : ReadonlyArray < PythonProject > {
0 commit comments