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
packages/app/public/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.js
*.map

View File

@ -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;
}

View File

@ -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<string, any>;
};
@ -80,28 +83,7 @@ export class DataCenter {
return provider;
}
async getWorkspace(
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>) {
async setConfig(workspace: string, config: Record<string, any>) {
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<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();
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)));
}
}

View File

@ -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']);
});