Merge pull request #782 from toeverything/bugfix/20230203

refactor: The implementation of the _handlerAffineListMessage functio…
This commit is contained in:
DarkSky
2023-02-04 15:40:03 +08:00
committed by GitHub

View File

@@ -107,18 +107,25 @@ 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 newlyRemovedWorkspacecIds = currentWorkspaceIds;
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 workspaceIndex = currentWorkspaceIds.indexOf(id);
const ifWorkspaceExist = workspaceIndex !== -1;
if (ifWorkspaceExist) {
newlyRemovedWorkspacecIds.splice(workspaceIndex, 1);
}
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 = {
name: name,
avatar,
@@ -141,13 +148,18 @@ export class AffineProvider extends BaseProvider {
{ id, ...workspace },
this._apis
);
addedWorkspaces.push(workspaceUnit);
newlyCreatedWorkspaces.push(workspaceUnit);
}
} else {
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(newlyRemovedWorkspacecIds);
}
private _getWebsocketProvider(workspace: BlocksuiteWorkspace) {