refactor(infra): simplify currentWorkspaceAtom (#4462)

This commit is contained in:
Alex Yang 2023-09-22 15:07:26 -05:00 committed by GitHub
parent 56d8fa5d29
commit 2da6702991
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 24 deletions

View File

@ -16,7 +16,7 @@ import {
} from '@toeverything/infra/__internal__/plugin'; } from '@toeverything/infra/__internal__/plugin';
import { import {
contentLayoutAtom, contentLayoutAtom,
currentPageAtom, currentPageIdAtom,
currentWorkspaceAtom, currentWorkspaceAtom,
} from '@toeverything/infra/atom'; } from '@toeverything/infra/atom';
import { atom } from 'jotai'; import { atom } from 'jotai';
@ -129,7 +129,7 @@ export function createSetup(rootStore: ReturnType<typeof createStore>) {
function createSetupImpl(rootStore: ReturnType<typeof createStore>) { function createSetupImpl(rootStore: ReturnType<typeof createStore>) {
// clean up plugin windows when switching to other pages // clean up plugin windows when switching to other pages
rootStore.sub(currentPageAtom, () => { rootStore.sub(currentPageIdAtom, () => {
rootStore.set(contentLayoutAtom, 'editor'); rootStore.set(contentLayoutAtom, 'editor');
}); });
@ -149,7 +149,7 @@ function createSetupImpl(rootStore: ReturnType<typeof createStore>) {
'@affine/sdk/entry': { '@affine/sdk/entry': {
rootStore, rootStore,
currentWorkspaceAtom: currentWorkspaceAtom, currentWorkspaceAtom: currentWorkspaceAtom,
currentPageAtom: currentPageAtom, currentPageIdAtom: currentPageIdAtom,
pushLayoutAtom: pushLayoutAtom, pushLayoutAtom: pushLayoutAtom,
deleteLayoutAtom: deleteLayoutAtom, deleteLayoutAtom: deleteLayoutAtom,
}, },

View File

@ -1,9 +1,9 @@
import type { ExpectedLayout } from '@affine/sdk/entry'; import type { ExpectedLayout } from '@affine/sdk/entry';
import { assertExists } from '@blocksuite/global/utils'; import { assertExists } from '@blocksuite/global/utils';
import type { Page, Workspace } from '@blocksuite/store'; import type { Workspace } from '@blocksuite/store';
import { atom, createStore } from 'jotai/vanilla'; import { atom, createStore } from 'jotai/vanilla';
import { getWorkspace, waitForWorkspace } from './__internal__/workspace.js'; import { getActiveBlockSuiteWorkspaceAtom } from './__internal__/workspace';
// global store // global store
let rootStore = createStore(); let rootStore = createStore();
@ -24,25 +24,10 @@ export const loadedPluginNameAtom = atom<string[]>([]);
export const currentWorkspaceIdAtom = atom<string | null>(null); export const currentWorkspaceIdAtom = atom<string | null>(null);
export const currentPageIdAtom = atom<string | null>(null); export const currentPageIdAtom = atom<string | null>(null);
export const currentWorkspaceAtom = atom<Promise<Workspace>>(async get => { export const currentWorkspaceAtom = atom<Promise<Workspace>>(async get => {
const currentWorkspaceId = get(currentWorkspaceIdAtom); const workspaceId = get(currentWorkspaceIdAtom);
assertExists(currentWorkspaceId, 'current workspace id'); assertExists(workspaceId);
const workspace = getWorkspace(currentWorkspaceId); const currentWorkspaceAtom = getActiveBlockSuiteWorkspaceAtom(workspaceId);
await waitForWorkspace(workspace); return get(currentWorkspaceAtom);
return workspace;
});
export const currentPageAtom = atom<Promise<Page>>(async get => {
const currentWorkspaceId = get(currentWorkspaceIdAtom);
assertExists(currentWorkspaceId, 'current workspace id');
const currentPageId = get(currentPageIdAtom);
assertExists(currentPageId, 'current page id');
const workspace = getWorkspace(currentWorkspaceId);
await waitForWorkspace(workspace);
const page = workspace.getPage(currentPageId);
assertExists(page);
if (!page.loaded) {
await page.waitForLoaded();
}
return page;
}); });
const contentLayoutBaseAtom = atom<ExpectedLayout>('editor'); const contentLayoutBaseAtom = atom<ExpectedLayout>('editor');