feat/add get workspaces

This commit is contained in:
MingLiang Wang 2023-01-08 16:48:28 +08:00
parent 7140d41296
commit 153150cd3b
3 changed files with 35 additions and 19 deletions

View File

@ -44,13 +44,27 @@ export class DataCenter {
return Array.from(this.providerMap.values());
}
/**
* get workspaces list if focusUpdate is true, it will refresh workspaces
* @param {string} name workspace name
* @returns {Promise<WS[]>}
*/
public async getWorkspaces(focusUpdate = false) {
if (focusUpdate) {
this.workspaces.refreshWorkspaces();
}
return this.workspaces.workspaces;
}
/**
* create new workspace , new workspace is a local workspace
* @param {string} name workspace name
* @returns
* @returns {Promise<WS>}
*/
public async createWorkspace(name: string) {
const workspaceInfo = this.workspaces.addLocalWorkspace(name);
// IMP: create workspace in local provider
this.providerMap.get('local')?.createWorkspace({ name });
return workspaceInfo;
}
@ -102,6 +116,7 @@ export class DataCenter {
/**
* load workspace instance by id
* @param {string} workspaceId workspace id
* @returns {Promise<Workspace>}
*/
public async loadWorkspace(workspaceId: string) {
const workspaceInfo = this.workspaces.getWorkspace(workspaceId);
@ -111,13 +126,11 @@ export class DataCenter {
currentProvider.close(workspaceId);
}
const provider = this.providerMap.get(workspaceInfo.provider);
if (provider) {
this._logger(`Loading ${provider} workspace: `, workspaceId);
const workspace = this._getWorkspace(workspaceId);
this.currentWorkspace = await provider.warpWorkspace(workspace);
return this.currentWorkspace;
}
return workspaceInfo;
assert(provider, `provide '${workspaceInfo.provider}' is not registered`);
this._logger(`Loading ${workspaceInfo.provider} workspace: `, workspaceId);
const workspace = this._getWorkspace(workspaceId);
this.currentWorkspace = await provider.warpWorkspace(workspace);
return this.currentWorkspace;
}
/**
@ -127,7 +140,7 @@ export class DataCenter {
public async getUserInfo(providerId = 'affine') {
// XXX: maybe return all user info
const provider = this.providerMap.get(providerId);
assert(provider, 'Provider not found');
assert(provider, `provide '${providerId}' is not registered`);
return provider.getUserInfo();
}
@ -219,28 +232,31 @@ export class DataCenter {
private async _transWorkspaceProvider(
workspace: Workspace,
provider: string
providerId: string
) {
assert(workspace.room, 'No workspace');
assert(workspace.room, 'No workspace id');
const workspaceInfo = this.workspaces.getWorkspace(workspace.room);
assert(workspaceInfo, 'Workspace not found');
if (workspaceInfo.provider === provider) {
if (workspaceInfo.provider === providerId) {
this._logger('Workspace provider is same');
return;
}
const currentProvider = this.providerMap.get(workspaceInfo.provider);
assert(currentProvider, 'Provider not found');
const newProvider = this.providerMap.get(provider);
assert(newProvider, `${provider} provider is not registered`);
const newProvider = this.providerMap.get(providerId);
assert(newProvider, `provide '${providerId}' is not registered`);
this._logger(`create ${providerId} workspace: `, workspaceInfo.name);
const newWorkspace = await newProvider.createWorkspace({
name: workspaceInfo.name,
avatar: workspaceInfo.avatar,
});
assert(newWorkspace, 'Create workspace failed');
// load doc to another workspace
this._logger(
`update workspace data from ${workspaceInfo.provider} to ${providerId}`
);
applyUpdate(newWorkspace.doc, encodeStateAsUpdate(workspace.doc));
assert(newWorkspace, 'Create workspace failed');
currentProvider.delete(workspace.room);
await currentProvider.delete(workspace.room);
this.workspaces.refreshWorkspaces();
}

View File

@ -41,7 +41,7 @@ export class BaseProvider {
}
/**
* warp workspace with workspace functions
* warp workspace with provider functions
* @param workspace
* @returns
*/

View File

@ -1,8 +1,8 @@
import { Workspace as WS } from '../types';
import { Workspace as WS } from 'src/types';
import { Observable } from 'lib0/observable';
import { uuidv4 } from '@blocksuite/store';
import { DataCenter } from '../datacenter';
import { DataCenter } from 'src/datacenter';
export class Workspaces extends Observable<string> {
private _workspaces: WS[];