fix: duplicate api call for cloud workspace (#928)

This commit is contained in:
zuomeng wang 2023-02-09 15:53:46 +08:00 committed by GitHub
parent 718322ec65
commit 5a93b26cc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -45,6 +45,7 @@ export class AffineProvider extends BaseProvider {
private _apis: Apis;
private _channel?: WebsocketClient;
// private _idbMap: Map<string, IndexedDBProvider> = new Map();
private _workspaceLoadingQueue: Set<string> = new Set();
constructor({ apis, ...params }: AffineProviderConstructorParams) {
super(params);
@ -145,11 +146,13 @@ export class AffineProvider extends BaseProvider {
// update workspaces
this._workspaces.update(id, workspace);
} else {
const workspaceUnit = await loadWorkspaceUnit(
{ id, ...workspace },
this._apis
);
newlyCreatedWorkspaces.push(workspaceUnit);
if (!this._workspaceLoadingQueue.has(id)) {
const workspaceUnit = await loadWorkspaceUnit(
{ id, ...workspace },
this._apis
);
newlyCreatedWorkspaces.push(workspaceUnit);
}
}
} else {
console.log(`[log warn] ${id} name is empty`);
@ -204,6 +207,7 @@ export class AffineProvider extends BaseProvider {
}
override async warpWorkspace(workspace: BlocksuiteWorkspace) {
// FIXME: if add indexedDB cache in the future, can remove following line.
await this._applyCloudUpdates(workspace);
const { room } = workspace;
assert(room);
@ -234,6 +238,7 @@ export class AffineProvider extends BaseProvider {
const workspacesList = await this._apis.getWorkspaces();
const workspaceUnits = await Promise.all(
workspacesList.map(w => {
this._workspaceLoadingQueue.add(w.id);
return loadWorkspaceUnit(
{
id: w.id,
@ -246,7 +251,9 @@ export class AffineProvider extends BaseProvider {
syncMode: 'core',
},
this._apis
);
).finally(() => {
this._workspaceLoadingQueue.delete(w.id);
});
})
);
this._workspaces.add(workspaceUnits);