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
@@ -107,47 +107,59 @@ 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 newlyRemovedWorkspacecIds = currentWorkspaceIds;
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 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
); */
const workspace = { if (name) {
name: name, const workspace = {
avatar, name: name,
owner: { avatar,
name: detail.owner.name, owner: {
id: detail.owner.id, name: detail.owner.name,
email: detail.owner.email, id: detail.owner.id,
avatar: detail.owner.avatar_url, email: detail.owner.email,
}, avatar: detail.owner.avatar_url,
published: detail.public, },
memberCount: detail.member_count, published: detail.public,
provider: this.id, memberCount: detail.member_count,
syncMode: 'core' as SyncMode, provider: this.id,
}; syncMode: 'core' as SyncMode,
if (this._workspaces.get(id)) { };
// update workspaces if (this._workspaces.get(id)) {
this._workspaces.update(id, workspace); // update workspaces
this._workspaces.update(id, workspace);
} else {
const workspaceUnit = await loadWorkspaceUnit(
{ id, ...workspace },
this._apis
);
newlyCreatedWorkspaces.push(workspaceUnit);
}
} else { } else {
const workspaceUnit = await loadWorkspaceUnit( console.log(`[log warn] ${id} name is empty`);
{ id, ...workspace },
this._apis
);
addedWorkspaces.push(workspaceUnit);
} }
} }
// 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(newlyRemovedWorkspacecIds);
} }
private _getWebsocketProvider(workspace: BlocksuiteWorkspace) { private _getWebsocketProvider(workspace: BlocksuiteWorkspace) {