diff --git a/packages/data-center/src/datacenter.ts b/packages/data-center/src/datacenter.ts index 487e82cd5f..e4fe394c71 100644 --- a/packages/data-center/src/datacenter.ts +++ b/packages/data-center/src/datacenter.ts @@ -46,6 +46,10 @@ export class DataCenter { dc.registerProvider(new LocalProvider(getInitParams())); dc.registerProvider(new AffineProvider(getInitParams())); + for (const provider of dc.providerMap.values()) { + await provider.loadWorkspaces(); + } + return dc; } @@ -181,8 +185,14 @@ export class DataCenter { * @param {Function} callback callback function */ public async onWorkspacesChange( - callback: (workspaces: WorkspaceUnitCollectionChangeEvent) => void + callback: (workspaces: WorkspaceUnitCollectionChangeEvent) => void, + { immediate = true }: { immediate?: boolean } ) { + if (immediate) { + callback({ + added: this._workspaceUnitCollection.workspaces, + }); + } this._workspaceUnitCollection.on('change', callback); } diff --git a/packages/data-center/src/provider/affine/affine.ts b/packages/data-center/src/provider/affine/affine.ts index b98a08cf25..cbe5adef18 100644 --- a/packages/data-center/src/provider/affine/affine.ts +++ b/packages/data-center/src/provider/affine/affine.ts @@ -2,7 +2,6 @@ import { BaseProvider } from '../base.js'; import type { ProviderConstructorParams, CreateWorkspaceInfoParams, - UpdateWorkspaceMetaParams, WorkspaceMeta0, } from '../base'; import type { User } from '../../types'; @@ -34,11 +33,6 @@ export class AffineProvider extends BaseProvider { constructor({ apis, ...params }: AffineProviderConstructorParams) { super(params); this._apis = apis || getApis(); - this.init().then(() => { - if (this._apis.token.isLogin) { - this.loadWorkspaces(); - } - }); } override async init() { diff --git a/packages/data-center/src/provider/local/local.ts b/packages/data-center/src/provider/local/local.ts index f87d7c95fb..2bc6bae718 100644 --- a/packages/data-center/src/provider/local/local.ts +++ b/packages/data-center/src/provider/local/local.ts @@ -19,7 +19,6 @@ export class LocalProvider extends BaseProvider { constructor(params: ProviderConstructorParams) { super(params); - this.loadWorkspaces(); } private _storeWorkspaces(workspaces: WorkspaceMeta0[]) { diff --git a/packages/data-center/src/workspace-unit-collection.ts b/packages/data-center/src/workspace-unit-collection.ts index e4dedad8c7..58770e2122 100644 --- a/packages/data-center/src/workspace-unit-collection.ts +++ b/packages/data-center/src/workspace-unit-collection.ts @@ -18,7 +18,7 @@ export interface WorkspaceUnitCollectionScope { } export interface WorkspaceUnitCollectionChangeEvent { - added?: WorkspaceUnit; + added?: WorkspaceUnit[]; deleted?: WorkspaceUnit; updated?: WorkspaceUnit; } @@ -71,7 +71,7 @@ export class WorkspaceUnitCollection { this._events.emit('change', [ { - added: workspaceUnit, + added: [workspaceUnit], } as WorkspaceUnitCollectionChangeEvent, ]); };