Skip to content

Commit 407c82c

Browse files
committed
Improve service reset handling for no workspace folder
1 parent 115aa7d commit 407c82c

4 files changed

Lines changed: 44 additions & 81 deletions

File tree

src/vs/workbench/contrib/roopik/browser/contributions/startupContribution.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,20 @@ export class RoopikStartupContribution extends Disposable implements IWorkbenchC
5757

5858
/**
5959
* Initialize Roopik services with workspace path
60+
* If no workspace folder, clear services to remove stale data
6061
*/
6162
private async initializeRoopikServices(): Promise<void> {
6263
await this.lifecycleService.when(LifecyclePhase.Restored);
6364

6465
const workspace = this.workspaceContextService.getWorkspace();
6566
if (!workspace.folders || workspace.folders.length === 0) {
66-
this.logger.warn('No workspace folder found, services not initialized');
67+
this.logger.info('No workspace folder found, clearing services');
68+
// Clear services to remove any stale data from previous sessions
69+
try {
70+
await this.clearServices();
71+
} catch (err) {
72+
this.logger.error('Failed to clear services on startup', { error: err });
73+
}
6774
return;
6875
}
6976

@@ -88,7 +95,7 @@ export class RoopikStartupContribution extends Disposable implements IWorkbenchC
8895

8996
// If no folders, clear all services (workspace closed)
9097
if (!workspace.folders || workspace.folders.length === 0) {
91-
this.logger.info('Workspace closed, clearing services');
98+
// this.logger.info('Workspace closed, clearing services');
9299
try {
93100
await this.clearServices();
94101
} catch (err) {
@@ -98,7 +105,7 @@ export class RoopikStartupContribution extends Disposable implements IWorkbenchC
98105
}
99106

100107
const newWorkspacePath = workspace.folders[0].uri.fsPath;
101-
this.logger.info('Workspace changed, re-initializing services', { workspacePath: newWorkspacePath });
108+
// this.logger.info('Workspace changed, re-initializing services', { workspacePath: newWorkspacePath });
102109

103110
try {
104111
// Re-initialize all services with the new workspace path

src/vs/workbench/contrib/roopik/electron-main/canvas/canvasService.ts

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,11 @@ export class CanvasService implements ICanvasService {
105105
async initialize(workspacePath: string): Promise<void> {
106106
// If already initialized with a DIFFERENT workspace, re-initialize
107107
if (this.initialized && this._workspacePath !== workspacePath) {
108-
this.logger.info('Workspace changed, re-initializing', {
109-
oldPath: this._workspacePath,
110-
newPath: workspacePath
111-
});
108+
// this.logger.info('Workspace changed, re-initializing', {
109+
// oldPath: this._workspacePath,
110+
// newPath: workspacePath
111+
// });
112+
112113
// Clear old state
113114
this.canvases.clear();
114115
this.panelStates.clear();
@@ -169,7 +170,7 @@ export class CanvasService implements ICanvasService {
169170
* Resets to uninitialized state and fires events to update UI
170171
*/
171172
async clear(): Promise<void> {
172-
this.logger.info('Clearing canvas service (workspace closed)');
173+
// this.logger.info('Clearing canvas service (workspace closed)');
173174

174175
// Fire delete events for all canvases so UI updates
175176
for (const [canvasId] of this.canvases) {
@@ -182,7 +183,10 @@ export class CanvasService implements ICanvasService {
182183
this._workspacePath = '';
183184
this.initialized = false;
184185

185-
this.logger.info('Canvas service cleared');
186+
// Fire onDidInitialize to signal UI that service state changed (now uninitialized)
187+
this._onDidInitialize.fire();
188+
189+
// this.logger.info('Canvas service cleared');
186190
}
187191

188192
// ========================================================================
@@ -319,70 +323,17 @@ export class CanvasService implements ICanvasService {
319323
}
320324

321325
async listCanvasesAsync(options?: ListCanvasOptions): Promise<CanvasMeta[]> {
322-
// When initialized, use in-memory cache (Map is always in sync with disk)
326+
// When not initialized (no workspace), return empty list
327+
if (!this.initialized) {
328+
this.logger.debug('Not initialized, returning empty canvas list');
329+
return [];
330+
}
331+
332+
// Use in-memory cache (Map is always in sync with disk)
323333
// This avoids redundant filesystem reads since:
324334
// - loadAllCanvases() populates Map during initialize()
325335
// - createCanvas(), deleteCanvas(), updateCanvas() keep Map in sync
326-
if (this.initialized) {
327-
return this.listCanvases(options);
328-
}
329-
330-
try {
331-
const canvasIds = await this.storageService.listCanvases();
332-
333-
const canvases: CanvasMeta[] = [];
334-
for (const canvasId of canvasIds) {
335-
const meta = await this.storageService.loadCanvasMeta(canvasId);
336-
if (meta) {
337-
canvases.push(meta);
338-
// Also update in-memory cache
339-
if (!this.canvases.has(canvasId)) {
340-
this.canvases.set(canvasId, {
341-
...meta,
342-
isOpen: false,
343-
isFocused: false
344-
});
345-
}
346-
}
347-
}
348-
349-
this.logger.info('listCanvasesAsync: loaded canvases from storage', { count: canvases.length });
350-
351-
// Apply name filter
352-
let result = canvases;
353-
if (options?.nameFilter) {
354-
const filter = options.nameFilter.toLowerCase();
355-
result = result.filter(c => c.name.toLowerCase().includes(filter));
356-
}
357-
358-
// Apply sorting
359-
const sortBy = options?.sortBy || 'updatedAt';
360-
const sortDir = options?.sortDirection || 'desc';
361-
362-
result.sort((a, b) => {
363-
let comparison = 0;
364-
switch (sortBy) {
365-
case 'name':
366-
comparison = a.name.localeCompare(b.name);
367-
break;
368-
case 'createdAt':
369-
comparison = a.createdAt - b.createdAt;
370-
break;
371-
case 'updatedAt':
372-
comparison = a.updatedAt - b.updatedAt;
373-
break;
374-
case 'componentCount':
375-
comparison = a.componentCount - b.componentCount;
376-
break;
377-
}
378-
return sortDir === 'asc' ? comparison : -comparison;
379-
});
380-
381-
return result;
382-
} catch (err) {
383-
this.logger.error('Failed to read canvases from storage', err);
384-
return this.listCanvases(options);
385-
}
336+
return this.listCanvases(options);
386337
}
387338

388339
async updateCanvas(

src/vs/workbench/contrib/roopik/electron-main/component/componentService.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,11 @@ export class ComponentService extends Disposable implements IComponentService {
193193
async initialize(workspacePath: string): Promise<void> {
194194
// Handle workspace change - reset state
195195
if (this.initialized && this._workspacePath !== workspacePath) {
196-
this.logger.info('Workspace changed, re-initializing', {
197-
oldPath: this._workspacePath,
198-
newPath: workspacePath
199-
});
196+
// this.logger.info('Workspace changed, re-initializing', {
197+
// oldPath: this._workspacePath,
198+
// newPath: workspacePath
199+
// });
200+
200201
// Stop file watcher for old workspace
201202
this.fileWatcher.stop();
202203
// Clear old components

src/vs/workbench/contrib/roopik/electron-main/projectStorage/projectStorageService.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ export class ProjectStorageService implements IProjectStorageService {
5656
async initialize(workspacePath: string): Promise<void> {
5757
// Handle workspace change - reset state
5858
if (this.initialized && this._workspacePath !== workspacePath) {
59-
this.logger.info('Workspace changed, re-initializing', {
60-
oldPath: this._workspacePath,
61-
newPath: workspacePath
62-
});
59+
// this.logger.info('Workspace changed, re-initializing', {
60+
// oldPath: this._workspacePath,
61+
// newPath: workspacePath
62+
// });
63+
6364
this.storage = null;
6465
this.initialized = false;
6566
}
@@ -96,7 +97,7 @@ export class ProjectStorageService implements IProjectStorageService {
9697
* Resets to uninitialized state
9798
*/
9899
async clear(): Promise<void> {
99-
this.logger.info('Clearing project storage service (workspace closed)');
100+
// this.logger.info('Clearing project storage service (workspace closed)');
100101

101102
this.storage = null;
102103
this._workspacePath = null;
@@ -105,7 +106,10 @@ export class ProjectStorageService implements IProjectStorageService {
105106
// Fire event so UI clears project list
106107
this._onProjectsChanged.fire();
107108

108-
this.logger.info('Project storage service cleared');
109+
// Fire onDidInitialize to signal UI that service state changed (now uninitialized)
110+
this._onDidInitialize.fire();
111+
112+
// this.logger.info('Project storage service cleared');
109113
}
110114

111115
// ========================================================================
@@ -117,7 +121,7 @@ export class ProjectStorageService implements IProjectStorageService {
117121
*/
118122
async getRecentProjects(limit: number = 5): Promise<ProjectInfo[]> {
119123
if (!this.storage || !this.initialized) {
120-
this.logger.warn('Not initialized, returning empty list');
124+
// this.logger.warn('Not initialized, returning empty list');
121125
return [];
122126
}
123127

0 commit comments

Comments
 (0)