From ee289706ecec58445fe5ca93532317fa1b404eb5 Mon Sep 17 00:00:00 2001 From: Himself65 Date: Wed, 31 May 2023 13:13:59 +0800 Subject: [PATCH] refactor: move affine utils into `@affine/workspace` (#2611) --- apps/web/src/adapters/affine/fetcher.ts | 4 +- apps/web/src/adapters/affine/index.tsx | 10 +++-- apps/web/src/atoms/public-workspace/index.ts | 2 +- apps/web/src/blocksuite/index.ts | 29 -------------- apps/web/src/blocksuite/logger.ts | 3 -- .../header-right-items/sync-user.tsx | 2 +- .../components/pure/message-center/index.tsx | 6 +-- .../affine/use-affine-refresh-auth-token.ts | 3 +- apps/web/src/hooks/affine/use-members.ts | 2 +- .../affine/use-toggle-workspace-publish.ts | 2 +- .../hooks/root/use-on-transform-workspace.ts | 2 +- apps/web/src/shared/__tests__/apis.spec.ts | 5 +-- packages/workspace/package.json | 1 - .../src/affine/__tests__/sync.spec.ts | 6 +++ .../workspace/src/affine/shared.ts | 21 +++------- packages/workspace/src/blob/index.ts | 3 +- .../src/providers/affine-download.ts | 27 ++++++------- packages/workspace/src/providers/index.ts | 39 ++++++++++++------- 18 files changed, 72 insertions(+), 95 deletions(-) delete mode 100644 apps/web/src/blocksuite/index.ts delete mode 100644 apps/web/src/blocksuite/logger.ts rename apps/web/src/shared/apis.ts => packages/workspace/src/affine/shared.ts (79%) rename apps/web/src/blocksuite/providers/affine/index.ts => packages/workspace/src/providers/affine-download.ts (61%) diff --git a/apps/web/src/adapters/affine/fetcher.ts b/apps/web/src/adapters/affine/fetcher.ts index 25e1db1cda..6efe4af2ea 100644 --- a/apps/web/src/adapters/affine/fetcher.ts +++ b/apps/web/src/adapters/affine/fetcher.ts @@ -1,13 +1,13 @@ import { Unreachable } from '@affine/env/constant'; +import { affineApis } from '@affine/workspace/affine/shared'; import { rootStore } from '@affine/workspace/atom'; +import { createAffineProviders } from '@affine/workspace/providers'; import type { AffineLegacyCloudWorkspace } from '@affine/workspace/type'; import { WorkspaceFlavour } from '@affine/workspace/type'; import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils'; import { assertExists } from '@blocksuite/store'; import { workspacesAtom } from '../../atoms'; -import { createAffineProviders } from '../../blocksuite'; -import { affineApis } from '../../shared/apis'; type Query = (typeof QueryKey)[keyof typeof QueryKey]; diff --git a/apps/web/src/adapters/affine/index.tsx b/apps/web/src/adapters/affine/index.tsx index fbaa5f898f..1ee1177237 100644 --- a/apps/web/src/adapters/affine/index.tsx +++ b/apps/web/src/adapters/affine/index.tsx @@ -14,8 +14,13 @@ import { setLoginStorage, SignMethod, } from '@affine/workspace/affine/login'; +import { affineApis, affineAuth } from '@affine/workspace/affine/shared'; import { rootStore, rootWorkspacesMetadataAtom } from '@affine/workspace/atom'; -import { createIndexedDBBackgroundProvider } from '@affine/workspace/providers'; +import { + createAffineProviders, + createIndexedDBBackgroundProvider, +} from '@affine/workspace/providers'; +import { createAffineDownloadProvider } from '@affine/workspace/providers'; import type { AffineLegacyCloudWorkspace } from '@affine/workspace/type'; import { LoadPriority, @@ -32,12 +37,9 @@ import { Suspense, useEffect } from 'react'; import { mutate } from 'swr'; import { z } from 'zod'; -import { createAffineProviders } from '../../blocksuite'; -import { createAffineDownloadProvider } from '../../blocksuite/providers/affine'; import { PageLoading } from '../../components/pure/loading'; import { useAffineRefreshAuthToken } from '../../hooks/affine/use-affine-refresh-auth-token'; import { BlockSuiteWorkspace } from '../../shared'; -import { affineApis, affineAuth } from '../../shared/apis'; import { toast } from '../../utils'; import { BlockSuitePageList, diff --git a/apps/web/src/atoms/public-workspace/index.ts b/apps/web/src/atoms/public-workspace/index.ts index 2b9b412ff5..68ce69bce3 100644 --- a/apps/web/src/atoms/public-workspace/index.ts +++ b/apps/web/src/atoms/public-workspace/index.ts @@ -1,12 +1,12 @@ import type { BlockSuiteFeatureFlags } from '@affine/env'; import { config } from '@affine/env'; +import { affineApis } from '@affine/workspace/affine/shared'; import type { AffinePublicWorkspace } from '@affine/workspace/type'; import { WorkspaceFlavour } from '@affine/workspace/type'; import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils'; import { atom } from 'jotai'; import { BlockSuiteWorkspace } from '../../shared'; -import { affineApis } from '../../shared/apis'; function createPublicWorkspace( workspaceId: string, diff --git a/apps/web/src/blocksuite/index.ts b/apps/web/src/blocksuite/index.ts deleted file mode 100644 index eae4335c64..0000000000 --- a/apps/web/src/blocksuite/index.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { config } from '@affine/env'; -import { - createIndexedDBDownloadProvider, - createLocalProviders, -} from '@affine/workspace/providers'; -import { - createAffineWebSocketProvider, - createBroadCastChannelProvider, -} from '@affine/workspace/providers'; -import type { Provider } from '@affine/workspace/type'; - -import type { BlockSuiteWorkspace } from '../shared'; -import { createAffineDownloadProvider } from './providers/affine'; - -export const createAffineProviders = ( - blockSuiteWorkspace: BlockSuiteWorkspace -): Provider[] => { - return ( - [ - createAffineDownloadProvider(blockSuiteWorkspace), - createAffineWebSocketProvider(blockSuiteWorkspace), - config.enableBroadCastChannelProvider && - createBroadCastChannelProvider(blockSuiteWorkspace), - createIndexedDBDownloadProvider(blockSuiteWorkspace), - ] as any[] - ).filter(v => Boolean(v)); -}; - -export { createLocalProviders }; diff --git a/apps/web/src/blocksuite/logger.ts b/apps/web/src/blocksuite/logger.ts deleted file mode 100644 index 51fc38af06..0000000000 --- a/apps/web/src/blocksuite/logger.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { DebugLogger } from '@affine/debug'; - -export const providerLogger = new DebugLogger('provider'); diff --git a/apps/web/src/components/blocksuite/workspace-header/header-right-items/sync-user.tsx b/apps/web/src/components/blocksuite/workspace-header/header-right-items/sync-user.tsx index 71f3afb7fa..75d2dc0db6 100644 --- a/apps/web/src/components/blocksuite/workspace-header/header-right-items/sync-user.tsx +++ b/apps/web/src/components/blocksuite/workspace-header/header-right-items/sync-user.tsx @@ -6,6 +6,7 @@ import { setLoginStorage, SignMethod, } from '@affine/workspace/affine/login'; +import { affineAuth } from '@affine/workspace/affine/shared'; import type { LocalWorkspace } from '@affine/workspace/type'; import { WorkspaceFlavour } from '@affine/workspace/type'; import { @@ -20,7 +21,6 @@ import React, { useEffect, useState } from 'react'; import { useCurrentWorkspace } from '../../../../hooks/current/use-current-workspace'; import { useTransformWorkspace } from '../../../../hooks/use-transform-workspace'; import type { AffineOfficialWorkspace } from '../../../../shared'; -import { affineAuth } from '../../../../shared/apis'; import { TransformWorkspaceToAffineModal } from '../../../affine/transform-workspace-to-affine-modal'; const IconWrapper = styled('div')(() => { diff --git a/apps/web/src/components/pure/message-center/index.tsx b/apps/web/src/components/pure/message-center/index.tsx index 8734c2485a..db8d71f30f 100644 --- a/apps/web/src/components/pure/message-center/index.tsx +++ b/apps/web/src/components/pure/message-center/index.tsx @@ -1,10 +1,10 @@ import { MessageCode, Messages } from '@affine/env/constant'; import { setLoginStorage, SignMethod } from '@affine/workspace/affine/login'; -import type React from 'react'; +import { affineAuth } from '@affine/workspace/affine/shared'; +import type { FC } from 'react'; import { memo, useEffect, useState } from 'react'; import { useAffineLogOut } from '../../../hooks/affine/use-affine-log-out'; -import { affineAuth } from '../../../shared/apis'; import { toast } from '../../../utils'; declare global { @@ -15,7 +15,7 @@ declare global { } } -export const MessageCenter: React.FC = memo(function MessageCenter() { +export const MessageCenter: FC = memo(function MessageCenter() { const [popup, setPopup] = useState(false); const onLogout = useAffineLogOut(); useEffect(() => { diff --git a/apps/web/src/hooks/affine/use-affine-refresh-auth-token.ts b/apps/web/src/hooks/affine/use-affine-refresh-auth-token.ts index 42324e6886..501cbb5403 100644 --- a/apps/web/src/hooks/affine/use-affine-refresh-auth-token.ts +++ b/apps/web/src/hooks/affine/use-affine-refresh-auth-token.ts @@ -6,10 +6,9 @@ import { setLoginStorage, storageChangeSlot, } from '@affine/workspace/affine/login'; +import { affineAuth } from '@affine/workspace/affine/shared'; import useSWR from 'swr'; -import { affineAuth } from '../../shared/apis'; - const logger = new DebugLogger('auth-token'); const revalidate = async () => { diff --git a/apps/web/src/hooks/affine/use-members.ts b/apps/web/src/hooks/affine/use-members.ts index cdd68f98b9..b266d1ead8 100644 --- a/apps/web/src/hooks/affine/use-members.ts +++ b/apps/web/src/hooks/affine/use-members.ts @@ -1,9 +1,9 @@ import type { Member } from '@affine/workspace/affine/api'; +import { affineApis } from '@affine/workspace/affine/shared'; import { useCallback } from 'react'; import useSWR from 'swr'; import { QueryKey } from '../../adapters/affine/fetcher'; -import { affineApis } from '../../shared/apis'; export function useMembers(workspaceId: string) { const { data, mutate } = useSWR( diff --git a/apps/web/src/hooks/affine/use-toggle-workspace-publish.ts b/apps/web/src/hooks/affine/use-toggle-workspace-publish.ts index 2ec5c689fe..fea2f02847 100644 --- a/apps/web/src/hooks/affine/use-toggle-workspace-publish.ts +++ b/apps/web/src/hooks/affine/use-toggle-workspace-publish.ts @@ -1,10 +1,10 @@ +import { affineApis } from '@affine/workspace/affine/shared'; import { rootStore, rootWorkspacesMetadataAtom } from '@affine/workspace/atom'; import type { AffineLegacyCloudWorkspace } from '@affine/workspace/type'; import { useCallback } from 'react'; import useSWR from 'swr'; import { QueryKey } from '../../adapters/affine/fetcher'; -import { affineApis } from '../../shared/apis'; export function useToggleWorkspacePublish( workspace: AffineLegacyCloudWorkspace diff --git a/apps/web/src/hooks/root/use-on-transform-workspace.ts b/apps/web/src/hooks/root/use-on-transform-workspace.ts index 1e7239880d..075214d4d0 100644 --- a/apps/web/src/hooks/root/use-on-transform-workspace.ts +++ b/apps/web/src/hooks/root/use-on-transform-workspace.ts @@ -6,13 +6,13 @@ import { SignMethod, storageChangeSlot, } from '@affine/workspace/affine/login'; +import { affineAuth } from '@affine/workspace/affine/shared'; import { rootCurrentWorkspaceIdAtom } from '@affine/workspace/atom'; import type { WorkspaceRegistry } from '@affine/workspace/type'; import { WorkspaceFlavour } from '@affine/workspace/type'; import { useSetAtom } from 'jotai'; import { useCallback } from 'react'; -import { affineAuth } from '../../shared/apis'; import { useTransformWorkspace } from '../use-transform-workspace'; export function useOnTransformWorkspace() { diff --git a/apps/web/src/shared/__tests__/apis.spec.ts b/apps/web/src/shared/__tests__/apis.spec.ts index e72d8f7797..fec8972cf0 100644 --- a/apps/web/src/shared/__tests__/apis.spec.ts +++ b/apps/web/src/shared/__tests__/apis.spec.ts @@ -2,16 +2,15 @@ * @vitest-environment node */ import { loginResponseSchema } from '@affine/workspace/affine/login'; +import type { affineApis as API } from '@affine/workspace/affine/shared'; import { beforeAll, describe, expect, it, vi } from 'vitest'; -import type { affineApis as API } from '../apis'; - let affineApis: typeof API; beforeAll(async () => { // @ts-expect-error globalThis.window = undefined; - affineApis = (await import('../apis')).affineApis; + affineApis = (await import('@affine/workspace/affine/shared')).affineApis; }); describe('apis', () => { diff --git a/packages/workspace/package.json b/packages/workspace/package.json index 6f78d70ac8..618bceaff6 100644 --- a/packages/workspace/package.json +++ b/packages/workspace/package.json @@ -10,7 +10,6 @@ "./providers": "./src/providers/index.ts", "./affine/*": "./src/affine/*.ts", "./affine/api": "./src/affine/api/index.ts", - "./affine/sync": "./src/affine/sync.js", "./affine/keck": "./src/affine/keck/index.ts" }, "peerDependencies": { diff --git a/packages/workspace/src/affine/__tests__/sync.spec.ts b/packages/workspace/src/affine/__tests__/sync.spec.ts index 1edb44c3ca..e43ffe0942 100644 --- a/packages/workspace/src/affine/__tests__/sync.spec.ts +++ b/packages/workspace/src/affine/__tests__/sync.spec.ts @@ -21,6 +21,12 @@ import { uuidv4, Workspace as BlockSuiteWorkspace } from '@blocksuite/store'; import { beforeEach, describe, expect, test, vi } from 'vitest'; import { WebSocket } from 'ws'; +declare module '@blocksuite/store' { + interface PageMeta { + foo: string; + } +} + // @ts-expect-error globalThis.WebSocket = WebSocket; diff --git a/apps/web/src/shared/apis.ts b/packages/workspace/src/affine/shared.ts similarity index 79% rename from apps/web/src/shared/apis.ts rename to packages/workspace/src/affine/shared.ts index 4ef4041158..e90e2cc52e 100644 --- a/apps/web/src/shared/apis.ts +++ b/packages/workspace/src/affine/shared.ts @@ -1,17 +1,10 @@ -import { DebugLogger } from '@affine/debug'; import { prefixUrl } from '@affine/env'; -import { - createUserApis, - createWorkspaceApis, -} from '@affine/workspace/affine/api'; -import { currentAffineUserAtom } from '@affine/workspace/affine/atom'; -import type { LoginResponse } from '@affine/workspace/affine/login'; -import { - createAffineAuth, - parseIdToken, - setLoginStorage, -} from '@affine/workspace/affine/login'; -import { rootStore } from '@affine/workspace/atom'; + +import { rootStore } from '../atom'; +import { createUserApis, createWorkspaceApis } from './api/index'; +import { currentAffineUserAtom } from './atom'; +import type { LoginResponse } from './login'; +import { createAffineAuth, parseIdToken, setLoginStorage } from './login'; export const affineAuth = createAffineAuth(prefixUrl); const affineApis = {} as ReturnType & @@ -19,8 +12,6 @@ const affineApis = {} as ReturnType & Object.assign(affineApis, createUserApis(prefixUrl)); Object.assign(affineApis, createWorkspaceApis(prefixUrl)); -const _debugLogger = new DebugLogger('affine-debug-apis'); - if (!globalThis.AFFINE_APIS) { globalThis.AFFINE_APIS = affineApis; globalThis.setLogin = (response: LoginResponse) => { diff --git a/packages/workspace/src/blob/index.ts b/packages/workspace/src/blob/index.ts index d6589437e7..eb0d749cb3 100644 --- a/packages/workspace/src/blob/index.ts +++ b/packages/workspace/src/blob/index.ts @@ -1,10 +1,9 @@ +import type { createWorkspaceApis } from '@affine/workspace/affine/api'; import type { BlobStorage } from '@blocksuite/store'; import { createIndexeddbStorage } from '@blocksuite/store'; import { openDB } from 'idb'; import type { DBSchema } from 'idb/build/entry'; -import type { createWorkspaceApis } from '../affine/api'; - type UploadingBlob = { key: string; arrayBuffer: ArrayBuffer; diff --git a/apps/web/src/blocksuite/providers/affine/index.ts b/packages/workspace/src/providers/affine-download.ts similarity index 61% rename from apps/web/src/blocksuite/providers/affine/index.ts rename to packages/workspace/src/providers/affine-download.ts index d4edad15d6..ca6a45bce3 100644 --- a/apps/web/src/blocksuite/providers/affine/index.ts +++ b/packages/workspace/src/providers/affine-download.ts @@ -1,14 +1,15 @@ -import type { AffineDownloadProvider } from '@affine/workspace/type'; -import { assertExists } from '@blocksuite/store'; +import { DebugLogger } from '@affine/debug'; +import { assertExists, Workspace } from '@blocksuite/store'; -import { BlockSuiteWorkspace } from '../../../shared'; -import { affineApis } from '../../../shared/apis'; -import { providerLogger } from '../../logger'; +import { affineApis } from '../affine/shared'; +import type { AffineDownloadProvider } from '../type'; const hashMap = new Map(); +const logger = new DebugLogger('affine:workspace:download-provider'); + export const createAffineDownloadProvider = ( - blockSuiteWorkspace: BlockSuiteWorkspace + blockSuiteWorkspace: Workspace ): AffineDownloadProvider => { assertExists(blockSuiteWorkspace.id); const id = blockSuiteWorkspace.id; @@ -22,10 +23,10 @@ export const createAffineDownloadProvider = ( }, callbacks, connect: () => { - providerLogger.info('connect download provider', id); + logger.info('connect download provider', id); if (hashMap.has(id)) { - providerLogger.debug('applyUpdate'); - BlockSuiteWorkspace.Y.applyUpdate( + logger.debug('applyUpdate'); + Workspace.Y.applyUpdate( blockSuiteWorkspace.doc, new Uint8Array(hashMap.get(id) as ArrayBuffer) ); @@ -37,8 +38,8 @@ export const createAffineDownloadProvider = ( .downloadWorkspace(id, false) .then(binary => { hashMap.set(id, binary); - providerLogger.debug('applyUpdate'); - BlockSuiteWorkspace.Y.applyUpdate( + logger.debug('applyUpdate'); + Workspace.Y.applyUpdate( blockSuiteWorkspace.doc, new Uint8Array(binary) ); @@ -46,11 +47,11 @@ export const createAffineDownloadProvider = ( callbacks.forEach(cb => cb()); }) .catch(e => { - providerLogger.error('downloadWorkspace', e); + logger.error('downloadWorkspace', e); }); }, disconnect: () => { - providerLogger.info('disconnect download provider', id); + logger.info('disconnect download provider', id); connected = false; }, cleanup: () => { diff --git a/packages/workspace/src/providers/index.ts b/packages/workspace/src/providers/index.ts index af3d3da241..f2f21e1d49 100644 --- a/packages/workspace/src/providers/index.ts +++ b/packages/workspace/src/providers/index.ts @@ -1,17 +1,4 @@ import { config, websocketPrefixUrl } from '@affine/env'; -import { KeckProvider } from '@affine/workspace/affine/keck'; -import { - getLoginStorage, - storageChangeSlot, -} from '@affine/workspace/affine/login'; -import type { - AffineWebSocketProvider, - LocalIndexedDBBackgroundProvider, - LocalIndexedDBDownloadProvider, - Provider, - SQLiteProvider, -} from '@affine/workspace/type'; -import { CallbackSet } from '@affine/workspace/utils'; import type { BlobManager, Disposable } from '@blocksuite/store'; import { assertExists, @@ -23,6 +10,17 @@ import { EarlyDisconnectError, } from '@toeverything/y-indexeddb'; +import { KeckProvider } from '../affine/keck'; +import { getLoginStorage, storageChangeSlot } from '../affine/login'; +import type { + AffineWebSocketProvider, + LocalIndexedDBBackgroundProvider, + LocalIndexedDBDownloadProvider, + Provider, + SQLiteProvider, +} from '../type'; +import { CallbackSet } from '../utils'; +import { createAffineDownloadProvider } from './affine-download'; import { createBroadCastChannelProvider } from './broad-cast-channel'; import { localProviderLogger as logger } from './logger'; @@ -258,6 +256,7 @@ const createSQLiteProvider = ( }; export { + createAffineDownloadProvider, createAffineWebSocketProvider, createBroadCastChannelProvider, createIndexedDBBackgroundProvider, @@ -278,3 +277,17 @@ export const createLocalProviders = ( ] as any[] ).filter(v => Boolean(v)); }; + +export const createAffineProviders = ( + blockSuiteWorkspace: BlockSuiteWorkspace +): Provider[] => { + return ( + [ + createAffineDownloadProvider(blockSuiteWorkspace), + createAffineWebSocketProvider(blockSuiteWorkspace), + config.enableBroadCastChannelProvider && + createBroadCastChannelProvider(blockSuiteWorkspace), + createIndexedDBDownloadProvider(blockSuiteWorkspace), + ] as any[] + ).filter(v => Boolean(v)); +};