feat: memory provider first work

This commit is contained in:
DarkSky
2022-12-29 23:05:19 +08:00
committed by DarkSky
parent 2bf6bf7ed8
commit 6ed2d467b7
10 changed files with 74 additions and 42 deletions

View File

@@ -25,6 +25,7 @@
"typescript": "^4.8.4"
},
"dependencies": {
"@blocksuite/store": "0.3.0-20221228162706-9576a3a",
"encoding": "^0.1.13",
"firebase": "^9.15.0",
"idb-keyval": "^6.2.0",

View File

@@ -1,4 +1,4 @@
import { Doc } from 'yjs';
import { Workspace } from '@blocksuite/store';
import type { BaseProvider } from './provider/index.js';
import { MemoryProvider } from './provider/index.js';
@@ -6,7 +6,7 @@ import { getKVConfigure } from './store.js';
export class DataCenter {
private readonly _providers = new Map<string, typeof BaseProvider>();
private readonly _workspaces = new Map<string, Promise<Doc | undefined>>();
private readonly _workspaces = new Map<string, Promise<Workspace>>();
private readonly _config;
static async init(): Promise<DataCenter> {
@@ -24,25 +24,34 @@ export class DataCenter {
this._providers.set(provider.id, provider);
}
private async _initWorkspace(id: string): Promise<Doc> {
const workspace = new Doc();
const providerId = await this._config.get(`workspace:${id}:provider`);
if (this._providers.has(providerId)) {
private async _initWorkspace(
id: string,
workspace: Workspace,
providerId: string
): Promise<Workspace> {
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();
provider.init(getKVConfigure(id));
await provider.init(getKVConfigure(id), workspace);
}
return workspace;
}
async getWorkspace(id: string): Promise<Doc | undefined> {
async initWorkspace(
id: string,
workspace: Workspace,
provider = 'memory'
): Promise<Workspace> {
if (!this._workspaces.has(id)) {
const workspace = this._initWorkspace(id);
this._workspaces.set(id, workspace);
this._workspaces.set(id, this._initWorkspace(id, workspace, provider));
}
return this._workspaces.get(id);
return this._workspaces.get(id)!;
}
}

View File

@@ -1,14 +1,18 @@
import { Workspace } from '@blocksuite/store';
import type { ConfigStore } from '../store.js';
export class BaseProvider {
static id = 'memory';
protected _config: ConfigStore | undefined;
protected _workspace: Workspace | undefined;
constructor() {
// TODO
}
init(config: ConfigStore) {
async init(config: ConfigStore, workspace: Workspace) {
this._config = config;
this._workspace = workspace;
}
}

View File

@@ -4,4 +4,8 @@ export class MemoryProvider extends BaseProvider {
constructor() {
super();
}
test() {
console.log(this._config, this._workspace);
}
}

View File

@@ -44,4 +44,11 @@ const scopedIndexedDB = () => {
};
};
export const getKVConfigure = scopedIndexedDB();
let lazyKVConfigure: ReturnType<typeof scopedIndexedDB> | undefined = undefined;
export const getKVConfigure = (scope: string) => {
if (!lazyKVConfigure) {
lazyKVConfigure = scopedIndexedDB();
}
return lazyKVConfigure(scope);
};

View File

@@ -1,4 +1,6 @@
import { test, expect } from '@playwright/test';
import { Workspace } from '@blocksuite/store';
import { getDataCenter } from './utils.js';
import 'fake-indexeddb/auto';
@@ -7,6 +9,11 @@ test('can init data center', async () => {
const dataCenter = await getDataCenter();
expect(dataCenter).toBeTruthy();
const workspace = await dataCenter.getWorkspace('test');
const workspace = await dataCenter.initWorkspace(
'test',
new Workspace({
room: 'test',
})
);
expect(workspace).toBeTruthy();
});