feat: improve data center api typo & docs

This commit is contained in:
DarkSky
2023-01-03 20:46:38 +08:00
parent 72d38f1e70
commit 2ee99a37fb
4 changed files with 91 additions and 71 deletions
+2
View File
@@ -0,0 +1,2 @@
*.js
*.map
@@ -18,7 +18,7 @@ const DynamicBlocksuite = ({
const openWorkspace: LoadWorkspaceHandler = async (workspaceId: string) => { const openWorkspace: LoadWorkspaceHandler = async (workspaceId: string) => {
if (workspaceId) { if (workspaceId) {
const dc = await getDataCenter(); const dc = await getDataCenter();
return dc.getWorkspace(workspaceId, { providerId: 'affine' }); return dc.load(workspaceId, { providerId: 'affine' });
} else { } else {
return null; return null;
} }
+55 -39
View File
@@ -8,8 +8,11 @@ import { AffineProvider, BaseProvider } from './provider/index.js';
import { LocalProvider } from './provider/index.js'; import { LocalProvider } from './provider/index.js';
import { getKVConfigure } from './store.js'; import { getKVConfigure } from './store.js';
type GetWorkspaceParams = { // load workspace's config
type LoadConfig = {
// use witch provider load data
providerId?: string; providerId?: string;
// provider config
config?: Record<string, any>; config?: Record<string, any>;
}; };
@@ -80,28 +83,7 @@ export class DataCenter {
return provider; return provider;
} }
async getWorkspace( async setConfig(workspace: string, config: Record<string, any>) {
id: string,
params: GetWorkspaceParams = {}
): Promise<Workspace | null> {
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<string, any>) {
const values = Object.entries(config); const values = Object.entries(config);
if (values.length) { if (values.length) {
const configure = getKVConfigure(workspace); 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<Workspace | null> {
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<Workspace | null> {
await this.destroy(workspaceId);
return this.load(workspaceId, config);
}
async list() {
const keys = await this._config.keys(); const keys = await this._config.keys();
return keys return keys
.filter(k => k.startsWith('workspace:')) .filter(k => k.startsWith('workspace:'))
.map(k => k.split(':')[1]); .map(k => k.split(':')[1]);
} }
async destroyWorkspace(id: string) { // delete local workspace's data
const provider = await this._workspaces.get(id); async delete(workspaceId: string) {
await this._config.delete(`workspace:${workspaceId}:provider`);
const provider = await this._workspaces.get(workspaceId);
if (provider) { if (provider) {
this._workspaces.delete(id); this._workspaces.delete(workspaceId);
await provider.destroy(); // clear workspace data implement by provider
}
}
async removeWorkspace(id: string) {
await this._config.delete(`workspace:${id}:provider`);
const provider = await this._workspaces.get(id);
if (provider) {
this._workspaces.delete(id);
await provider.clear(); await provider.clear();
} }
} }
async clearWorkspaces() { // clear all local workspace's data
const workspaces = await this.listWorkspace(); async clear() {
await Promise.all(workspaces.map(id => this.removeWorkspace(id))); const workspaces = await this.list();
await Promise.all(workspaces.map(id => this.delete(id)));
} }
} }
+33 -31
View File
@@ -7,37 +7,38 @@ import 'fake-indexeddb/auto';
test('init data center', async () => { test('init data center', async () => {
const dataCenter = await getDataCenter(); const dataCenter = await getDataCenter();
expect(dataCenter).toBeTruthy(); expect(dataCenter).toBeTruthy();
await dataCenter.clearWorkspaces(); await dataCenter.clear();
const workspace = await dataCenter.getWorkspace('test1'); const workspace = await dataCenter.load('test1');
expect(workspace).toBeTruthy(); expect(workspace).toBeTruthy();
}); });
test('init data center singleton', async () => { test('init data center singleton', async () => {
// data center is singleton
const [dc1, dc2] = await Promise.all([getDataCenter(), getDataCenter()]); const [dc1, dc2] = await Promise.all([getDataCenter(), getDataCenter()]);
expect(dc1).toEqual(dc2); expect(dc1).toEqual(dc2);
const [ws1, ws2] = await Promise.all([ // load same workspace will get same instance
dc1.getWorkspace('test1'), const [ws1, ws2] = await Promise.all([dc1.load('test1'), dc2.load('test1')]);
dc2.getWorkspace('test1'),
]);
expect(ws1).toEqual(ws2); expect(ws1).toEqual(ws2);
}); });
test('should init error with unknown provider', async () => { test('should init error with unknown provider', async () => {
const dataCenter = await getDataCenter(); const dc = await getDataCenter();
await dataCenter.clearWorkspaces(); await dc.clear();
// load workspace with unknown provider will throw error
test.fail(); test.fail();
await dataCenter.getWorkspace('test2', { providerId: 'not exist provider' }); await dc.load('test2', { providerId: 'not exist provider' });
}); });
test.skip('init affine provider', async () => { test.skip('init affine provider', async () => {
const dataCenter = await getDataCenter(); const dataCenter = await getDataCenter();
await dataCenter.clearWorkspaces(); await dataCenter.clear();
// load workspace with affine provider
// TODO: set constant token for testing // TODO: set constant token for testing
const workspace = await dataCenter.getWorkspace('6', { const workspace = await dataCenter.load('6', {
providerId: 'affine', providerId: 'affine',
config: { token: 'YOUR_TOKEN' }, config: { token: 'YOUR_TOKEN' },
}); });
@@ -46,16 +47,16 @@ test.skip('init affine provider', async () => {
test('list workspaces', async () => { test('list workspaces', async () => {
const dataCenter = await getDataCenter(); const dataCenter = await getDataCenter();
await dataCenter.clearWorkspaces(); await dataCenter.clear();
await Promise.all([ await Promise.all([
dataCenter.getWorkspace('test3'), dataCenter.load('test3'),
dataCenter.getWorkspace('test4'), dataCenter.load('test4'),
dataCenter.getWorkspace('test5'), dataCenter.load('test5'),
dataCenter.getWorkspace('test6'), dataCenter.load('test6'),
]); ]);
expect(await dataCenter.listWorkspace()).toStrictEqual([ expect(await dataCenter.list()).toStrictEqual([
'test3', 'test3',
'test4', 'test4',
'test5', 'test5',
@@ -65,25 +66,26 @@ test('list workspaces', async () => {
test('destroy workspaces', async () => { test('destroy workspaces', async () => {
const dataCenter = await getDataCenter(); const dataCenter = await getDataCenter();
await dataCenter.clearWorkspaces(); await dataCenter.clear();
const dc1 = await dataCenter.getWorkspace('test7'); // return new workspace if origin workspace is destroyed
await dataCenter.destroyWorkspace('test7'); const ws1 = await dataCenter.load('test7');
const dc2 = await dataCenter.getWorkspace('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 () => { test('remove workspaces', async () => {
const dataCenter = await getDataCenter(); const dataCenter = await getDataCenter();
await dataCenter.clearWorkspaces(); await dataCenter.clear();
await Promise.all([ // remove workspace will remove workspace data
dataCenter.getWorkspace('test8'), await Promise.all([dataCenter.load('test9'), dataCenter.load('test10')]);
dataCenter.getWorkspace('test9'), await dataCenter.delete('test9');
]); expect(await dataCenter.list()).toStrictEqual(['test10']);
await dataCenter.removeWorkspace('test8');
expect(await dataCenter.listWorkspace()).toStrictEqual(['test9']);
}); });