mirror of
https://github.com/toeverything/AFFiNE.git
synced 2025-01-09 01:47:11 +03:00
fix: init workspace logic (#1471)
This commit is contained in:
parent
047adde310
commit
a0fd890def
@ -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);
|
||||
|
@ -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) => {
|
||||
|
@ -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';
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user