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

View File

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