From b102e234e3eae307d7415c6a00491dbc2e12e4f2 Mon Sep 17 00:00:00 2001 From: tzhangchi Date: Fri, 3 Feb 2023 23:25:44 +0800 Subject: [PATCH 1/2] refactor: The implementation of the _handlerAffineListMessage function has been improved to make the code for synchronising the latest number and status of workspaces more readable and easier to understand, --- .../data-center/src/provider/affine/affine.ts | 82 +++++++++++-------- 1 file changed, 47 insertions(+), 35 deletions(-) diff --git a/packages/data-center/src/provider/affine/affine.ts b/packages/data-center/src/provider/affine/affine.ts index b061b1ce5a..ad53c496c3 100644 --- a/packages/data-center/src/provider/affine/affine.ts +++ b/packages/data-center/src/provider/affine/affine.ts @@ -107,47 +107,59 @@ export class AffineProvider extends BaseProvider { metadata, }: ChannelMessage) { this._logger('receive server message'); - const addedWorkspaces: WorkspaceUnit[] = []; - const removeWorkspaceList = this._workspaces.list().map(w => w.id); + const newlyCreatedWorkspaces: WorkspaceUnit[] = []; + const currentWorkspaceIds = this._workspaces.list().map(w => w.id); + const newlyRemoveWorkspaceIds = []; + for (const [id, detail] of Object.entries(ws_details)) { const { name, avatar } = metadata[id]; - const index = removeWorkspaceList.indexOf(id); - if (index !== -1) { - removeWorkspaceList.splice(index, 1); + + /** + * collect the workspaces that need to be removed in the context + */ + const ifWorkspaceNotExists = currentWorkspaceIds.indexOf(id) < 0; + if (ifWorkspaceNotExists) { + newlyRemoveWorkspaceIds.push(id); } - assert( - name, - 'workspace name not found by id when receive server message' - ); - const workspace = { - name: name, - avatar, - owner: { - name: detail.owner.name, - id: detail.owner.id, - email: detail.owner.email, - avatar: detail.owner.avatar_url, - }, - published: detail.public, - memberCount: detail.member_count, - provider: this.id, - syncMode: 'core' as SyncMode, - }; - if (this._workspaces.get(id)) { - // update workspaces - this._workspaces.update(id, workspace); + + /** + * if workspace name is not empty, it is a valid workspace, so sync its state + */ + if (name) { + const workspace = { + name: name, + avatar, + owner: { + name: detail.owner.name, + id: detail.owner.id, + email: detail.owner.email, + avatar: detail.owner.avatar_url, + }, + published: detail.public, + memberCount: detail.member_count, + provider: this.id, + syncMode: 'core' as SyncMode, + }; + if (this._workspaces.get(id)) { + // update workspaces + this._workspaces.update(id, workspace); + } else { + const workspaceUnit = await loadWorkspaceUnit( + { id, ...workspace }, + this._apis + ); + newlyCreatedWorkspaces.push(workspaceUnit); + } } else { - const workspaceUnit = await loadWorkspaceUnit( - { id, ...workspace }, - this._apis - ); - addedWorkspaces.push(workspaceUnit); + console.log(`[log warn] ${id} name is empty`); } } - // add workspaces - this._workspaces.add(addedWorkspaces); - // remove workspaces - this._workspaces.remove(removeWorkspaceList); + + // sync newlyCreatedWorkspaces to context + this._workspaces.add(newlyCreatedWorkspaces); + + // sync newlyRemoveWorkspaces to context + this._workspaces.remove(newlyRemoveWorkspaceIds); } private _getWebsocketProvider(workspace: BlocksuiteWorkspace) { From 9a315ed994cda09263c6888b57ebaf55baf9c83f Mon Sep 17 00:00:00 2001 From: tzhangchi Date: Fri, 3 Feb 2023 23:57:24 +0800 Subject: [PATCH 2/2] fix: make remove workspace logic correct --- packages/data-center/src/provider/affine/affine.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/data-center/src/provider/affine/affine.ts b/packages/data-center/src/provider/affine/affine.ts index ad53c496c3..b4627de081 100644 --- a/packages/data-center/src/provider/affine/affine.ts +++ b/packages/data-center/src/provider/affine/affine.ts @@ -109,17 +109,17 @@ export class AffineProvider extends BaseProvider { this._logger('receive server message'); const newlyCreatedWorkspaces: WorkspaceUnit[] = []; const currentWorkspaceIds = this._workspaces.list().map(w => w.id); - const newlyRemoveWorkspaceIds = []; - + const newlyRemovedWorkspacecIds = currentWorkspaceIds; for (const [id, detail] of Object.entries(ws_details)) { const { name, avatar } = metadata[id]; /** * collect the workspaces that need to be removed in the context */ - const ifWorkspaceNotExists = currentWorkspaceIds.indexOf(id) < 0; - if (ifWorkspaceNotExists) { - newlyRemoveWorkspaceIds.push(id); + const workspaceIndex = currentWorkspaceIds.indexOf(id); + const ifWorkspaceExist = workspaceIndex !== -1; + if (ifWorkspaceExist) { + newlyRemovedWorkspacecIds.splice(workspaceIndex, 1); } /** @@ -159,7 +159,7 @@ export class AffineProvider extends BaseProvider { this._workspaces.add(newlyCreatedWorkspaces); // sync newlyRemoveWorkspaces to context - this._workspaces.remove(newlyRemoveWorkspaceIds); + this._workspaces.remove(newlyRemovedWorkspacecIds); } private _getWebsocketProvider(workspace: BlocksuiteWorkspace) {