From 1f5995ffc7bf2f55a54ed3b06d9da3c972ef42ce Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Thu, 13 Jul 2023 23:52:06 +0800 Subject: [PATCH] refactor(plugin-infra): workspace loading (#3222) --- .commitlintrc.json | 4 ++- apps/web/src/adapters/local/index.tsx | 2 +- .../setting-modal/setting-sidebar/index.tsx | 2 +- .../setting-modal/workspace-setting/index.tsx | 2 +- apps/web/src/hooks/use-workspace.ts | 2 +- apps/web/src/hooks/use-workspaces.ts | 2 +- apps/web/src/layouts/workspace-layout.tsx | 2 +- apps/web/src/pages/index.tsx | 2 +- .../src/components/block-hub/index.tsx | 1 - .../components/card/workspace-card/index.tsx | 2 +- packages/plugin-infra/package.json | 10 +++--- .../src/__internal__/workspace.ts} | 32 +++++++++++-------- .../use-block-suite-workspace.spec.ts | 7 ++-- packages/plugin-infra/src/manager.ts | 26 ++++++++++++++- packages/plugin-infra/vite.config.ts | 14 +++----- packages/workspace/src/utils.ts | 2 +- 16 files changed, 70 insertions(+), 42 deletions(-) rename packages/{hooks/src/use-block-suite-workspace.ts => plugin-infra/src/__internal__/workspace.ts} (76%) rename packages/{hooks => plugin-infra}/src/__tests__/use-block-suite-workspace.spec.ts (96%) diff --git a/.commitlintrc.json b/.commitlintrc.json index df382ac60c..8c6ec0ca66 100644 --- a/.commitlintrc.json +++ b/.commitlintrc.json @@ -22,7 +22,9 @@ "templates", "y-indexeddb", "debug", - "storage" + "storage", + "infra", + "plugin-infra" ] ] } diff --git a/apps/web/src/adapters/local/index.tsx b/apps/web/src/adapters/local/index.tsx index e84cfc4758..f6d42ccb06 100644 --- a/apps/web/src/adapters/local/index.tsx +++ b/apps/web/src/adapters/local/index.tsx @@ -19,7 +19,7 @@ import { import { createIndexedDBDownloadProvider } from '@affine/workspace/providers'; import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils'; import { nanoid } from '@blocksuite/store'; -import { useStaticBlockSuiteWorkspace } from '@toeverything/hooks/use-block-suite-workspace'; +import { useStaticBlockSuiteWorkspace } from '@toeverything/plugin-infra/__internal__/workspace'; import { BlockSuitePageList, diff --git a/apps/web/src/components/affine/setting-modal/setting-sidebar/index.tsx b/apps/web/src/components/affine/setting-modal/setting-sidebar/index.tsx index e60853d4b7..a90f57ea50 100644 --- a/apps/web/src/components/affine/setting-modal/setting-sidebar/index.tsx +++ b/apps/web/src/components/affine/setting-modal/setting-sidebar/index.tsx @@ -6,8 +6,8 @@ import { WorkspaceAvatar } from '@affine/component/workspace-avatar'; import { useAFFiNEI18N } from '@affine/i18n/hooks'; import type { RootWorkspaceMetadata } from '@affine/workspace/atom'; import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom'; -import { useStaticBlockSuiteWorkspace } from '@toeverything/hooks/use-block-suite-workspace'; import { useBlockSuiteWorkspaceName } from '@toeverything/hooks/use-block-suite-workspace-name'; +import { useStaticBlockSuiteWorkspace } from '@toeverything/plugin-infra/__internal__/workspace'; import clsx from 'clsx'; import { useAtomValue } from 'jotai'; import type { FC } from 'react'; diff --git a/apps/web/src/components/affine/setting-modal/workspace-setting/index.tsx b/apps/web/src/components/affine/setting-modal/workspace-setting/index.tsx index 11148ec8ca..f3d9d3685a 100644 --- a/apps/web/src/components/affine/setting-modal/workspace-setting/index.tsx +++ b/apps/web/src/components/affine/setting-modal/workspace-setting/index.tsx @@ -1,5 +1,5 @@ import { WorkspaceDetailSkeleton } from '@affine/component/setting-components'; -import { usePassiveWorkspaceEffect } from '@toeverything/hooks/use-block-suite-workspace'; +import { usePassiveWorkspaceEffect } from '@toeverything/plugin-infra/__internal__/workspace'; import { useSetAtom } from 'jotai'; import { useRouter } from 'next/router'; import { Suspense, useCallback } from 'react'; diff --git a/apps/web/src/hooks/use-workspace.ts b/apps/web/src/hooks/use-workspace.ts index 11df7e8932..ca215566c1 100644 --- a/apps/web/src/hooks/use-workspace.ts +++ b/apps/web/src/hooks/use-workspace.ts @@ -1,7 +1,7 @@ import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom'; import { assertExists } from '@blocksuite/global/utils'; import type { Workspace } from '@blocksuite/store'; -import { useStaticBlockSuiteWorkspace } from '@toeverything/hooks/use-block-suite-workspace'; +import { useStaticBlockSuiteWorkspace } from '@toeverything/plugin-infra/__internal__/workspace'; import type { Atom } from 'jotai'; import { atom, useAtomValue } from 'jotai'; diff --git a/apps/web/src/hooks/use-workspaces.ts b/apps/web/src/hooks/use-workspaces.ts index fdd685b62e..8493cab0aa 100644 --- a/apps/web/src/hooks/use-workspaces.ts +++ b/apps/web/src/hooks/use-workspaces.ts @@ -4,7 +4,7 @@ import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom'; import { saveWorkspaceToLocalStorage } from '@affine/workspace/local/crud'; import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils'; import { nanoid } from '@blocksuite/store'; -import { getWorkspace } from '@toeverything/hooks/use-block-suite-workspace'; +import { getWorkspace } from '@toeverything/plugin-infra/__internal__/workspace'; import { useAtomValue, useSetAtom } from 'jotai'; import { useCallback } from 'react'; diff --git a/apps/web/src/layouts/workspace-layout.tsx b/apps/web/src/layouts/workspace-layout.tsx index 2e0af3443e..dd39343fa6 100644 --- a/apps/web/src/layouts/workspace-layout.tsx +++ b/apps/web/src/layouts/workspace-layout.tsx @@ -32,8 +32,8 @@ import { useSensor, useSensors, } from '@dnd-kit/core'; -import { usePassiveWorkspaceEffect } from '@toeverything/hooks/use-block-suite-workspace'; import { useBlockSuiteWorkspaceHelper } from '@toeverything/hooks/use-block-suite-workspace-helper'; +import { usePassiveWorkspaceEffect } from '@toeverything/plugin-infra/__internal__/workspace'; import { currentPageIdAtom, currentWorkspaceIdAtom, diff --git a/apps/web/src/pages/index.tsx b/apps/web/src/pages/index.tsx index 8d08164bce..7ada99e9ef 100644 --- a/apps/web/src/pages/index.tsx +++ b/apps/web/src/pages/index.tsx @@ -2,7 +2,7 @@ import { WorkspaceFallback } from '@affine/component/workspace'; import { DebugLogger } from '@affine/debug'; import { WorkspaceSubPath } from '@affine/env/workspace'; import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom'; -import { getWorkspace } from '@toeverything/hooks/use-block-suite-workspace'; +import { getWorkspace } from '@toeverything/plugin-infra/__internal__/workspace'; import { useAtomValue } from 'jotai'; import type { NextPage } from 'next'; import { useRouter } from 'next/router'; diff --git a/packages/component/src/components/block-hub/index.tsx b/packages/component/src/components/block-hub/index.tsx index 5d54f46637..537ddaf771 100644 --- a/packages/component/src/components/block-hub/index.tsx +++ b/packages/component/src/components/block-hub/index.tsx @@ -12,7 +12,6 @@ export const BlockHubWrapper = (props: BlockHubProps): ReactElement => { const blockHub = useAtomValue(props.blockHubAtom); const ref = useRef(null); useEffect(() => { - console.log('ref.current', blockHub, ref); if (ref.current) { const div = ref.current; if (blockHub) { diff --git a/packages/component/src/components/card/workspace-card/index.tsx b/packages/component/src/components/card/workspace-card/index.tsx index 9c3f179d49..79eae830ea 100644 --- a/packages/component/src/components/card/workspace-card/index.tsx +++ b/packages/component/src/components/card/workspace-card/index.tsx @@ -8,8 +8,8 @@ import { LocalDataIcon as DefaultLocalDataIcon, LocalWorkspaceIcon as DefaultLocalWorkspaceIcon, } from '@blocksuite/icons'; -import { useStaticBlockSuiteWorkspace } from '@toeverything/hooks/use-block-suite-workspace'; import { useBlockSuiteWorkspaceName } from '@toeverything/hooks/use-block-suite-workspace-name'; +import { useStaticBlockSuiteWorkspace } from '@toeverything/plugin-infra/__internal__/workspace'; import type { FC } from 'react'; import { useCallback } from 'react'; diff --git a/packages/plugin-infra/package.json b/packages/plugin-infra/package.json index e7f6393b6a..40a138d0ab 100644 --- a/packages/plugin-infra/package.json +++ b/packages/plugin-infra/package.json @@ -18,17 +18,19 @@ "type": "./dist/type.d.ts", "import": "./dist/type.js", "require": "./dist/type.cjs" + }, + "./__internal__/workspace": { + "type": "./dist/__internal__/workspace.d.ts", + "import": "./dist/__internal__/workspace.js", + "require": "./dist/__internal__/workspace.cjs" } }, - "dependencies": { + "devDependencies": { "@blocksuite/blocks": "0.0.0-20230713062147-9eb7708c-nightly", "@blocksuite/editor": "0.0.0-20230713062147-9eb7708c-nightly", "@blocksuite/global": "0.0.0-20230713062147-9eb7708c-nightly", "@blocksuite/lit": "0.0.0-20230713062147-9eb7708c-nightly", "@blocksuite/store": "0.0.0-20230713062147-9eb7708c-nightly", - "jotai": "^2.2.2" - }, - "devDependencies": { "jotai": "^2.2.2", "vite": "^4.3.9", "vite-plugin-dts": "3.0.2" diff --git a/packages/hooks/src/use-block-suite-workspace.ts b/packages/plugin-infra/src/__internal__/workspace.ts similarity index 76% rename from packages/hooks/src/use-block-suite-workspace.ts rename to packages/plugin-infra/src/__internal__/workspace.ts index a7e90fdf50..0b3e1e7f5e 100644 --- a/packages/hooks/src/use-block-suite-workspace.ts +++ b/packages/plugin-infra/src/__internal__/workspace.ts @@ -17,10 +17,25 @@ const workspacePassiveAtomWeakMap = new WeakMap< >(); // Whether the workspace is active to use -const workspaceActiveWeakMap = new WeakMap(); +export const workspaceActiveWeakMap = new WeakMap(); // Whether the workspace has been enabled the passive effect (background) -const workspacePassiveEffectWeakMap = new WeakMap(); +export const workspacePassiveEffectWeakMap = new WeakMap(); + +export async function waitForWorkspace(workspace: Workspace) { + if (workspaceActiveWeakMap.get(workspace) !== true) { + const providers = workspace.providers.filter( + (provider): provider is ActiveDocProvider => + 'active' in provider && provider.active === true + ); + for (const provider of providers) { + provider.sync(); + // we will wait for the necessary providers to be ready + await provider.whenReady; + } + workspaceActiveWeakMap.set(workspace, true); + } +} export function getWorkspace(id: string) { if (!INTERNAL_BLOCKSUITE_HASH_MAP.has(id)) { @@ -38,18 +53,7 @@ export function getActiveBlockSuiteWorkspaceAtom( const workspace = INTERNAL_BLOCKSUITE_HASH_MAP.get(id) as Workspace; if (!workspacePassiveAtomWeakMap.has(workspace)) { const baseAtom = atom(async () => { - if (workspaceActiveWeakMap.get(workspace) !== true) { - const providers = workspace.providers.filter( - (provider): provider is ActiveDocProvider => - 'active' in provider && provider.active === true - ); - for (const provider of providers) { - provider.sync(); - // we will wait for the necessary providers to be ready - await provider.whenReady; - } - workspaceActiveWeakMap.set(workspace, true); - } + await waitForWorkspace(workspace); return workspace; }); workspacePassiveAtomWeakMap.set(workspace, baseAtom); diff --git a/packages/hooks/src/__tests__/use-block-suite-workspace.spec.ts b/packages/plugin-infra/src/__tests__/use-block-suite-workspace.spec.ts similarity index 96% rename from packages/hooks/src/__tests__/use-block-suite-workspace.spec.ts rename to packages/plugin-infra/src/__tests__/use-block-suite-workspace.spec.ts index 6981103751..ec6b4bf752 100644 --- a/packages/hooks/src/__tests__/use-block-suite-workspace.spec.ts +++ b/packages/plugin-infra/src/__tests__/use-block-suite-workspace.spec.ts @@ -3,14 +3,15 @@ */ import { Workspace } from '@blocksuite/store'; import { renderHook } from '@testing-library/react'; +import { getDefaultStore } from 'jotai/vanilla'; +import { expect, test, vi } from 'vitest'; + import { getActiveBlockSuiteWorkspaceAtom, INTERNAL_BLOCKSUITE_HASH_MAP, usePassiveWorkspaceEffect, useStaticBlockSuiteWorkspace, -} from '@toeverything/hooks/use-block-suite-workspace'; -import { getDefaultStore } from 'jotai/vanilla'; -import { expect, test, vi } from 'vitest'; +} from '../__internal__/workspace'; test('useStaticBlockSuiteWorkspace', async () => { const sync = vi.fn(); diff --git a/packages/plugin-infra/src/manager.ts b/packages/plugin-infra/src/manager.ts index 91adb14e85..82db915b9e 100644 --- a/packages/plugin-infra/src/manager.ts +++ b/packages/plugin-infra/src/manager.ts @@ -1,5 +1,8 @@ +import { assertExists } from '@blocksuite/global/utils'; +import type { Page, Workspace } from '@blocksuite/store'; import { atom, createStore } from 'jotai/vanilla'; +import { getWorkspace, waitForWorkspace } from './__internal__/workspace'; import type { AffinePlugin, Definition, ServerAdapter } from './type'; import type { Loader, PluginUIAdapter } from './type'; import type { PluginBlockSuiteAdapter } from './type'; @@ -12,8 +15,29 @@ export const rootStore = createStore(); // todo: for now every plugin is enabled by default export const affinePluginsAtom = atom>>({}); -export const currentPageIdAtom = atom(null); export const currentWorkspaceIdAtom = atom(null); +export const currentPageIdAtom = atom(null); +export const currentWorkspaceAtom = atom>(async get => { + const currentWorkspaceId = get(currentWorkspaceIdAtom); + assertExists(currentWorkspaceId, 'current workspace id'); + const workspace = getWorkspace(currentWorkspaceId); + await waitForWorkspace(workspace); + return workspace; +}); +export const currentPageAtom = atom>(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; +}); export function definePlugin( definition: Definition, diff --git a/packages/plugin-infra/vite.config.ts b/packages/plugin-infra/vite.config.ts index fc987684ba..4a6a417b33 100644 --- a/packages/plugin-infra/vite.config.ts +++ b/packages/plugin-infra/vite.config.ts @@ -13,18 +13,14 @@ export default defineConfig({ entry: { type: resolve(root, 'src/type.ts'), manager: resolve(root, 'src/manager.ts'), + '__internal__/workspace': resolve( + root, + 'src/__internal__/workspace.ts' + ), }, }, rollupOptions: { - external: [ - 'jotai', - 'jotai/vanilla', - '@blocksuite/blocks', - '@blocksuite/store', - '@blocksuite/global', - '@blocksuite/editor', - '@blocksuite/lit', - ], + external: ['react', /^jotai/, /^@blocksuite/], }, }, plugins: [ diff --git a/packages/workspace/src/utils.ts b/packages/workspace/src/utils.ts index c508f29279..a1acf6e21c 100644 --- a/packages/workspace/src/utils.ts +++ b/packages/workspace/src/utils.ts @@ -12,7 +12,7 @@ import { Generator, Workspace, } from '@blocksuite/store'; -import { INTERNAL_BLOCKSUITE_HASH_MAP } from '@toeverything/hooks/use-block-suite-workspace'; +import { INTERNAL_BLOCKSUITE_HASH_MAP } from '@toeverything/plugin-infra/__internal__/workspace'; import { createStaticStorage } from './blob/local-static-storage'; import { createSQLiteStorage } from './blob/sqlite-blob-storage';