mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-15 05:37:32 +00:00
fix: init workspace logic (#1471)
This commit is contained in:
@@ -56,13 +56,16 @@ const createIndexedDBProvider = (
|
|||||||
blockSuiteWorkspace: BlockSuiteWorkspace
|
blockSuiteWorkspace: BlockSuiteWorkspace
|
||||||
): LocalIndexedDBProvider => {
|
): LocalIndexedDBProvider => {
|
||||||
let indexeddbProvider: IndexeddbPersistence | null = null;
|
let indexeddbProvider: IndexeddbPersistence | null = null;
|
||||||
|
const callbacks = new Set<() => void>();
|
||||||
return {
|
return {
|
||||||
flavour: 'local-indexeddb',
|
flavour: 'local-indexeddb',
|
||||||
|
callbacks,
|
||||||
// fixme: remove background long polling
|
// fixme: remove background long polling
|
||||||
background: true,
|
background: true,
|
||||||
cleanup: () => {
|
cleanup: () => {
|
||||||
assertExists(indexeddbProvider);
|
assertExists(indexeddbProvider);
|
||||||
indexeddbProvider.clearData();
|
indexeddbProvider.clearData();
|
||||||
|
callbacks.clear();
|
||||||
indexeddbProvider = null;
|
indexeddbProvider = null;
|
||||||
},
|
},
|
||||||
connect: () => {
|
connect: () => {
|
||||||
@@ -71,6 +74,9 @@ const createIndexedDBProvider = (
|
|||||||
blockSuiteWorkspace.id,
|
blockSuiteWorkspace.id,
|
||||||
blockSuiteWorkspace.doc
|
blockSuiteWorkspace.doc
|
||||||
);
|
);
|
||||||
|
indexeddbProvider.whenSynced.then(() => {
|
||||||
|
callbacks.forEach(cb => cb());
|
||||||
|
});
|
||||||
},
|
},
|
||||||
disconnect: () => {
|
disconnect: () => {
|
||||||
assertExists(indexeddbProvider);
|
assertExists(indexeddbProvider);
|
||||||
|
|||||||
@@ -16,7 +16,11 @@ import { useRouterHelper } from '../../../hooks/use-router-helper';
|
|||||||
import { useSyncRouterWithCurrentWorkspace } from '../../../hooks/use-sync-router-with-current-workspace';
|
import { useSyncRouterWithCurrentWorkspace } from '../../../hooks/use-sync-router-with-current-workspace';
|
||||||
import { WorkspaceLayout } from '../../../layouts';
|
import { WorkspaceLayout } from '../../../layouts';
|
||||||
import { WorkspacePlugins } from '../../../plugins';
|
import { WorkspacePlugins } from '../../../plugins';
|
||||||
import { NextPageWithLayout, RemWorkspaceFlavour } from '../../../shared';
|
import {
|
||||||
|
LocalIndexedDBProvider,
|
||||||
|
NextPageWithLayout,
|
||||||
|
RemWorkspaceFlavour,
|
||||||
|
} from '../../../shared';
|
||||||
|
|
||||||
const AllPage: NextPageWithLayout = () => {
|
const AllPage: NextPageWithLayout = () => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@@ -28,23 +32,33 @@ const AllPage: NextPageWithLayout = () => {
|
|||||||
if (!router.isReady) {
|
if (!router.isReady) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const id = setTimeout(() => {
|
if (!currentWorkspace) {
|
||||||
if (currentWorkspace?.blockSuiteWorkspace.isEmpty) {
|
return;
|
||||||
// this is a new workspace, so we should redirect to the new page
|
}
|
||||||
const pageId = nanoid();
|
const localProvider = currentWorkspace.providers.find(
|
||||||
currentWorkspace.blockSuiteWorkspace.slots.pageAdded.once(id => {
|
provider => provider.flavour === 'local-indexeddb'
|
||||||
currentWorkspace.blockSuiteWorkspace.setPageMeta(id, {
|
);
|
||||||
init: true,
|
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);
|
currentWorkspace.blockSuiteWorkspace.createPage(pageId);
|
||||||
jumpToPage(currentWorkspace.id, pageId);
|
}
|
||||||
});
|
};
|
||||||
currentWorkspace.blockSuiteWorkspace.createPage(pageId);
|
provider.callbacks.add(callback);
|
||||||
}
|
return () => {
|
||||||
}, 1000);
|
provider.callbacks.delete(callback);
|
||||||
return () => {
|
};
|
||||||
clearTimeout(id);
|
}
|
||||||
};
|
|
||||||
}, [currentWorkspace, jumpToPage, router]);
|
}, [currentWorkspace, jumpToPage, router]);
|
||||||
const onClickPage = useCallback(
|
const onClickPage = useCallback(
|
||||||
(pageId: string, newTab?: boolean) => {
|
(pageId: string, newTab?: boolean) => {
|
||||||
|
|||||||
@@ -45,6 +45,11 @@ export type BaseProvider = {
|
|||||||
cleanup: () => void;
|
cleanup: () => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export interface BackgroundProvider extends BaseProvider {
|
||||||
|
background: true;
|
||||||
|
callbacks: Set<() => void>;
|
||||||
|
}
|
||||||
|
|
||||||
export interface AffineDownloadProvider extends BaseProvider {
|
export interface AffineDownloadProvider extends BaseProvider {
|
||||||
flavour: 'affine-download';
|
flavour: 'affine-download';
|
||||||
}
|
}
|
||||||
@@ -53,7 +58,7 @@ export interface BroadCastChannelProvider extends BaseProvider {
|
|||||||
flavour: 'broadcast-channel';
|
flavour: 'broadcast-channel';
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LocalIndexedDBProvider extends BaseProvider {
|
export interface LocalIndexedDBProvider extends BackgroundProvider {
|
||||||
flavour: 'local-indexeddb';
|
flavour: 'local-indexeddb';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user