fix: local provider create workspace should wait sync to idb

This commit is contained in:
alt0 2023-01-10 20:22:14 +08:00
parent b4571539ac
commit b111c411bd
3 changed files with 25 additions and 11 deletions

View File

@ -0,0 +1,20 @@
import assert from 'assert';
import * as idb from 'lib0/indexeddb.js';
import { Workspace as BlocksuiteWorkspace } from '@blocksuite/store';
const { encodeStateAsUpdate } = BlocksuiteWorkspace.Y;
export const initStore = async (blocksuiteWorkspace: BlocksuiteWorkspace) => {
const workspaceId = blocksuiteWorkspace.room;
assert(workspaceId);
await idb.deleteDB(workspaceId);
const db = await idb.openDB(workspaceId, db =>
idb.createStores(db, [['updates', { autoIncrement: true }], ['custom']])
);
const currState = encodeStateAsUpdate(blocksuiteWorkspace.doc);
const [updatesStore] = idb.transact(db, ['updates']); // , 'readonly')
if (updatesStore) {
await idb.addAutoKey(updatesStore, currState);
}
};

View File

@ -7,7 +7,8 @@ import type {
} from '../base';
import { varStorage as storage } from 'lib0/storage';
import { Workspace as BlocksuiteWorkspace, uuidv4 } from '@blocksuite/store';
import { IndexedDBProvider } from './indexeddb.js';
import { IndexedDBProvider } from './indexeddb/indexeddb.js';
import { initStore } from './indexeddb/utils.js';
import assert from 'assert';
import { setDefaultAvatar } from '../utils.js';
@ -101,21 +102,12 @@ export class LocalProvider extends BaseProvider {
): Promise<BlocksuiteWorkspace | undefined> {
const workspaceId = blocksuiteWorkspace.room;
assert(workspaceId, 'Blocksuite Workspace without room(workspaceId).');
assert(meta.name, 'Workspace name is required');
this._logger('Creating affine workspace');
const workspaceInfo: WorkspaceMeta0 = {
name: meta.name,
id: workspaceId,
published: false,
avatar: '',
owner: undefined,
syncMode: 'core',
memberCount: 1,
provider: 'local',
...meta,
};
this.linkLocal(blocksuiteWorkspace);
blocksuiteWorkspace.meta.setName(meta.name);
if (!meta.avatar) {
@ -123,6 +115,8 @@ export class LocalProvider extends BaseProvider {
workspaceInfo.avatar = blocksuiteWorkspace.meta.avatar;
}
await initStore(blocksuiteWorkspace);
this._workspaces.add(workspaceInfo);
this._storeWorkspaces(this._workspaces.list());