feat: init workspace & test case

This commit is contained in:
DarkSky
2022-12-30 12:06:08 +08:00
committed by DarkSky
parent 6ed2d467b7
commit 3aca098bee
5 changed files with 65 additions and 46 deletions

View File

@@ -1,4 +1,5 @@
import { Workspace } from '@blocksuite/store';
import assert from 'assert';
import type { BaseProvider } from './provider/index.js';
import { MemoryProvider } from './provider/index.js';
@@ -6,7 +7,7 @@ import { getKVConfigure } from './store.js';
export class DataCenter {
private readonly _providers = new Map<string, typeof BaseProvider>();
private readonly _workspaces = new Map<string, Promise<Workspace>>();
private readonly _workspaces = new Map<string, Promise<BaseProvider>>();
private readonly _config;
static async init(): Promise<DataCenter> {
@@ -24,34 +25,50 @@ export class DataCenter {
this._providers.set(provider.id, provider);
}
private async _initWithProvider(id: string, providerId: string) {
const workspace = new Workspace({ room: id });
const Provider = this._providers.get(providerId);
assert(Provider);
const provider = new Provider();
console.log(`Loading workspace ${id} with provider ${Provider.id}`);
await provider.init(getKVConfigure(id), workspace);
await provider.initData();
console.log(`Workspace ${id} loaded`);
return provider;
}
private async _initWorkspace(
id: string,
workspace: Workspace,
providerId: string
): Promise<Workspace> {
): Promise<BaseProvider> {
const providerKey = `workspace:${id}:provider`;
const providerValue = await this._config.get(providerKey);
if (this._providers.has(providerValue || providerId)) {
if (!providerValue) {
await this._config.set(providerKey, providerId);
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const Provider = this._providers.get(providerId)!;
const provider = new Provider();
await provider.init(getKVConfigure(id), workspace);
return this._initWithProvider(id, await this._config.get(providerKey));
} else {
throw Error(`provider ${providerId} not found`);
}
return workspace;
}
async initWorkspace(
id: string,
workspace: Workspace,
provider = 'memory'
): Promise<Workspace> {
async initWorkspace(id: string, provider = 'memory'): Promise<Workspace> {
if (!this._workspaces.has(id)) {
this._workspaces.set(id, this._initWorkspace(id, workspace, provider));
this._workspaces.set(id, this._initWorkspace(id, provider));
}
const workspace = this._workspaces.get(id);
assert(workspace);
return workspace.then(w => w.workspace);
}
return this._workspaces.get(id)!;
setWorkspaceConfig(workspace: string, key: string, value: any) {
const config = getKVConfigure(workspace);
return config.set(key, value);
}
}

View File

@@ -4,15 +4,23 @@ import type { ConfigStore } from '../store.js';
export class BaseProvider {
static id = 'memory';
protected _config: ConfigStore | undefined;
protected _workspace: Workspace | undefined;
protected _config!: ConfigStore;
protected _workspace!: Workspace;
constructor() {
// TODO
// Nothing to do here
}
async init(config: ConfigStore, workspace: Workspace) {
this._config = config;
this._workspace = workspace;
}
async initData() {
throw Error('Not implemented: initData');
}
get workspace() {
return this._workspace;
}
}

View File

@@ -5,7 +5,7 @@ export class MemoryProvider extends BaseProvider {
super();
}
test() {
console.log(this._config, this._workspace);
async initData() {
console.log('Skip data reload in memory provider');
}
}

View File

@@ -1,5 +1,4 @@
import { test, expect } from '@playwright/test';
import { Workspace } from '@blocksuite/store';
import { getDataCenter } from './utils.js';
@@ -9,11 +8,16 @@ test('can init data center', async () => {
const dataCenter = await getDataCenter();
expect(dataCenter).toBeTruthy();
const workspace = await dataCenter.initWorkspace(
'test',
new Workspace({
room: 'test',
})
);
dataCenter.setWorkspaceConfig('test', 'key', 'value');
const workspace = await dataCenter.initWorkspace('test');
expect(workspace).toBeTruthy();
});
test('should init error', async () => {
const dataCenter = await getDataCenter();
test.fail();
await dataCenter.initWorkspace('test', 'not exist provider');
});