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,

This commit is contained in:
tzhangchi
2023-02-03 23:25:44 +08:00
parent 7522b76089
commit b102e234e3

View File

@@ -107,18 +107,25 @@ export class AffineProvider extends BaseProvider {
metadata, metadata,
}: ChannelMessage) { }: ChannelMessage) {
this._logger('receive server message'); this._logger('receive server message');
const addedWorkspaces: WorkspaceUnit[] = []; const newlyCreatedWorkspaces: WorkspaceUnit[] = [];
const removeWorkspaceList = this._workspaces.list().map(w => w.id); const currentWorkspaceIds = this._workspaces.list().map(w => w.id);
const newlyRemoveWorkspaceIds = [];
for (const [id, detail] of Object.entries(ws_details)) { for (const [id, detail] of Object.entries(ws_details)) {
const { name, avatar } = metadata[id]; 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' * if workspace name is not empty, it is a valid workspace, so sync its state
); */
if (name) {
const workspace = { const workspace = {
name: name, name: name,
avatar, avatar,
@@ -141,13 +148,18 @@ export class AffineProvider extends BaseProvider {
{ id, ...workspace }, { id, ...workspace },
this._apis this._apis
); );
addedWorkspaces.push(workspaceUnit); newlyCreatedWorkspaces.push(workspaceUnit);
}
} else {
console.log(`[log warn] ${id} name is empty`);
} }
} }
// add workspaces
this._workspaces.add(addedWorkspaces); // sync newlyCreatedWorkspaces to context
// remove workspaces this._workspaces.add(newlyCreatedWorkspaces);
this._workspaces.remove(removeWorkspaceList);
// sync newlyRemoveWorkspaces to context
this._workspaces.remove(newlyRemoveWorkspaceIds);
} }
private _getWebsocketProvider(workspace: BlocksuiteWorkspace) { private _getWebsocketProvider(workspace: BlocksuiteWorkspace) {