From 512c0a791e66a937ad062f812b60b0894160ff85 Mon Sep 17 00:00:00 2001 From: MingLiang Wang Date: Sun, 8 Jan 2023 17:43:42 +0800 Subject: [PATCH] feat: add clear all workspaces --- packages/data-center/src/datacenter.ts | 10 ++++++++++ packages/data-center/src/provider/affine/affine.ts | 13 +++++++++++-- packages/data-center/src/provider/local/local.ts | 12 +++++++----- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/packages/data-center/src/datacenter.ts b/packages/data-center/src/datacenter.ts index a065228670..d88762b436 100644 --- a/packages/data-center/src/datacenter.ts +++ b/packages/data-center/src/datacenter.ts @@ -279,6 +279,16 @@ export class DataCenter { 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 * @param {File} file file of workspace. diff --git a/packages/data-center/src/provider/affine/affine.ts b/packages/data-center/src/provider/affine/affine.ts index c1caa4048d..c2bea657db 100644 --- a/packages/data-center/src/provider/affine/affine.ts +++ b/packages/data-center/src/provider/affine/affine.ts @@ -185,12 +185,21 @@ export class AffineProvider extends BaseProvider { } public override async delete(id: string): Promise { - // TODO delete workspace all local data + await this.close(id); + IndexedDBProvider.delete(id); await deleteWorkspace({ id }); } public override async clear(): Promise { - // 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(); } diff --git a/packages/data-center/src/provider/local/local.ts b/packages/data-center/src/provider/local/local.ts index 58dfd7acd6..20ed420ba1 100644 --- a/packages/data-center/src/provider/local/local.ts +++ b/packages/data-center/src/provider/local/local.ts @@ -38,22 +38,23 @@ export class LocalProvider extends BaseProvider { return workspace; } - override loadWorkspaces() { + override loadWorkspaces(): Promise { const workspaceStr = storage.getItem(WORKSPACE_KEY); + let workspaces: WS[] = []; if (workspaceStr) { try { - return JSON.parse(workspaceStr); + workspaces = JSON.parse(workspaceStr) as WS[]; } catch (error) { this._logger(`Failed to parse workspaces from storage`); } } - return []; + return Promise.resolve(workspaces); } public override async delete(id: string): Promise { const index = this._workspacesList.findIndex(ws => ws.id === id); if (index !== -1) { - // TODO delete workspace all data + IndexedDBProvider.delete(id); this._workspacesList.splice(index, 1); this._storeWorkspaces(this._workspacesList); } else { @@ -75,7 +76,8 @@ export class LocalProvider extends BaseProvider { } public override async clear(): Promise { - // TODO: clear all data + const workspaces = await this.loadWorkspaces(); + workspaces.forEach(ws => IndexedDBProvider.delete(ws.id)); this._storeWorkspaces([]); } }