refactor: move affine utils into @affine/workspace (#2611)

This commit is contained in:
Himself65 2023-05-31 13:13:59 +08:00 committed by GitHub
parent 6cbf310a5a
commit ee289706ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 72 additions and 95 deletions

View File

@ -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];

View File

@ -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,

View File

@ -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,

View File

@ -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 };

View File

@ -1,3 +0,0 @@
import { DebugLogger } from '@affine/debug';
export const providerLogger = new DebugLogger('provider');

View File

@ -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')(() => {

View File

@ -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(() => {

View File

@ -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 () => {

View File

@ -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<Member[]>(

View File

@ -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

View File

@ -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() {

View File

@ -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', () => {

View File

@ -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": {

View File

@ -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;

View File

@ -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<typeof createUserApis> &
@ -19,8 +12,6 @@ const affineApis = {} as ReturnType<typeof createUserApis> &
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) => {

View File

@ -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;

View File

@ -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<string, ArrayBuffer>();
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: () => {

View File

@ -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));
};