mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 20:38:52 +00:00
feat: init workspace & test case
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user