feat: add clear all workspaces

This commit is contained in:
MingLiang Wang
2023-01-08 17:43:42 +08:00
parent d154a28ad1
commit 512c0a791e
3 changed files with 28 additions and 7 deletions

View File

@@ -279,6 +279,16 @@ export class DataCenter {
return await this._transWorkspaceProvider(workspace, 'affine'); return await this._transWorkspaceProvider(workspace, 'affine');
} }
/**
* @deprecated
* clear all workspaces and data
*/
public async clear() {
for (const provider of this.providerMap.values()) {
await provider.clear();
}
}
/** /**
* Select a file to import the workspace * Select a file to import the workspace
* @param {File} file file of workspace. * @param {File} file file of workspace.

View File

@@ -185,12 +185,21 @@ export class AffineProvider extends BaseProvider {
} }
public override async delete(id: string): Promise<void> { public override async delete(id: string): Promise<void> {
// TODO delete workspace all local data await this.close(id);
IndexedDBProvider.delete(id);
await deleteWorkspace({ id }); await deleteWorkspace({ id });
} }
public override async clear(): Promise<void> { public override async clear(): Promise<void> {
// TODO: clear all workspaces source for (const w of this._workspacesCache.values()) {
if (w.room) {
try {
await this.delete(w.room);
} catch (e) {
this._logger('has a problem of delete workspace ', e);
}
}
}
this._workspacesCache.clear(); this._workspacesCache.clear();
} }

View File

@@ -38,22 +38,23 @@ export class LocalProvider extends BaseProvider {
return workspace; return workspace;
} }
override loadWorkspaces() { override loadWorkspaces(): Promise<WS[]> {
const workspaceStr = storage.getItem(WORKSPACE_KEY); const workspaceStr = storage.getItem(WORKSPACE_KEY);
let workspaces: WS[] = [];
if (workspaceStr) { if (workspaceStr) {
try { try {
return JSON.parse(workspaceStr); workspaces = JSON.parse(workspaceStr) as WS[];
} catch (error) { } catch (error) {
this._logger(`Failed to parse workspaces from storage`); this._logger(`Failed to parse workspaces from storage`);
} }
} }
return []; return Promise.resolve(workspaces);
} }
public override async delete(id: string): Promise<void> { public override async delete(id: string): Promise<void> {
const index = this._workspacesList.findIndex(ws => ws.id === id); const index = this._workspacesList.findIndex(ws => ws.id === id);
if (index !== -1) { if (index !== -1) {
// TODO delete workspace all data IndexedDBProvider.delete(id);
this._workspacesList.splice(index, 1); this._workspacesList.splice(index, 1);
this._storeWorkspaces(this._workspacesList); this._storeWorkspaces(this._workspacesList);
} else { } else {
@@ -75,7 +76,8 @@ export class LocalProvider extends BaseProvider {
} }
public override async clear(): Promise<void> { public override async clear(): Promise<void> {
// TODO: clear all data const workspaces = await this.loadWorkspaces();
workspaces.forEach(ws => IndexedDBProvider.delete(ws.id));
this._storeWorkspaces([]); this._storeWorkspaces([]);
} }
} }