diff --git a/apps/web/src/blocksuite/providers/index.ts b/apps/web/src/blocksuite/providers/index.ts index b1e3b7705..3e53017ec 100644 --- a/apps/web/src/blocksuite/providers/index.ts +++ b/apps/web/src/blocksuite/providers/index.ts @@ -56,13 +56,16 @@ const createIndexedDBProvider = ( blockSuiteWorkspace: BlockSuiteWorkspace ): LocalIndexedDBProvider => { let indexeddbProvider: IndexeddbPersistence | null = null; + const callbacks = new Set<() => void>(); return { flavour: 'local-indexeddb', + callbacks, // fixme: remove background long polling background: true, cleanup: () => { assertExists(indexeddbProvider); indexeddbProvider.clearData(); + callbacks.clear(); indexeddbProvider = null; }, connect: () => { @@ -71,6 +74,9 @@ const createIndexedDBProvider = ( blockSuiteWorkspace.id, blockSuiteWorkspace.doc ); + indexeddbProvider.whenSynced.then(() => { + callbacks.forEach(cb => cb()); + }); }, disconnect: () => { assertExists(indexeddbProvider); diff --git a/apps/web/src/pages/workspace/[workspaceId]/all.tsx b/apps/web/src/pages/workspace/[workspaceId]/all.tsx index f70d21ab6..e6d0b7168 100644 --- a/apps/web/src/pages/workspace/[workspaceId]/all.tsx +++ b/apps/web/src/pages/workspace/[workspaceId]/all.tsx @@ -16,7 +16,11 @@ import { useRouterHelper } from '../../../hooks/use-router-helper'; import { useSyncRouterWithCurrentWorkspace } from '../../../hooks/use-sync-router-with-current-workspace'; import { WorkspaceLayout } from '../../../layouts'; import { WorkspacePlugins } from '../../../plugins'; -import { NextPageWithLayout, RemWorkspaceFlavour } from '../../../shared'; +import { + LocalIndexedDBProvider, + NextPageWithLayout, + RemWorkspaceFlavour, +} from '../../../shared'; const AllPage: NextPageWithLayout = () => { const router = useRouter(); @@ -28,23 +32,33 @@ const AllPage: NextPageWithLayout = () => { if (!router.isReady) { return; } - const id = setTimeout(() => { - if (currentWorkspace?.blockSuiteWorkspace.isEmpty) { - // this is a new workspace, so we should redirect to the new page - const pageId = nanoid(); - currentWorkspace.blockSuiteWorkspace.slots.pageAdded.once(id => { - currentWorkspace.blockSuiteWorkspace.setPageMeta(id, { - init: true, + if (!currentWorkspace) { + return; + } + const localProvider = currentWorkspace.providers.find( + provider => provider.flavour === 'local-indexeddb' + ); + if (localProvider && localProvider.flavour === 'local-indexeddb') { + const provider = localProvider as LocalIndexedDBProvider; + const callback = () => { + if (currentWorkspace.blockSuiteWorkspace.isEmpty) { + // this is a new workspace, so we should redirect to the new page + const pageId = nanoid(); + currentWorkspace.blockSuiteWorkspace.slots.pageAdded.once(id => { + currentWorkspace.blockSuiteWorkspace.setPageMeta(id, { + init: true, + }); + assertExists(pageId, id); + jumpToPage(currentWorkspace.id, pageId); }); - assertExists(pageId, id); - jumpToPage(currentWorkspace.id, pageId); - }); - currentWorkspace.blockSuiteWorkspace.createPage(pageId); - } - }, 1000); - return () => { - clearTimeout(id); - }; + currentWorkspace.blockSuiteWorkspace.createPage(pageId); + } + }; + provider.callbacks.add(callback); + return () => { + provider.callbacks.delete(callback); + }; + } }, [currentWorkspace, jumpToPage, router]); const onClickPage = useCallback( (pageId: string, newTab?: boolean) => { diff --git a/apps/web/src/shared/index.ts b/apps/web/src/shared/index.ts index ae6310199..98dbf38ea 100644 --- a/apps/web/src/shared/index.ts +++ b/apps/web/src/shared/index.ts @@ -45,6 +45,11 @@ export type BaseProvider = { cleanup: () => void; }; +export interface BackgroundProvider extends BaseProvider { + background: true; + callbacks: Set<() => void>; +} + export interface AffineDownloadProvider extends BaseProvider { flavour: 'affine-download'; } @@ -53,7 +58,7 @@ export interface BroadCastChannelProvider extends BaseProvider { flavour: 'broadcast-channel'; } -export interface LocalIndexedDBProvider extends BaseProvider { +export interface LocalIndexedDBProvider extends BackgroundProvider { flavour: 'local-indexeddb'; }