From 2ee99a37fb256da408d414b7209fb8b638fd06e4 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Tue, 3 Jan 2023 20:46:38 +0800 Subject: [PATCH] feat: improve data center api typo & docs --- packages/app/public/.gitignore | 2 + .../app-state-provider/dynamic-blocksuite.tsx | 2 +- packages/data-center/src/datacenter.ts | 94 +++++++++++-------- packages/data-center/tests/datacenter.spec.ts | 64 +++++++------ 4 files changed, 91 insertions(+), 71 deletions(-) create mode 100644 packages/app/public/.gitignore diff --git a/packages/app/public/.gitignore b/packages/app/public/.gitignore new file mode 100644 index 0000000000..69a24fb744 --- /dev/null +++ b/packages/app/public/.gitignore @@ -0,0 +1,2 @@ +*.js +*.map \ No newline at end of file diff --git a/packages/app/src/providers/app-state-provider/dynamic-blocksuite.tsx b/packages/app/src/providers/app-state-provider/dynamic-blocksuite.tsx index 61eac14e03..3a8fb7671c 100644 --- a/packages/app/src/providers/app-state-provider/dynamic-blocksuite.tsx +++ b/packages/app/src/providers/app-state-provider/dynamic-blocksuite.tsx @@ -18,7 +18,7 @@ const DynamicBlocksuite = ({ const openWorkspace: LoadWorkspaceHandler = async (workspaceId: string) => { if (workspaceId) { const dc = await getDataCenter(); - return dc.getWorkspace(workspaceId, { providerId: 'affine' }); + return dc.load(workspaceId, { providerId: 'affine' }); } else { return null; } diff --git a/packages/data-center/src/datacenter.ts b/packages/data-center/src/datacenter.ts index 348247ed6d..b43a7c7b61 100644 --- a/packages/data-center/src/datacenter.ts +++ b/packages/data-center/src/datacenter.ts @@ -8,8 +8,11 @@ import { AffineProvider, BaseProvider } from './provider/index.js'; import { LocalProvider } from './provider/index.js'; import { getKVConfigure } from './store.js'; -type GetWorkspaceParams = { +// load workspace's config +type LoadConfig = { + // use witch provider load data providerId?: string; + // provider config config?: Record; }; @@ -80,28 +83,7 @@ export class DataCenter { return provider; } - async getWorkspace( - id: string, - params: GetWorkspaceParams = {} - ): Promise { - const { providerId = 'local', config = {} } = params; - if (id) { - if (!this._workspaces.has(id)) { - this._workspaces.set( - id, - this.setWorkspaceConfig(id, config).then(() => - this._getWorkspace(id, providerId) - ) - ); - } - const workspace = this._workspaces.get(id); - assert(workspace); - return workspace.then(w => w.workspace); - } - return null; - } - - async setWorkspaceConfig(workspace: string, config: Record) { + async setConfig(workspace: string, config: Record) { const values = Object.entries(config); if (values.length) { const configure = getKVConfigure(workspace); @@ -109,32 +91,66 @@ export class DataCenter { } } - async listWorkspace() { + // load workspace data to memory + async load( + workspaceId: string, + params: LoadConfig = {} + ): Promise { + const { providerId = 'local', config = {} } = params; + if (workspaceId) { + if (!this._workspaces.has(workspaceId)) { + this._workspaces.set( + workspaceId, + this.setConfig(workspaceId, config).then(() => + this._getWorkspace(workspaceId, providerId) + ) + ); + } + const workspace = this._workspaces.get(workspaceId); + assert(workspace); + return workspace.then(w => w.workspace); + } + return null; + } + + // destroy workspace's instance in memory + async destroy(workspaceId: string) { + const provider = await this._workspaces.get(workspaceId); + if (provider) { + this._workspaces.delete(workspaceId); + await provider.destroy(); + } + } + + async reload( + workspaceId: string, + config: LoadConfig = {} + ): Promise { + await this.destroy(workspaceId); + return this.load(workspaceId, config); + } + + async list() { const keys = await this._config.keys(); return keys .filter(k => k.startsWith('workspace:')) .map(k => k.split(':')[1]); } - async destroyWorkspace(id: string) { - const provider = await this._workspaces.get(id); + // delete local workspace's data + async delete(workspaceId: string) { + await this._config.delete(`workspace:${workspaceId}:provider`); + const provider = await this._workspaces.get(workspaceId); if (provider) { - this._workspaces.delete(id); - await provider.destroy(); - } - } - - async removeWorkspace(id: string) { - await this._config.delete(`workspace:${id}:provider`); - const provider = await this._workspaces.get(id); - if (provider) { - this._workspaces.delete(id); + this._workspaces.delete(workspaceId); + // clear workspace data implement by provider await provider.clear(); } } - async clearWorkspaces() { - const workspaces = await this.listWorkspace(); - await Promise.all(workspaces.map(id => this.removeWorkspace(id))); + // clear all local workspace's data + async clear() { + const workspaces = await this.list(); + await Promise.all(workspaces.map(id => this.delete(id))); } } diff --git a/packages/data-center/tests/datacenter.spec.ts b/packages/data-center/tests/datacenter.spec.ts index a5785c3b37..c924bfd2cb 100644 --- a/packages/data-center/tests/datacenter.spec.ts +++ b/packages/data-center/tests/datacenter.spec.ts @@ -7,37 +7,38 @@ import 'fake-indexeddb/auto'; test('init data center', async () => { const dataCenter = await getDataCenter(); expect(dataCenter).toBeTruthy(); - await dataCenter.clearWorkspaces(); + await dataCenter.clear(); - const workspace = await dataCenter.getWorkspace('test1'); + const workspace = await dataCenter.load('test1'); expect(workspace).toBeTruthy(); }); test('init data center singleton', async () => { + // data center is singleton const [dc1, dc2] = await Promise.all([getDataCenter(), getDataCenter()]); expect(dc1).toEqual(dc2); - const [ws1, ws2] = await Promise.all([ - dc1.getWorkspace('test1'), - dc2.getWorkspace('test1'), - ]); + // load same workspace will get same instance + const [ws1, ws2] = await Promise.all([dc1.load('test1'), dc2.load('test1')]); expect(ws1).toEqual(ws2); }); test('should init error with unknown provider', async () => { - const dataCenter = await getDataCenter(); - await dataCenter.clearWorkspaces(); + const dc = await getDataCenter(); + await dc.clear(); + // load workspace with unknown provider will throw error test.fail(); - await dataCenter.getWorkspace('test2', { providerId: 'not exist provider' }); + await dc.load('test2', { providerId: 'not exist provider' }); }); test.skip('init affine provider', async () => { const dataCenter = await getDataCenter(); - await dataCenter.clearWorkspaces(); + await dataCenter.clear(); + // load workspace with affine provider // TODO: set constant token for testing - const workspace = await dataCenter.getWorkspace('6', { + const workspace = await dataCenter.load('6', { providerId: 'affine', config: { token: 'YOUR_TOKEN' }, }); @@ -46,16 +47,16 @@ test.skip('init affine provider', async () => { test('list workspaces', async () => { const dataCenter = await getDataCenter(); - await dataCenter.clearWorkspaces(); + await dataCenter.clear(); await Promise.all([ - dataCenter.getWorkspace('test3'), - dataCenter.getWorkspace('test4'), - dataCenter.getWorkspace('test5'), - dataCenter.getWorkspace('test6'), + dataCenter.load('test3'), + dataCenter.load('test4'), + dataCenter.load('test5'), + dataCenter.load('test6'), ]); - expect(await dataCenter.listWorkspace()).toStrictEqual([ + expect(await dataCenter.list()).toStrictEqual([ 'test3', 'test4', 'test5', @@ -65,25 +66,26 @@ test('list workspaces', async () => { test('destroy workspaces', async () => { const dataCenter = await getDataCenter(); - await dataCenter.clearWorkspaces(); + await dataCenter.clear(); - const dc1 = await dataCenter.getWorkspace('test7'); - await dataCenter.destroyWorkspace('test7'); - const dc2 = await dataCenter.getWorkspace('test7'); + // return new workspace if origin workspace is destroyed + const ws1 = await dataCenter.load('test7'); + await dataCenter.destroy('test7'); + const ws2 = await dataCenter.load('test7'); + expect(ws1 !== ws2).toBeTruthy(); - expect(dc1 !== dc2).toBeTruthy(); + // return new workspace if workspace is reload + const ws3 = await dataCenter.load('test8'); + const ws4 = await dataCenter.reload('test8', { providerId: 'affine' }); + expect(ws3 !== ws4).toBeTruthy(); }); test('remove workspaces', async () => { const dataCenter = await getDataCenter(); - await dataCenter.clearWorkspaces(); + await dataCenter.clear(); - await Promise.all([ - dataCenter.getWorkspace('test8'), - dataCenter.getWorkspace('test9'), - ]); - - await dataCenter.removeWorkspace('test8'); - - expect(await dataCenter.listWorkspace()).toStrictEqual(['test9']); + // remove workspace will remove workspace data + await Promise.all([dataCenter.load('test9'), dataCenter.load('test10')]); + await dataCenter.delete('test9'); + expect(await dataCenter.list()).toStrictEqual(['test10']); });