mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
feat: memory provider first work
This commit is contained in:
@@ -8,7 +8,6 @@ import type {
|
||||
import type { EditorContainer } from '@blocksuite/editor';
|
||||
export type LoadWorkspaceHandler = (
|
||||
workspaceId: string,
|
||||
websocket?: boolean,
|
||||
user?: AccessTokenMessage | null
|
||||
) => Promise<StoreWorkspace | null> | null;
|
||||
export type CreateEditorHandler = (page: StorePage) => EditorContainer | null;
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
import { useEffect } from 'react';
|
||||
import type { Page } from '@blocksuite/store';
|
||||
import {
|
||||
IndexedDBDocProvider,
|
||||
Workspace as StoreWorkspace,
|
||||
} from '@blocksuite/store';
|
||||
import { Workspace as StoreWorkspace } from '@blocksuite/store';
|
||||
import '@blocksuite/blocks';
|
||||
import { EditorContainer } from '@blocksuite/editor';
|
||||
import { BlockSchema } from '@blocksuite/blocks/models';
|
||||
import type { LoadWorkspaceHandler, CreateEditorHandler } from './context';
|
||||
import { downloadWorkspace, getDataCenter } from '@affine/datacenter';
|
||||
|
||||
interface Props {
|
||||
setLoadWorkspaceHandler: (handler: LoadWorkspaceHandler) => void;
|
||||
@@ -19,18 +17,18 @@ const DynamicBlocksuite = ({
|
||||
setCreateEditorHandler,
|
||||
}: Props) => {
|
||||
useEffect(() => {
|
||||
const openWorkspace: LoadWorkspaceHandler = (
|
||||
workspaceId: string,
|
||||
websocket = false,
|
||||
user
|
||||
) =>
|
||||
const openWorkspace: LoadWorkspaceHandler = (workspaceId: string, user) =>
|
||||
// eslint-disable-next-line no-async-promise-executor
|
||||
new Promise(async resolve => {
|
||||
const workspace = new StoreWorkspace({
|
||||
room: workspaceId,
|
||||
providers: [IndexedDBDocProvider],
|
||||
}).register(BlockSchema);
|
||||
console.log('websocket', websocket);
|
||||
const dc = await getDataCenter();
|
||||
const workspace = await dc.initWorkspace(
|
||||
workspaceId,
|
||||
new StoreWorkspace({
|
||||
room: workspaceId,
|
||||
}).register(BlockSchema)
|
||||
);
|
||||
|
||||
// console.log('websocket', websocket);
|
||||
console.log('user', user);
|
||||
|
||||
// if (websocket && token.refresh) {
|
||||
@@ -58,9 +56,9 @@ const DynamicBlocksuite = ({
|
||||
// workspace.__ws__ = ws;
|
||||
// }
|
||||
|
||||
const indexDBProvider = workspace.providers.find(
|
||||
p => p instanceof IndexedDBDocProvider
|
||||
);
|
||||
// const indexDBProvider = workspace.providers.find(
|
||||
// p => p instanceof IndexedDBDocProvider
|
||||
// );
|
||||
// if (user) {
|
||||
// const updates = await downloadWorkspace({ workspaceId });
|
||||
// updates &&
|
||||
@@ -71,13 +69,14 @@ const DynamicBlocksuite = ({
|
||||
// // if after update, the space:meta is empty, then we need to get map with doc
|
||||
// workspace.doc.getMap('space:meta');
|
||||
// }
|
||||
if (indexDBProvider) {
|
||||
(indexDBProvider as IndexedDBDocProvider).whenSynced.then(() => {
|
||||
resolve(workspace);
|
||||
});
|
||||
} else {
|
||||
resolve(workspace);
|
||||
}
|
||||
|
||||
// if (indexDBProvider) {
|
||||
// (indexDBProvider as IndexedDBDocProvider).whenSynced.then(() => {
|
||||
// resolve(workspace);
|
||||
// });
|
||||
// } else {
|
||||
resolve(workspace);
|
||||
// }
|
||||
});
|
||||
|
||||
setLoadWorkspaceHandler(openWorkspace);
|
||||
|
||||
@@ -41,7 +41,7 @@ export const AppStateProvider = ({ children }: { children?: ReactNode }) => {
|
||||
const workspacesList = await Promise.all(
|
||||
state.workspacesMeta.map(async ({ id }) => {
|
||||
const workspace =
|
||||
(await loadWorkspaceHandler?.(id, false, state.user)) || null;
|
||||
(await loadWorkspaceHandler?.(id, state.user)) || null;
|
||||
return { id, workspace };
|
||||
})
|
||||
);
|
||||
@@ -84,7 +84,7 @@ export const AppStateProvider = ({ children }: { children?: ReactNode }) => {
|
||||
return state.currentWorkspace;
|
||||
}
|
||||
const workspace =
|
||||
(await loadWorkspaceHandler?.(workspaceId, true, state.user)) || null;
|
||||
(await loadWorkspaceHandler?.(workspaceId, state.user)) || null;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-expect-error
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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)!;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,4 +4,8 @@ export class MemoryProvider extends BaseProvider {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
test() {
|
||||
console.log(this._config, this._workspace);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user