mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-11-22 18:42:48 +03:00
refactor: move affine utils into @affine/workspace
(#2611)
This commit is contained in:
parent
6cbf310a5a
commit
ee289706ec
@ -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];
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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 };
|
@ -1,3 +0,0 @@
|
||||
import { DebugLogger } from '@affine/debug';
|
||||
|
||||
export const providerLogger = new DebugLogger('provider');
|
@ -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')(() => {
|
||||
|
@ -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(() => {
|
||||
|
@ -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 () => {
|
||||
|
@ -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[]>(
|
||||
|
@ -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
|
||||
|
@ -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() {
|
||||
|
@ -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', () => {
|
||||
|
@ -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": {
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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) => {
|
@ -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;
|
||||
|
@ -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: () => {
|
@ -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));
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user