mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-12-29 03:51:45 +03:00
refactor: add workspace unit
This commit is contained in:
parent
5723117cb8
commit
e13d27ad9c
@ -1,10 +1,14 @@
|
|||||||
import { WorkspaceMetaCollection } from './workspace-meta-collection.js';
|
import { WorkspaceUnitCollection } from './workspace-unit-collection.js';
|
||||||
import type { WorkspaceMetaCollectionChangeEvent } from './workspace-meta-collection';
|
import type { WorkspaceUnitCollectionChangeEvent } from './workspace-unit-collection';
|
||||||
import { Workspace as BlocksuiteWorkspace } from '@blocksuite/store';
|
import { Workspace as BlocksuiteWorkspace } from '@blocksuite/store';
|
||||||
import { BaseProvider } from './provider/base';
|
import type {
|
||||||
|
BaseProvider,
|
||||||
|
CreateWorkspaceInfoParams,
|
||||||
|
UpdateWorkspaceMetaParams,
|
||||||
|
} from './provider/base';
|
||||||
import { LocalProvider } from './provider/local/local';
|
import { LocalProvider } from './provider/local/local';
|
||||||
import { AffineProvider } from './provider';
|
import { AffineProvider } from './provider';
|
||||||
import type { Message, WorkspaceMeta } from './types';
|
import type { Message } from './types';
|
||||||
import assert from 'assert';
|
import assert from 'assert';
|
||||||
import { getLogger } from './logger';
|
import { getLogger } from './logger';
|
||||||
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
|
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
|
||||||
@ -16,7 +20,7 @@ import { MessageCenter } from './message';
|
|||||||
* @classdesc Data center is made for managing different providers for business
|
* @classdesc Data center is made for managing different providers for business
|
||||||
*/
|
*/
|
||||||
export class DataCenter {
|
export class DataCenter {
|
||||||
private readonly _workspaceMetaCollection = new WorkspaceMetaCollection();
|
private readonly _workspaceUnitCollection = new WorkspaceUnitCollection();
|
||||||
private readonly _logger = getLogger('dc');
|
private readonly _logger = getLogger('dc');
|
||||||
private _workspaceInstances: Map<string, BlocksuiteWorkspace> = new Map();
|
private _workspaceInstances: Map<string, BlocksuiteWorkspace> = new Map();
|
||||||
private _messageCenter = new MessageCenter();
|
private _messageCenter = new MessageCenter();
|
||||||
@ -35,7 +39,7 @@ export class DataCenter {
|
|||||||
const getInitParams = () => {
|
const getInitParams = () => {
|
||||||
return {
|
return {
|
||||||
logger: dc._logger,
|
logger: dc._logger,
|
||||||
workspaces: dc._workspaceMetaCollection.createScope(),
|
workspaces: dc._workspaceUnitCollection.createScope(),
|
||||||
messageCenter: dc._messageCenter,
|
messageCenter: dc._messageCenter,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -68,7 +72,7 @@ export class DataCenter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public get workspaces() {
|
public get workspaces() {
|
||||||
return this._workspaceMetaCollection.workspaces;
|
return this._workspaceUnitCollection.workspaces;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async refreshWorkspaces() {
|
public async refreshWorkspaces() {
|
||||||
@ -82,17 +86,15 @@ export class DataCenter {
|
|||||||
* @param {string} name workspace name
|
* @param {string} name workspace name
|
||||||
* @returns {Promise<Workspace>}
|
* @returns {Promise<Workspace>}
|
||||||
*/
|
*/
|
||||||
public async createWorkspace(workspaceMeta: WorkspaceMeta) {
|
public async createWorkspace(params: CreateWorkspaceInfoParams) {
|
||||||
assert(
|
assert(
|
||||||
this._mainProvider,
|
this._mainProvider,
|
||||||
'There is no provider. You should add provider first.'
|
'There is no provider. You should add provider first.'
|
||||||
);
|
);
|
||||||
|
|
||||||
const workspaceInfo = await this._mainProvider.createWorkspaceInfo(
|
const workspaceMeta = await this._mainProvider.createWorkspaceInfo(params);
|
||||||
workspaceMeta
|
|
||||||
);
|
|
||||||
|
|
||||||
const workspace = createBlocksuiteWorkspace(workspaceInfo.id);
|
const workspace = createBlocksuiteWorkspace(workspaceMeta.id);
|
||||||
|
|
||||||
await this._mainProvider.createWorkspace(workspace, workspaceMeta);
|
await this._mainProvider.createWorkspace(workspace, workspaceMeta);
|
||||||
return workspace;
|
return workspace;
|
||||||
@ -103,7 +105,7 @@ export class DataCenter {
|
|||||||
* @param {string} workspaceId workspace id
|
* @param {string} workspaceId workspace id
|
||||||
*/
|
*/
|
||||||
public async deleteWorkspace(workspaceId: string) {
|
public async deleteWorkspace(workspaceId: string) {
|
||||||
const workspaceInfo = this._workspaceMetaCollection.find(workspaceId);
|
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
|
||||||
assert(workspaceInfo, 'Workspace not found');
|
assert(workspaceInfo, 'Workspace not found');
|
||||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||||
assert(provider, `Workspace exists, but we couldn't find its provider.`);
|
assert(provider, `Workspace exists, but we couldn't find its provider.`);
|
||||||
@ -115,7 +117,7 @@ export class DataCenter {
|
|||||||
* @param {string} workspaceId workspace id
|
* @param {string} workspaceId workspace id
|
||||||
*/
|
*/
|
||||||
private _getBlocksuiteWorkspace(workspaceId: string) {
|
private _getBlocksuiteWorkspace(workspaceId: string) {
|
||||||
const workspaceInfo = this._workspaceMetaCollection.find(workspaceId);
|
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
|
||||||
assert(workspaceInfo, 'Workspace not found');
|
assert(workspaceInfo, 'Workspace not found');
|
||||||
return (
|
return (
|
||||||
this._workspaceInstances.get(workspaceId) ||
|
this._workspaceInstances.get(workspaceId) ||
|
||||||
@ -149,7 +151,7 @@ export class DataCenter {
|
|||||||
* @returns {Promise<BlocksuiteWorkspace>}
|
* @returns {Promise<BlocksuiteWorkspace>}
|
||||||
*/
|
*/
|
||||||
public async loadWorkspace(workspaceId: string) {
|
public async loadWorkspace(workspaceId: string) {
|
||||||
const workspaceInfo = this._workspaceMetaCollection.find(workspaceId);
|
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
|
||||||
assert(workspaceInfo, 'Workspace not found');
|
assert(workspaceInfo, 'Workspace not found');
|
||||||
const currentProvider = this.providerMap.get(workspaceInfo.provider);
|
const currentProvider = this.providerMap.get(workspaceInfo.provider);
|
||||||
if (currentProvider) {
|
if (currentProvider) {
|
||||||
@ -180,9 +182,9 @@ export class DataCenter {
|
|||||||
* @param {Function} callback callback function
|
* @param {Function} callback callback function
|
||||||
*/
|
*/
|
||||||
public async onWorkspacesChange(
|
public async onWorkspacesChange(
|
||||||
callback: (workspaces: WorkspaceMetaCollectionChangeEvent) => void
|
callback: (workspaces: WorkspaceUnitCollectionChangeEvent) => void
|
||||||
) {
|
) {
|
||||||
this._workspaceMetaCollection.on('change', callback);
|
this._workspaceUnitCollection.on('change', callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -191,11 +193,11 @@ export class DataCenter {
|
|||||||
* @param {BlocksuiteWorkspace} workspace workspace instance
|
* @param {BlocksuiteWorkspace} workspace workspace instance
|
||||||
*/
|
*/
|
||||||
public async updateWorkspaceMeta(
|
public async updateWorkspaceMeta(
|
||||||
{ name, avatar }: Partial<WorkspaceMeta>,
|
{ name, avatar }: UpdateWorkspaceMetaParams,
|
||||||
workspace: BlocksuiteWorkspace
|
workspace: BlocksuiteWorkspace
|
||||||
) {
|
) {
|
||||||
assert(workspace?.room, 'No workspace to set meta');
|
assert(workspace?.room, 'No workspace to set meta');
|
||||||
const update: Partial<WorkspaceMeta> = {};
|
const update: Partial<UpdateWorkspaceMetaParams> = {};
|
||||||
if (name) {
|
if (name) {
|
||||||
workspace.meta.setName(name);
|
workspace.meta.setName(name);
|
||||||
update.name = name;
|
update.name = name;
|
||||||
@ -205,7 +207,7 @@ export class DataCenter {
|
|||||||
update.avatar = avatar;
|
update.avatar = avatar;
|
||||||
}
|
}
|
||||||
// may run for change workspace meta
|
// may run for change workspace meta
|
||||||
const workspaceInfo = this._workspaceMetaCollection.find(workspace.room);
|
const workspaceInfo = this._workspaceUnitCollection.find(workspace.room);
|
||||||
assert(workspaceInfo, 'Workspace not found');
|
assert(workspaceInfo, 'Workspace not found');
|
||||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||||
provider?.updateWorkspaceMeta(workspace.room, update);
|
provider?.updateWorkspaceMeta(workspace.room, update);
|
||||||
@ -217,7 +219,7 @@ export class DataCenter {
|
|||||||
* @param id workspace id
|
* @param id workspace id
|
||||||
*/
|
*/
|
||||||
public async leaveWorkspace(workspaceId: string) {
|
public async leaveWorkspace(workspaceId: string) {
|
||||||
const workspaceInfo = this._workspaceMetaCollection.find(workspaceId);
|
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
|
||||||
assert(workspaceInfo, 'Workspace not found');
|
assert(workspaceInfo, 'Workspace not found');
|
||||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||||
if (provider) {
|
if (provider) {
|
||||||
@ -227,7 +229,7 @@ export class DataCenter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async setWorkspacePublish(workspaceId: string, isPublish: boolean) {
|
public async setWorkspacePublish(workspaceId: string, isPublish: boolean) {
|
||||||
const workspaceInfo = this._workspaceMetaCollection.find(workspaceId);
|
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
|
||||||
assert(workspaceInfo, 'Workspace not found');
|
assert(workspaceInfo, 'Workspace not found');
|
||||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||||
if (provider) {
|
if (provider) {
|
||||||
@ -236,7 +238,7 @@ export class DataCenter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async inviteMember(id: string, email: string) {
|
public async inviteMember(id: string, email: string) {
|
||||||
const workspaceInfo = this._workspaceMetaCollection.find(id);
|
const workspaceInfo = this._workspaceUnitCollection.find(id);
|
||||||
assert(workspaceInfo, 'Workspace not found');
|
assert(workspaceInfo, 'Workspace not found');
|
||||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||||
if (provider) {
|
if (provider) {
|
||||||
@ -249,7 +251,7 @@ export class DataCenter {
|
|||||||
* @param {number} permissionId permission id
|
* @param {number} permissionId permission id
|
||||||
*/
|
*/
|
||||||
public async removeMember(workspaceId: string, permissionId: number) {
|
public async removeMember(workspaceId: string, permissionId: number) {
|
||||||
const workspaceInfo = this._workspaceMetaCollection.find(workspaceId);
|
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
|
||||||
assert(workspaceInfo, 'Workspace not found');
|
assert(workspaceInfo, 'Workspace not found');
|
||||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||||
if (provider) {
|
if (provider) {
|
||||||
@ -280,7 +282,7 @@ export class DataCenter {
|
|||||||
providerId: string
|
providerId: string
|
||||||
) {
|
) {
|
||||||
assert(workspace.room, 'No workspace id');
|
assert(workspace.room, 'No workspace id');
|
||||||
const workspaceInfo = this._workspaceMetaCollection.find(workspace.room);
|
const workspaceInfo = this._workspaceUnitCollection.find(workspace.room);
|
||||||
assert(workspaceInfo, 'Workspace not found');
|
assert(workspaceInfo, 'Workspace not found');
|
||||||
if (workspaceInfo.provider === providerId) {
|
if (workspaceInfo.provider === providerId) {
|
||||||
this._logger('Workspace provider is same');
|
this._logger('Workspace provider is same');
|
||||||
@ -293,11 +295,12 @@ export class DataCenter {
|
|||||||
this._logger(`create ${providerId} workspace: `, workspaceInfo.name);
|
this._logger(`create ${providerId} workspace: `, workspaceInfo.name);
|
||||||
const newWorkspaceInfo = await newProvider.createWorkspaceInfo({
|
const newWorkspaceInfo = await newProvider.createWorkspaceInfo({
|
||||||
name: workspaceInfo.name,
|
name: workspaceInfo.name,
|
||||||
avatar: workspaceInfo.avatar,
|
// avatar: workspaceInfo.avatar,
|
||||||
});
|
});
|
||||||
const newWorkspace = createBlocksuiteWorkspace(newWorkspaceInfo.id);
|
const newWorkspace = createBlocksuiteWorkspace(newWorkspaceInfo.id);
|
||||||
// TODO optimize this function
|
// TODO optimize this function
|
||||||
await newProvider.createWorkspace(newWorkspace, {
|
await newProvider.createWorkspace(newWorkspace, {
|
||||||
|
...newWorkspaceInfo,
|
||||||
name: workspaceInfo.name,
|
name: workspaceInfo.name,
|
||||||
avatar: workspaceInfo.avatar,
|
avatar: workspaceInfo.avatar,
|
||||||
});
|
});
|
||||||
|
@ -26,6 +26,6 @@ const _initializeDataCenter = () => {
|
|||||||
export const getDataCenter = _initializeDataCenter();
|
export const getDataCenter = _initializeDataCenter();
|
||||||
|
|
||||||
export type { AccessTokenMessage } from './provider/affine/apis';
|
export type { AccessTokenMessage } from './provider/affine/apis';
|
||||||
export type { WorkspaceInfo } from './types';
|
export { WorkspaceUnit } from './workspace-unit';
|
||||||
export { getLogger } from './logger';
|
export { getLogger } from './logger';
|
||||||
export * from './message';
|
export * from './message';
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
export { MessageCenter } from './message';
|
export { MessageCenter } from './message.js';
|
||||||
export { MessageCode } from './code';
|
export { MessageCode } from './code.js';
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Observable } from 'lib0/observable';
|
import { Observable } from 'lib0/observable';
|
||||||
import { Message } from '../types';
|
import { Message } from '../types';
|
||||||
import { MessageCode } from './code';
|
import { MessageCode } from './code.js';
|
||||||
|
|
||||||
export class MessageCenter extends Observable<string> {
|
export class MessageCenter extends Observable<string> {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
import { BaseProvider } from '../base.js';
|
import { BaseProvider } from '../base.js';
|
||||||
import type { ProviderConstructorParams } from '../base';
|
import type {
|
||||||
import type { User, WorkspaceInfo, WorkspaceMeta } from '../../types';
|
ProviderConstructorParams,
|
||||||
|
CreateWorkspaceInfoParams,
|
||||||
|
UpdateWorkspaceMetaParams,
|
||||||
|
WorkspaceMeta0,
|
||||||
|
} from '../base';
|
||||||
|
import type { User } from '../../types';
|
||||||
import { Workspace as BlocksuiteWorkspace } from '@blocksuite/store';
|
import { Workspace as BlocksuiteWorkspace } from '@blocksuite/store';
|
||||||
import { BlockSchema } from '@blocksuite/blocks/models';
|
import { BlockSchema } from '@blocksuite/blocks/models';
|
||||||
import { applyUpdate } from 'yjs';
|
import { applyUpdate } from 'yjs';
|
||||||
@ -115,12 +120,13 @@ export class AffineProvider extends BaseProvider {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
const workspacesList = await this._apis.getWorkspaces();
|
const workspacesList = await this._apis.getWorkspaces();
|
||||||
const workspaces: WorkspaceInfo[] = workspacesList.map(w => {
|
const workspaces: WorkspaceMeta0[] = workspacesList.map(w => {
|
||||||
return {
|
return {
|
||||||
...w,
|
...w,
|
||||||
memberCount: 0,
|
memberCount: 0,
|
||||||
name: '',
|
name: '',
|
||||||
provider: 'affine',
|
provider: 'affine',
|
||||||
|
syncMode: 'core',
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
const workspaceInstances = workspaces.map(({ id }) => {
|
const workspaceInstances = workspaces.map(({ id }) => {
|
||||||
@ -268,19 +274,17 @@ export class AffineProvider extends BaseProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override async createWorkspaceInfo(
|
public override async createWorkspaceInfo(
|
||||||
meta: WorkspaceMeta
|
meta: CreateWorkspaceInfoParams
|
||||||
): Promise<WorkspaceInfo> {
|
): Promise<WorkspaceMeta0> {
|
||||||
const { id } = await this._apis.createWorkspace(
|
const { id } = await this._apis.createWorkspace(meta);
|
||||||
meta as Required<WorkspaceMeta>
|
|
||||||
);
|
|
||||||
|
|
||||||
const workspaceInfo: WorkspaceInfo = {
|
const workspaceInfo: WorkspaceMeta0 = {
|
||||||
name: meta.name,
|
name: meta.name,
|
||||||
id: id,
|
id: id,
|
||||||
isPublish: false,
|
published: false,
|
||||||
avatar: '',
|
avatar: '',
|
||||||
owner: await this.getUserInfo(),
|
owner: await this.getUserInfo(),
|
||||||
isLocal: true,
|
syncMode: 'core',
|
||||||
memberCount: 1,
|
memberCount: 1,
|
||||||
provider: 'affine',
|
provider: 'affine',
|
||||||
};
|
};
|
||||||
@ -289,7 +293,7 @@ export class AffineProvider extends BaseProvider {
|
|||||||
|
|
||||||
public override async createWorkspace(
|
public override async createWorkspace(
|
||||||
blocksuiteWorkspace: BlocksuiteWorkspace,
|
blocksuiteWorkspace: BlocksuiteWorkspace,
|
||||||
meta: WorkspaceMeta
|
meta: WorkspaceMeta0
|
||||||
): Promise<BlocksuiteWorkspace | undefined> {
|
): Promise<BlocksuiteWorkspace | undefined> {
|
||||||
const workspaceId = blocksuiteWorkspace.room;
|
const workspaceId = blocksuiteWorkspace.room;
|
||||||
assert(workspaceId, 'Blocksuite Workspace without room(workspaceId).');
|
assert(workspaceId, 'Blocksuite Workspace without room(workspaceId).');
|
||||||
@ -298,13 +302,13 @@ export class AffineProvider extends BaseProvider {
|
|||||||
this._applyCloudUpdates(blocksuiteWorkspace);
|
this._applyCloudUpdates(blocksuiteWorkspace);
|
||||||
this.linkLocal(blocksuiteWorkspace);
|
this.linkLocal(blocksuiteWorkspace);
|
||||||
|
|
||||||
const workspaceInfo: WorkspaceInfo = {
|
const workspaceInfo: WorkspaceMeta0 = {
|
||||||
name: meta.name,
|
name: meta.name,
|
||||||
id: workspaceId,
|
id: workspaceId,
|
||||||
isPublish: false,
|
published: false,
|
||||||
avatar: '',
|
avatar: '',
|
||||||
owner: undefined,
|
owner: undefined,
|
||||||
isLocal: true,
|
syncMode: 'core',
|
||||||
memberCount: 1,
|
memberCount: 1,
|
||||||
provider: 'affine',
|
provider: 'affine',
|
||||||
};
|
};
|
||||||
|
@ -79,7 +79,6 @@ export async function getWorkspaceMembers(
|
|||||||
|
|
||||||
export interface CreateWorkspaceParams {
|
export interface CreateWorkspaceParams {
|
||||||
name: string;
|
name: string;
|
||||||
avatar: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createWorkspace(
|
export async function createWorkspace(
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import { Workspace as BlocksuiteWorkspace, uuidv4 } from '@blocksuite/store';
|
import { Workspace as BlocksuiteWorkspace, uuidv4 } from '@blocksuite/store';
|
||||||
import { MessageCenter } from '../message';
|
import { MessageCenter } from '../message';
|
||||||
import { Logger, User, WorkspaceInfo, WorkspaceMeta } from '../types';
|
import { Logger, User } from '../types';
|
||||||
import type { WorkspaceMetaCollectionScope } from '../workspace-meta-collection';
|
import type { WorkspaceUnitCollectionScope } from '../workspace-unit-collection';
|
||||||
|
import type { WorkspaceUnitCtorParams } from '../workspace-unit';
|
||||||
|
|
||||||
const defaultLogger = () => {
|
const defaultLogger = () => {
|
||||||
return;
|
return;
|
||||||
@ -9,13 +10,19 @@ const defaultLogger = () => {
|
|||||||
|
|
||||||
export interface ProviderConstructorParams {
|
export interface ProviderConstructorParams {
|
||||||
logger?: Logger;
|
logger?: Logger;
|
||||||
workspaces: WorkspaceMetaCollectionScope;
|
workspaces: WorkspaceUnitCollectionScope;
|
||||||
messageCenter: MessageCenter;
|
messageCenter: MessageCenter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type WorkspaceMeta0 = WorkspaceUnitCtorParams;
|
||||||
|
export type CreateWorkspaceInfoParams = Pick<WorkspaceUnitCtorParams, 'name'>;
|
||||||
|
export type UpdateWorkspaceMetaParams = Partial<
|
||||||
|
Pick<WorkspaceUnitCtorParams, 'name' | 'avatar'>
|
||||||
|
>;
|
||||||
|
|
||||||
export class BaseProvider {
|
export class BaseProvider {
|
||||||
public readonly id: string = 'base';
|
public readonly id: string = 'base';
|
||||||
protected _workspaces!: WorkspaceMetaCollectionScope;
|
protected _workspaces!: WorkspaceUnitCollectionScope;
|
||||||
protected _logger!: Logger;
|
protected _logger!: Logger;
|
||||||
protected _messageCenter!: MessageCenter;
|
protected _messageCenter!: MessageCenter;
|
||||||
|
|
||||||
@ -37,8 +44,8 @@ export class BaseProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async createWorkspaceInfo(
|
public async createWorkspaceInfo(
|
||||||
meta: WorkspaceMeta
|
params: CreateWorkspaceInfoParams
|
||||||
): Promise<WorkspaceInfo> {
|
): Promise<WorkspaceMeta0> {
|
||||||
throw new Error(`provider: ${this.id} createWorkspaceInfo Not implemented`);
|
throw new Error(`provider: ${this.id} createWorkspaceInfo Not implemented`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +77,7 @@ export class BaseProvider {
|
|||||||
/**
|
/**
|
||||||
* load workspaces
|
* load workspaces
|
||||||
**/
|
**/
|
||||||
public async loadWorkspaces(): Promise<WorkspaceInfo[]> {
|
public async loadWorkspaces(): Promise<WorkspaceMeta0[]> {
|
||||||
throw new Error(`provider: ${this.id} loadWorkSpace Not implemented`);
|
throw new Error(`provider: ${this.id} loadWorkSpace Not implemented`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,10 +164,10 @@ export class BaseProvider {
|
|||||||
*/
|
*/
|
||||||
public async updateWorkspaceMeta(
|
public async updateWorkspaceMeta(
|
||||||
id: string,
|
id: string,
|
||||||
meta: Partial<WorkspaceMeta>
|
params: UpdateWorkspaceMetaParams
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
id;
|
id;
|
||||||
meta;
|
params;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,7 +177,7 @@ export class BaseProvider {
|
|||||||
*/
|
*/
|
||||||
public async createWorkspace(
|
public async createWorkspace(
|
||||||
blocksuiteWorkspace: BlocksuiteWorkspace,
|
blocksuiteWorkspace: BlocksuiteWorkspace,
|
||||||
meta: WorkspaceMeta
|
meta: WorkspaceMeta0
|
||||||
): Promise<BlocksuiteWorkspace | undefined> {
|
): Promise<BlocksuiteWorkspace | undefined> {
|
||||||
return blocksuiteWorkspace;
|
return blocksuiteWorkspace;
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
import { test, expect } from '@playwright/test';
|
import { test, expect } from '@playwright/test';
|
||||||
import { WorkspaceMetaCollection } from '../../workspace-meta-collection.js';
|
import { WorkspaceUnitCollection } from '../../workspace-unit-collection.js';
|
||||||
import { LocalProvider } from './local.js';
|
import { LocalProvider } from './local.js';
|
||||||
import { createBlocksuiteWorkspace } from '../../utils/index.js';
|
import { createBlocksuiteWorkspace } from '../../utils/index.js';
|
||||||
|
import { MessageCenter } from '../../message/index.js';
|
||||||
import 'fake-indexeddb/auto';
|
import 'fake-indexeddb/auto';
|
||||||
|
|
||||||
test.describe.serial('local provider', () => {
|
test.describe.serial('local provider', () => {
|
||||||
const workspaceMetaCollection = new WorkspaceMetaCollection();
|
const workspaceMetaCollection = new WorkspaceUnitCollection();
|
||||||
const provider = new LocalProvider({
|
const provider = new LocalProvider({
|
||||||
workspaces: workspaceMetaCollection.createScope(),
|
workspaces: workspaceMetaCollection.createScope(),
|
||||||
|
messageCenter: new MessageCenter(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const workspaceName = 'workspace-test';
|
const workspaceName = 'workspace-test';
|
||||||
@ -16,23 +18,20 @@ test.describe.serial('local provider', () => {
|
|||||||
test('create workspace', async () => {
|
test('create workspace', async () => {
|
||||||
const workspaceInfo = await provider.createWorkspaceInfo({
|
const workspaceInfo = await provider.createWorkspaceInfo({
|
||||||
name: workspaceName,
|
name: workspaceName,
|
||||||
avatar: 'avatar-url-test',
|
|
||||||
});
|
});
|
||||||
workspaceId = workspaceInfo.id;
|
workspaceId = workspaceInfo.id;
|
||||||
const blocksuiteWorkspace = createBlocksuiteWorkspace(workspaceId);
|
const blocksuiteWorkspace = createBlocksuiteWorkspace(workspaceId);
|
||||||
await provider.createWorkspace(blocksuiteWorkspace, {
|
await provider.createWorkspace(blocksuiteWorkspace, workspaceInfo);
|
||||||
name: workspaceName,
|
|
||||||
avatar: 'avatar-url-test',
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(workspaceMetaCollection.workspaces.length).toEqual(1);
|
expect(workspaceMetaCollection.workspaces.length).toEqual(1);
|
||||||
expect(workspaceMetaCollection.workspaces[0].name).toEqual(workspaceName);
|
expect(workspaceMetaCollection.workspaces[0].name).toEqual(workspaceName);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('workspace list cache', async () => {
|
test('workspace list cache', async () => {
|
||||||
const workspacesMetaCollection1 = new WorkspaceMetaCollection();
|
const workspacesMetaCollection1 = new WorkspaceUnitCollection();
|
||||||
const provider1 = new LocalProvider({
|
const provider1 = new LocalProvider({
|
||||||
workspaces: workspacesMetaCollection1.createScope(),
|
workspaces: workspacesMetaCollection1.createScope(),
|
||||||
|
messageCenter: new MessageCenter(),
|
||||||
});
|
});
|
||||||
await provider1.loadWorkspaces();
|
await provider1.loadWorkspaces();
|
||||||
expect(workspacesMetaCollection1.workspaces.length).toEqual(1);
|
expect(workspacesMetaCollection1.workspaces.length).toEqual(1);
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
import { BaseProvider } from '../base.js';
|
import { BaseProvider } from '../base.js';
|
||||||
import type { ProviderConstructorParams } from '../base';
|
import type {
|
||||||
|
ProviderConstructorParams,
|
||||||
|
WorkspaceMeta0,
|
||||||
|
UpdateWorkspaceMetaParams,
|
||||||
|
CreateWorkspaceInfoParams,
|
||||||
|
} from '../base';
|
||||||
import { varStorage as storage } from 'lib0/storage';
|
import { varStorage as storage } from 'lib0/storage';
|
||||||
import { WorkspaceInfo, WorkspaceMeta } from '../../types';
|
|
||||||
import { Workspace as BlocksuiteWorkspace, uuidv4 } from '@blocksuite/store';
|
import { Workspace as BlocksuiteWorkspace, uuidv4 } from '@blocksuite/store';
|
||||||
import { IndexedDBProvider } from './indexeddb.js';
|
import { IndexedDBProvider } from './indexeddb.js';
|
||||||
import assert from 'assert';
|
import assert from 'assert';
|
||||||
@ -18,7 +22,7 @@ export class LocalProvider extends BaseProvider {
|
|||||||
this.loadWorkspaces();
|
this.loadWorkspaces();
|
||||||
}
|
}
|
||||||
|
|
||||||
private _storeWorkspaces(workspaces: WorkspaceInfo[]) {
|
private _storeWorkspaces(workspaces: WorkspaceMeta0[]) {
|
||||||
storage.setItem(WORKSPACE_KEY, JSON.stringify(workspaces));
|
storage.setItem(WORKSPACE_KEY, JSON.stringify(workspaces));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,12 +44,12 @@ export class LocalProvider extends BaseProvider {
|
|||||||
return workspace;
|
return workspace;
|
||||||
}
|
}
|
||||||
|
|
||||||
override loadWorkspaces(): Promise<WorkspaceInfo[]> {
|
override loadWorkspaces(): Promise<WorkspaceMeta0[]> {
|
||||||
const workspaceStr = storage.getItem(WORKSPACE_KEY);
|
const workspaceStr = storage.getItem(WORKSPACE_KEY);
|
||||||
let workspaces: WorkspaceInfo[] = [];
|
let workspaces: WorkspaceMeta0[] = [];
|
||||||
if (workspaceStr) {
|
if (workspaceStr) {
|
||||||
try {
|
try {
|
||||||
workspaces = JSON.parse(workspaceStr) as WorkspaceInfo[];
|
workspaces = JSON.parse(workspaceStr) as WorkspaceMeta0[];
|
||||||
workspaces.forEach(workspace => {
|
workspaces.forEach(workspace => {
|
||||||
this._workspaces.add(workspace);
|
this._workspaces.add(workspace);
|
||||||
});
|
});
|
||||||
@ -69,22 +73,22 @@ export class LocalProvider extends BaseProvider {
|
|||||||
|
|
||||||
public override async updateWorkspaceMeta(
|
public override async updateWorkspaceMeta(
|
||||||
id: string,
|
id: string,
|
||||||
meta: Partial<WorkspaceMeta>
|
meta: UpdateWorkspaceMetaParams
|
||||||
) {
|
) {
|
||||||
this._workspaces.update(id, meta);
|
this._workspaces.update(id, meta);
|
||||||
this._storeWorkspaces(this._workspaces.list());
|
this._storeWorkspaces(this._workspaces.list());
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async createWorkspaceInfo(
|
public override async createWorkspaceInfo(
|
||||||
meta: WorkspaceMeta
|
meta: CreateWorkspaceInfoParams
|
||||||
): Promise<WorkspaceInfo> {
|
): Promise<WorkspaceMeta0> {
|
||||||
const workspaceInfo: WorkspaceInfo = {
|
const workspaceInfo: WorkspaceMeta0 = {
|
||||||
name: meta.name,
|
name: meta.name,
|
||||||
id: uuidv4(),
|
id: uuidv4(),
|
||||||
isPublish: false,
|
published: false,
|
||||||
avatar: '',
|
avatar: '',
|
||||||
owner: undefined,
|
owner: undefined,
|
||||||
isLocal: true,
|
syncMode: 'core',
|
||||||
memberCount: 1,
|
memberCount: 1,
|
||||||
provider: 'local',
|
provider: 'local',
|
||||||
};
|
};
|
||||||
@ -93,20 +97,20 @@ export class LocalProvider extends BaseProvider {
|
|||||||
|
|
||||||
public override async createWorkspace(
|
public override async createWorkspace(
|
||||||
blocksuiteWorkspace: BlocksuiteWorkspace,
|
blocksuiteWorkspace: BlocksuiteWorkspace,
|
||||||
meta: WorkspaceMeta
|
meta: WorkspaceMeta0
|
||||||
): Promise<BlocksuiteWorkspace | undefined> {
|
): Promise<BlocksuiteWorkspace | undefined> {
|
||||||
const workspaceId = blocksuiteWorkspace.room;
|
const workspaceId = blocksuiteWorkspace.room;
|
||||||
assert(workspaceId, 'Blocksuite Workspace without room(workspaceId).');
|
assert(workspaceId, 'Blocksuite Workspace without room(workspaceId).');
|
||||||
assert(meta.name, 'Workspace name is required');
|
assert(meta.name, 'Workspace name is required');
|
||||||
this._logger('Creating affine workspace');
|
this._logger('Creating affine workspace');
|
||||||
|
|
||||||
const workspaceInfo: WorkspaceInfo = {
|
const workspaceInfo: WorkspaceMeta0 = {
|
||||||
name: meta.name,
|
name: meta.name,
|
||||||
id: workspaceId,
|
id: workspaceId,
|
||||||
isPublish: false,
|
published: false,
|
||||||
avatar: '',
|
avatar: '',
|
||||||
owner: undefined,
|
owner: undefined,
|
||||||
isLocal: true,
|
syncMode: 'core',
|
||||||
memberCount: 1,
|
memberCount: 1,
|
||||||
provider: 'local',
|
provider: 'local',
|
||||||
};
|
};
|
||||||
|
@ -5,6 +5,9 @@ import { getDefaultHeadImgBlob } from '../utils/index.js';
|
|||||||
export const setDefaultAvatar = async (
|
export const setDefaultAvatar = async (
|
||||||
blocksuiteWorkspace: BlocksuiteWorkspace
|
blocksuiteWorkspace: BlocksuiteWorkspace
|
||||||
) => {
|
) => {
|
||||||
|
if (typeof document === 'undefined') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const blob = await getDefaultHeadImgBlob(blocksuiteWorkspace.meta.name);
|
const blob = await getDefaultHeadImgBlob(blocksuiteWorkspace.meta.name);
|
||||||
const blobStorage = await blocksuiteWorkspace.blobs;
|
const blobStorage = await blocksuiteWorkspace.blobs;
|
||||||
assert(blobStorage, 'No blob storage');
|
assert(blobStorage, 'No blob storage');
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
import { getLogger } from '../logger';
|
import { getLogger } from '../logger';
|
||||||
|
|
||||||
export type WorkspaceInfo = {
|
// export type WorkspaceInfo = {
|
||||||
name: string;
|
// name: string;
|
||||||
id: string;
|
// id: string;
|
||||||
isPublish?: boolean;
|
// isPublish?: boolean;
|
||||||
avatar?: string;
|
// avatar?: string;
|
||||||
owner?: User;
|
// owner?: User;
|
||||||
isLocal?: boolean;
|
// isLocal?: boolean;
|
||||||
memberCount: number;
|
// memberCount: number;
|
||||||
provider: string;
|
// provider: string;
|
||||||
};
|
// };
|
||||||
|
|
||||||
export type User = {
|
export type User = {
|
||||||
name: string;
|
name: string;
|
||||||
@ -18,7 +18,7 @@ export type User = {
|
|||||||
avatar: string;
|
avatar: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type WorkspaceMeta = Pick<WorkspaceInfo, 'name' | 'avatar'>;
|
// export type WorkspaceMeta = Pick<WorkspaceInfo, 'name' | 'avatar'>;
|
||||||
|
|
||||||
export type Logger = ReturnType<typeof getLogger>;
|
export type Logger = ReturnType<typeof getLogger>;
|
||||||
|
|
||||||
|
@ -1,50 +0,0 @@
|
|||||||
import { test, expect } from '@playwright/test';
|
|
||||||
import { WorkspaceMetaCollection } from './workspace-meta-collection.js';
|
|
||||||
import type { WorkspaceMetaCollectionChangeEvent } from './workspace-meta-collection';
|
|
||||||
|
|
||||||
test.describe.serial('workspace meta collection observable', () => {
|
|
||||||
const workspaces = new WorkspaceMetaCollection();
|
|
||||||
|
|
||||||
const scope = workspaces.createScope();
|
|
||||||
|
|
||||||
test('add workspace', () => {
|
|
||||||
workspaces.once('change', (event: WorkspaceMetaCollectionChangeEvent) => {
|
|
||||||
expect(event.added?.id).toEqual('123');
|
|
||||||
});
|
|
||||||
scope.add({
|
|
||||||
id: '123',
|
|
||||||
name: 'test',
|
|
||||||
memberCount: 1,
|
|
||||||
provider: '',
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('list workspace', () => {
|
|
||||||
const list = scope.list();
|
|
||||||
expect(list.length).toEqual(1);
|
|
||||||
expect(list[0].id).toEqual('123');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('get workspace', () => {
|
|
||||||
expect(scope.get('123')?.id).toEqual('123');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('update workspace', () => {
|
|
||||||
workspaces.once('change', (event: WorkspaceMetaCollectionChangeEvent) => {
|
|
||||||
expect(event.updated?.name).toEqual('demo');
|
|
||||||
});
|
|
||||||
scope.update('123', { name: 'demo' });
|
|
||||||
});
|
|
||||||
|
|
||||||
test('get workspace form other scope', () => {
|
|
||||||
const scope1 = workspaces.createScope();
|
|
||||||
expect(scope1.get('123')).toBeFalsy();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('delete workspace', () => {
|
|
||||||
workspaces.once('change', (event: WorkspaceMetaCollectionChangeEvent) => {
|
|
||||||
expect(event.deleted?.id).toEqual('123');
|
|
||||||
});
|
|
||||||
scope.remove('123');
|
|
||||||
});
|
|
||||||
});
|
|
@ -1,127 +0,0 @@
|
|||||||
import { Observable } from 'lib0/observable';
|
|
||||||
import type { WorkspaceInfo, WorkspaceMeta } from './types';
|
|
||||||
|
|
||||||
export interface WorkspaceMetaCollectionScope {
|
|
||||||
get: (workspaceId: string) => WorkspaceInfo | undefined;
|
|
||||||
list: () => WorkspaceInfo[];
|
|
||||||
add: (workspace: WorkspaceInfo) => void;
|
|
||||||
remove: (workspaceId: string) => boolean;
|
|
||||||
clear: () => void;
|
|
||||||
update: (workspaceId: string, workspaceMeta: Partial<WorkspaceMeta>) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface WorkspaceMetaCollectionChangeEvent {
|
|
||||||
added?: WorkspaceInfo;
|
|
||||||
deleted?: WorkspaceInfo;
|
|
||||||
updated?: WorkspaceInfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class WorkspaceMetaCollection extends Observable<'change'> {
|
|
||||||
private _workspacesMap = new Map<string, WorkspaceInfo>();
|
|
||||||
|
|
||||||
get workspaces(): WorkspaceInfo[] {
|
|
||||||
return Array.from(this._workspacesMap.values());
|
|
||||||
}
|
|
||||||
|
|
||||||
find(workspaceId: string) {
|
|
||||||
return this._workspacesMap.get(workspaceId);
|
|
||||||
}
|
|
||||||
|
|
||||||
createScope(): WorkspaceMetaCollectionScope {
|
|
||||||
const scopedWorkspaceIds = new Set<string>();
|
|
||||||
|
|
||||||
const get = (workspaceId: string) => {
|
|
||||||
if (!scopedWorkspaceIds.has(workspaceId)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return this._workspacesMap.get(workspaceId);
|
|
||||||
};
|
|
||||||
|
|
||||||
const add = (workspace: WorkspaceInfo) => {
|
|
||||||
if (this._workspacesMap.has(workspace.id)) {
|
|
||||||
throw new Error(`Duplicate workspace id.`);
|
|
||||||
}
|
|
||||||
this._workspacesMap.set(workspace.id, workspace);
|
|
||||||
scopedWorkspaceIds.add(workspace.id);
|
|
||||||
|
|
||||||
this.emit('change', [
|
|
||||||
{
|
|
||||||
added: workspace,
|
|
||||||
} as WorkspaceMetaCollectionChangeEvent,
|
|
||||||
]);
|
|
||||||
};
|
|
||||||
|
|
||||||
const remove = (workspaceId: string) => {
|
|
||||||
if (!scopedWorkspaceIds.has(workspaceId)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const workspace = this._workspacesMap.get(workspaceId);
|
|
||||||
if (workspace) {
|
|
||||||
const ret = this._workspacesMap.delete(workspaceId);
|
|
||||||
// If deletion failed, return.
|
|
||||||
if (!ret) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
scopedWorkspaceIds.delete(workspaceId);
|
|
||||||
|
|
||||||
this.emit('change', [
|
|
||||||
{
|
|
||||||
deleted: workspace,
|
|
||||||
} as WorkspaceMetaCollectionChangeEvent,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
const clear = () => {
|
|
||||||
scopedWorkspaceIds.forEach(id => {
|
|
||||||
remove(id);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const update = (
|
|
||||||
workspaceId: string,
|
|
||||||
workspaceMeta: Partial<WorkspaceMeta>
|
|
||||||
) => {
|
|
||||||
if (!scopedWorkspaceIds.has(workspaceId)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const workspace = this._workspacesMap.get(workspaceId);
|
|
||||||
if (!workspace) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._workspacesMap.set(workspaceId, { ...workspace, ...workspaceMeta });
|
|
||||||
|
|
||||||
this.emit('change', [
|
|
||||||
{
|
|
||||||
updated: this._workspacesMap.get(workspaceId),
|
|
||||||
} as WorkspaceMetaCollectionChangeEvent,
|
|
||||||
]);
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: need to optimize
|
|
||||||
const list = () => {
|
|
||||||
const workspaces: WorkspaceInfo[] = [];
|
|
||||||
scopedWorkspaceIds.forEach(id => {
|
|
||||||
const workspace = this._workspacesMap.get(id);
|
|
||||||
if (workspace) {
|
|
||||||
workspaces.push(workspace);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return workspaces;
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
get,
|
|
||||||
list,
|
|
||||||
add,
|
|
||||||
remove,
|
|
||||||
clear,
|
|
||||||
update,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
60
packages/data-center/src/workspace-unit-collection.spec.ts
Normal file
60
packages/data-center/src/workspace-unit-collection.spec.ts
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
import { test, expect } from '@playwright/test';
|
||||||
|
import { WorkspaceUnitCollection } from './workspace-unit-collection.js';
|
||||||
|
import type { WorkspaceUnitCollectionChangeEvent } from './workspace-unit-collection';
|
||||||
|
|
||||||
|
test.describe.serial('workspace meta collection observable', () => {
|
||||||
|
const workspaceUnitCollection = new WorkspaceUnitCollection();
|
||||||
|
|
||||||
|
const scope = workspaceUnitCollection.createScope();
|
||||||
|
|
||||||
|
test('add workspace', () => {
|
||||||
|
workspaceUnitCollection.once(
|
||||||
|
'change',
|
||||||
|
(event: WorkspaceUnitCollectionChangeEvent) => {
|
||||||
|
expect(event.added?.id).toEqual('123');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
scope.add({
|
||||||
|
id: '123',
|
||||||
|
name: 'test',
|
||||||
|
memberCount: 1,
|
||||||
|
provider: '',
|
||||||
|
syncMode: 'core',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('list workspace', () => {
|
||||||
|
const list = scope.list();
|
||||||
|
expect(list.length).toEqual(1);
|
||||||
|
expect(list[0].id).toEqual('123');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('get workspace', () => {
|
||||||
|
expect(scope.get('123')?.id).toEqual('123');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('update workspace', () => {
|
||||||
|
workspaceUnitCollection.once(
|
||||||
|
'change',
|
||||||
|
(event: WorkspaceUnitCollectionChangeEvent) => {
|
||||||
|
expect(event.updated?.name).toEqual('demo');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
scope.update('123', { name: 'demo' });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('get workspace form other scope', () => {
|
||||||
|
const scope1 = workspaceUnitCollection.createScope();
|
||||||
|
expect(scope1.get('123')).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('delete workspace', () => {
|
||||||
|
workspaceUnitCollection.once(
|
||||||
|
'change',
|
||||||
|
(event: WorkspaceUnitCollectionChangeEvent) => {
|
||||||
|
expect(event.deleted?.id).toEqual('123');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
scope.remove('123');
|
||||||
|
});
|
||||||
|
});
|
152
packages/data-center/src/workspace-unit-collection.ts
Normal file
152
packages/data-center/src/workspace-unit-collection.ts
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
import { Observable } from 'lib0/observable';
|
||||||
|
import { WorkspaceUnit } from './workspace-unit.js';
|
||||||
|
import type {
|
||||||
|
WorkspaceUnitCtorParams,
|
||||||
|
UpdateWorkspaceUnitParams,
|
||||||
|
} from './workspace-unit';
|
||||||
|
|
||||||
|
export interface WorkspaceUnitCollectionScope {
|
||||||
|
get: (workspaceId: string) => WorkspaceUnit | undefined;
|
||||||
|
list: () => WorkspaceUnit[];
|
||||||
|
add: (workspace: WorkspaceUnitCtorParams) => void;
|
||||||
|
remove: (workspaceId: string) => boolean;
|
||||||
|
clear: () => void;
|
||||||
|
update: (
|
||||||
|
workspaceId: string,
|
||||||
|
workspaceMeta: UpdateWorkspaceUnitParams
|
||||||
|
) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WorkspaceUnitCollectionChangeEvent {
|
||||||
|
added?: WorkspaceUnit;
|
||||||
|
deleted?: WorkspaceUnit;
|
||||||
|
updated?: WorkspaceUnit;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class WorkspaceUnitCollection {
|
||||||
|
private _events = new Observable();
|
||||||
|
private _workspaceUnitMap = new Map<string, WorkspaceUnit>();
|
||||||
|
|
||||||
|
get workspaces(): WorkspaceUnit[] {
|
||||||
|
return Array.from(this._workspaceUnitMap.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
public on(
|
||||||
|
type: 'change',
|
||||||
|
callback: (event: WorkspaceUnitCollectionChangeEvent) => void
|
||||||
|
) {
|
||||||
|
this._events.on(type, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public once(
|
||||||
|
type: 'change',
|
||||||
|
callback: (event: WorkspaceUnitCollectionChangeEvent) => void
|
||||||
|
) {
|
||||||
|
this._events.once(type, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
find(workspaceId: string) {
|
||||||
|
return this._workspaceUnitMap.get(workspaceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
createScope(): WorkspaceUnitCollectionScope {
|
||||||
|
const scopedWorkspaceIds = new Set<string>();
|
||||||
|
|
||||||
|
const get = (workspaceId: string) => {
|
||||||
|
if (!scopedWorkspaceIds.has(workspaceId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return this._workspaceUnitMap.get(workspaceId);
|
||||||
|
};
|
||||||
|
|
||||||
|
const add = (workspace: WorkspaceUnitCtorParams) => {
|
||||||
|
if (this._workspaceUnitMap.has(workspace.id)) {
|
||||||
|
throw new Error(`Duplicate workspace id.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspaceUnit = new WorkspaceUnit(workspace);
|
||||||
|
this._workspaceUnitMap.set(workspace.id, workspaceUnit);
|
||||||
|
|
||||||
|
scopedWorkspaceIds.add(workspace.id);
|
||||||
|
|
||||||
|
this._events.emit('change', [
|
||||||
|
{
|
||||||
|
added: workspaceUnit,
|
||||||
|
} as WorkspaceUnitCollectionChangeEvent,
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
|
const remove = (workspaceId: string) => {
|
||||||
|
if (!scopedWorkspaceIds.has(workspaceId)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspaceUnit = this._workspaceUnitMap.get(workspaceId);
|
||||||
|
if (workspaceUnit) {
|
||||||
|
const ret = this._workspaceUnitMap.delete(workspaceId);
|
||||||
|
// If deletion failed, return.
|
||||||
|
if (!ret) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
scopedWorkspaceIds.delete(workspaceId);
|
||||||
|
|
||||||
|
this._events.emit('change', [
|
||||||
|
{
|
||||||
|
deleted: workspaceUnit,
|
||||||
|
} as WorkspaceUnitCollectionChangeEvent,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const clear = () => {
|
||||||
|
scopedWorkspaceIds.forEach(id => {
|
||||||
|
remove(id);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const update = (
|
||||||
|
workspaceId: string,
|
||||||
|
workspaceMeta: UpdateWorkspaceUnitParams
|
||||||
|
) => {
|
||||||
|
if (!scopedWorkspaceIds.has(workspaceId)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspaceUnit = this._workspaceUnitMap.get(workspaceId);
|
||||||
|
if (!workspaceUnit) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
workspaceUnit.update(workspaceMeta);
|
||||||
|
|
||||||
|
this._events.emit('change', [
|
||||||
|
{
|
||||||
|
updated: workspaceUnit,
|
||||||
|
} as WorkspaceUnitCollectionChangeEvent,
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: need to optimize
|
||||||
|
const list = () => {
|
||||||
|
const workspaceUnits: WorkspaceUnit[] = [];
|
||||||
|
scopedWorkspaceIds.forEach(id => {
|
||||||
|
const workspaceUnit = this._workspaceUnitMap.get(id);
|
||||||
|
if (workspaceUnit) {
|
||||||
|
workspaceUnits.push(workspaceUnit);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return workspaceUnits;
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
get,
|
||||||
|
list,
|
||||||
|
add,
|
||||||
|
remove,
|
||||||
|
clear,
|
||||||
|
update,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
64
packages/data-center/src/workspace-unit.ts
Normal file
64
packages/data-center/src/workspace-unit.ts
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
import { Workspace as BlocksuiteWorkspace } from '@blocksuite/store';
|
||||||
|
import type { User } from './types';
|
||||||
|
|
||||||
|
export type SyncMode = 'all' | 'core';
|
||||||
|
|
||||||
|
export interface WorkspaceUnitCtorParams {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
avatar?: string;
|
||||||
|
owner?: User;
|
||||||
|
published?: boolean;
|
||||||
|
memberCount: number;
|
||||||
|
provider: string;
|
||||||
|
syncMode: SyncMode;
|
||||||
|
|
||||||
|
blocksuiteWorkspace?: BlocksuiteWorkspace;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type UpdateWorkspaceUnitParams = Partial<
|
||||||
|
Omit<WorkspaceUnitCtorParams, 'id'>
|
||||||
|
>;
|
||||||
|
|
||||||
|
export class WorkspaceUnit {
|
||||||
|
public readonly id: string;
|
||||||
|
public name!: string;
|
||||||
|
public avatar?: string;
|
||||||
|
public owner?: User;
|
||||||
|
public published?: boolean;
|
||||||
|
public memberCount!: number;
|
||||||
|
public provider!: string;
|
||||||
|
public syncMode: 'all' | 'core' = 'core';
|
||||||
|
|
||||||
|
private _blocksuiteWorkspace?: BlocksuiteWorkspace;
|
||||||
|
|
||||||
|
constructor(params: WorkspaceUnitCtorParams) {
|
||||||
|
this.id = params.id;
|
||||||
|
this.update(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
get blocksuiteWorkspace() {
|
||||||
|
return this._blocksuiteWorkspace;
|
||||||
|
}
|
||||||
|
|
||||||
|
setBlocksuiteWorkspace(blocksuiteWorkspace: BlocksuiteWorkspace) {
|
||||||
|
if (blocksuiteWorkspace?.room !== this.id) {
|
||||||
|
throw new Error('Workspace id inconsistent.');
|
||||||
|
}
|
||||||
|
this._blocksuiteWorkspace = blocksuiteWorkspace;
|
||||||
|
}
|
||||||
|
|
||||||
|
update(params: UpdateWorkspaceUnitParams) {
|
||||||
|
Object.assign(this, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
get isPublish() {
|
||||||
|
console.error('Suggest changing to published');
|
||||||
|
return this.published;
|
||||||
|
}
|
||||||
|
|
||||||
|
get isLocal() {
|
||||||
|
console.error('Suggest changing to syncMode');
|
||||||
|
return this.syncMode === 'all';
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user