refactor: datacenter only export workspaceUnit

This commit is contained in:
alt0 2023-01-11 15:07:04 +08:00
parent 122cce042c
commit db74706eca
2 changed files with 16 additions and 10 deletions

View File

@ -100,7 +100,8 @@ export class DataCenter {
const workspace = createBlocksuiteWorkspace(workspaceMeta.id);
await this._mainProvider.createWorkspace(workspace, workspaceMeta);
return workspace;
const workspaceUnit = this._workspaceUnitCollection.find(workspaceMeta.id);
return workspaceUnit;
}
/**
@ -154,18 +155,23 @@ export class DataCenter {
* @returns {Promise<BlocksuiteWorkspace>}
*/
public async loadWorkspace(workspaceId: string) {
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
assert(workspaceInfo, 'Workspace not found');
const currentProvider = this.providerMap.get(workspaceInfo.provider);
const workspaceUnit = this._workspaceUnitCollection.find(workspaceId);
assert(workspaceUnit, 'Workspace not found');
const currentProvider = this.providerMap.get(workspaceUnit.provider);
if (currentProvider) {
currentProvider.closeWorkspace(workspaceId);
}
const provider = this.providerMap.get(workspaceInfo.provider);
assert(provider, `provide '${workspaceInfo.provider}' is not registered`);
this._logger(`Loading ${workspaceInfo.provider} workspace: `, workspaceId);
const provider = this.providerMap.get(workspaceUnit.provider);
assert(provider, `provide '${workspaceUnit.provider}' is not registered`);
this._logger(`Loading ${workspaceUnit.provider} workspace: `, workspaceId);
const workspace = this._getBlocksuiteWorkspace(workspaceId);
this._workspaceInstances.set(workspaceId, workspace);
return await provider.warpWorkspace(workspace);
await provider.warpWorkspace(workspace);
this._workspaceUnitCollection.workspaces.forEach(workspaceUnit => {
workspaceUnit.setBlocksuiteWorkspace(null);
});
workspaceUnit.setBlocksuiteWorkspace(workspace);
return workspaceUnit;
}
/**

View File

@ -30,7 +30,7 @@ export class WorkspaceUnit {
public provider!: string;
public syncMode: 'all' | 'core' = 'core';
private _blocksuiteWorkspace?: BlocksuiteWorkspace;
private _blocksuiteWorkspace?: BlocksuiteWorkspace | null;
constructor(params: WorkspaceUnitCtorParams) {
this.id = params.id;
@ -41,7 +41,7 @@ export class WorkspaceUnit {
return this._blocksuiteWorkspace;
}
setBlocksuiteWorkspace(blocksuiteWorkspace: BlocksuiteWorkspace) {
setBlocksuiteWorkspace(blocksuiteWorkspace: BlocksuiteWorkspace | null) {
if (blocksuiteWorkspace?.room !== this.id) {
throw new Error('Workspace id inconsistent.');
}