Skip to content

Commit

Permalink
refactor(workspace): split workspace interface and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Jan 2, 2024
1 parent d653a1b commit 4b36568
Show file tree
Hide file tree
Showing 76 changed files with 319 additions and 159 deletions.
7 changes: 6 additions & 1 deletion .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ mod:plugin-cli:
mod:workspace:
- changed-files:
- any-glob-to-any-file:
- 'packages/frontend/workspace/**/*'
- 'packages/common/workspace/**/*'

mod:workspace-impl:
- changed-files:
- any-glob-to-any-file:
- 'packages/frontend/workspace-impl/**/*'

mod:i18n:
- changed-files:
Expand Down
File renamed without changes.
24 changes: 24 additions & 0 deletions packages/common/workspace/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@affine/workspace",
"private": true,
"main": "./src/index.ts",
"exports": {
".": "./src/index.ts"
},
"peerDependencies": {
"@blocksuite/blocks": "*",
"@blocksuite/global": "*",
"@blocksuite/store": "*"
},
"dependencies": {
"@affine/debug": "workspace:*",
"@affine/env": "workspace:*",
"@toeverything/infra": "workspace:*",
"lodash-es": "^4.17.21",
"yjs": "^13.6.10"
},
"devDependencies": {
"vitest": "1.1.0"
},
"version": "0.11.0"
}
File renamed without changes.
7 changes: 7 additions & 0 deletions packages/common/workspace/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './engine';
export * from './factory';
export * from './global-schema';
export * from './list';
export * from './manager';
export * from './metadata';
export * from './workspace';
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { applyUpdate, encodeStateAsUpdate } from 'yjs';

import type { BlobStorage } from '.';
import type { WorkspaceFactory } from './factory';
import { LOCAL_WORKSPACE_LOCAL_STORAGE_KEY } from './impl/local/consts';
import type { WorkspaceList } from './list';
import type { WorkspaceMetadata } from './metadata';
import { WorkspacePool } from './pool';
Expand Down Expand Up @@ -151,21 +150,6 @@ export class WorkspaceManager {
return factory.getWorkspaceBlob(metadata.id, blobKey);
}

/**
* a hack for directly add local workspace to workspace list
* Used after copying sqlite database file to appdata folder
*/
_addLocalWorkspace(id: string) {
const allWorkspaceIDs: string[] = JSON.parse(
localStorage.getItem(LOCAL_WORKSPACE_LOCAL_STORAGE_KEY) ?? '[]'
);
allWorkspaceIDs.push(id);
localStorage.setItem(
LOCAL_WORKSPACE_LOCAL_STORAGE_KEY,
JSON.stringify(allWorkspaceIDs)
);
}

private open(metadata: WorkspaceMetadata) {
logger.info(`open workspace [${metadata.flavour}] ${metadata.id} `);
const factory = this.factories.find(x => x.name === metadata.flavour);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/frontend/component/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{
"path": "../../frontend/electron-api"
},
{ "path": "../../frontend/workspace" },
{ "path": "../../common/workspace" },
{
"path": "../../common/debug"
},
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@affine/i18n": "workspace:*",
"@affine/templates": "workspace:*",
"@affine/workspace": "workspace:*",
"@affine/workspace-impl": "workspace:*",
"@blocksuite/block-std": "0.11.0-nightly-202312220916-e3abcbb",
"@blocksuite/blocks": "0.11.0-nightly-202312220916-e3abcbb",
"@blocksuite/global": "0.11.0-nightly-202312220916-e3abcbb",
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/core/src/bootstrap/first-app-data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DebugLogger } from '@affine/debug';
import { DEFAULT_WORKSPACE_NAME } from '@affine/env/constant';
import { WorkspaceFlavour } from '@affine/env/workspace';
import { workspaceManager } from '@affine/workspace';
import { workspaceManager } from '@affine/workspace-impl';
import { getCurrentStore } from '@toeverything/infra/atom';
import {
buildShowcaseWorkspace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { DebugLogger } from '@affine/debug';
import { apis } from '@affine/electron-api';
import { WorkspaceFlavour } from '@affine/env/workspace';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { _addLocalWorkspace } from '@affine/workspace-impl';
import { getCurrentStore } from '@toeverything/infra/atom';
import {
buildShowcaseWorkspace,
Expand Down Expand Up @@ -119,7 +120,7 @@ export const CreateWorkspaceModal = ({
setStep(undefined);
const result = await apis.dialog.loadDBFile();
if (result.workspaceId && !canceled) {
workspaceManager._addLocalWorkspace(result.workspaceId);
_addLocalWorkspace(result.workspaceId);
onCreate(result.workspaceId);
} else if (result.error || result.canceled) {
if (result.error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import {
listHistoryQuery,
recoverDocMutation,
} from '@affine/graphql';
import {
createAffineCloudBlobStorage,
globalBlockSuiteSchema,
} from '@affine/workspace';
import { globalBlockSuiteSchema } from '@affine/workspace';
import { createAffineCloudBlobStorage } from '@affine/workspace-impl';
import { assertEquals } from '@blocksuite/global/utils';
import { Workspace } from '@blocksuite/store';
import { revertUpdate } from '@toeverything/y-indexeddb';
Expand Down
7 changes: 2 additions & 5 deletions packages/frontend/core/src/modules/workspace/atoms.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { DebugLogger } from '@affine/debug';
import {
type Workspace,
workspaceManager,
type WorkspaceMetadata,
} from '@affine/workspace';
import type { Workspace, WorkspaceMetadata } from '@affine/workspace';
import { workspaceManager } from '@affine/workspace-impl';
import { atom } from 'jotai';
import { atomWithObservable } from 'jotai/utils';
import { Observable } from 'rxjs';
Expand Down
4 changes: 2 additions & 2 deletions packages/frontend/core/src/pages/share/share-detail-page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { MainContainer } from '@affine/component/workspace';
import { DebugLogger } from '@affine/debug';
import { fetchWithTraceReport } from '@affine/graphql';
import { globalBlockSuiteSchema } from '@affine/workspace';
import {
createAffineCloudBlobStorage,
createStaticBlobStorage,
globalBlockSuiteSchema,
} from '@affine/workspace';
} from '@affine/workspace-impl';
import { assertExists } from '@blocksuite/global/utils';
import { type Page, Workspace } from '@blocksuite/store';
import { noop } from 'foxact/noop';
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/core/src/providers/session-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { pushNotificationAtom } from '@affine/component/notification-center';
import { useAsyncCallback } from '@affine/core/hooks/affine-async-hooks';
import { affine } from '@affine/electron-api';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { CLOUD_WORKSPACE_CHANGED_BROADCAST_CHANNEL_KEY } from '@affine/workspace';
import { CLOUD_WORKSPACE_CHANGED_BROADCAST_CHANNEL_KEY } from '@affine/workspace-impl';
import { useAtom, useSetAtom } from 'jotai';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { SessionProvider, useSession } from 'next-auth/react';
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/core/src/utils/cloud-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
TRACE_ID_BYTES,
traceReporter,
} from '@affine/graphql';
import { CLOUD_WORKSPACE_CHANGED_BROADCAST_CHANNEL_KEY } from '@affine/workspace';
import { CLOUD_WORKSPACE_CHANGED_BROADCAST_CHANNEL_KEY } from '@affine/workspace-impl';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { signIn, signOut } from 'next-auth/react';

Expand Down
5 changes: 4 additions & 1 deletion packages/frontend/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"path": "../../frontend/i18n"
},
{
"path": "../../frontend/workspace"
"path": "../../common/workspace"
},
{
"path": "../../frontend/workspace-impl"
},
{
"path": "../../frontend/electron-api"
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/workspace-impl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
Original file line number Diff line number Diff line change
@@ -1,48 +1,33 @@
{
"name": "@affine/workspace",
"name": "@affine/workspace-impl",
"private": true,
"main": "./src/index.ts",
"exports": {
".": "./src/index.ts",
"./atom": "./src/atom.ts",
"./affine/*": "./src/affine/*.ts"
".": "./src/index.ts"
},
"peerDependencies": {
"@blocksuite/blocks": "*",
"@blocksuite/global": "*",
"@blocksuite/store": "*"
},
"dependencies": {
"@affine-test/fixtures": "workspace:*",
"@affine/debug": "workspace:*",
"@affine/electron-api": "workspace:*",
"@affine/env": "workspace:*",
"@affine/graphql": "workspace:*",
"@affine/workspace": "workspace:*",
"@toeverything/infra": "workspace:*",
"async-call-rpc": "^6.3.1",
"idb": "^8.0.0",
"idb-keyval": "^6.2.1",
"is-svg": "^5.0.0",
"jotai": "^2.5.1",
"js-base64": "^3.7.5",
"ky": "^1.0.1",
"lib0": "^0.2.87",
"lodash-es": "^4.17.21",
"nanoid": "^5.0.3",
"next-auth": "^4.24.5",
"react": "18.2.0",
"react-dom": "18.2.0",
"rxjs": "^7.8.1",
"socket.io-client": "^4.7.2",
"swr": "2.2.4",
"valtio": "^1.11.2",
"y-protocols": "^1.0.6",
"y-provider": "workspace:*",
"yjs": "^13.6.10",
"zod": "^3.22.4"
"yjs": "^13.6.10"
},
"devDependencies": {
"@testing-library/react": "^14.0.0",
"@types/ws": "^8.5.7",
"fake-indexeddb": "^5.0.0",
"vitest": "1.1.1",
"ws": "^8.14.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { DebugLogger } from '@affine/debug';
import type { AwarenessProvider } from '@affine/workspace';
import {
applyAwarenessUpdate,
type Awareness,
encodeAwarenessUpdate,
removeAwarenessStates,
} from 'y-protocols/awareness';

import type { AwarenessProvider } from '../../engine/awareness';
import { getIoManager } from '../../utils/affine-io';
import { base64ToUint8Array, uint8ArrayToBase64 } from '../../utils/base64';
import { getIoManager } from '../utils/affine-io';
import { base64ToUint8Array, uint8ArrayToBase64 } from '../utils/base64';

const logger = new DebugLogger('affine:awareness:socketio');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
setBlobMutation,
} from '@affine/graphql';
import { fetcher } from '@affine/graphql';
import type { BlobStorage } from '@affine/workspace';

import type { BlobStorage } from '../../engine/blob';
import { bufferToBlob } from '../../utils/buffer-to-blob';
import { bufferToBlob } from '../utils/buffer-to-blob';

export const createAffineCloudBlobStorage = (
workspaceId: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {
getWorkspacesQuery,
} from '@affine/graphql';
import { fetcher } from '@affine/graphql';
import type { WorkspaceListProvider } from '@affine/workspace';
import { globalBlockSuiteSchema } from '@affine/workspace';
import { Workspace as BlockSuiteWorkspace } from '@blocksuite/store';
import { difference } from 'lodash-es';
import { nanoid } from 'nanoid';
import { applyUpdate, encodeStateAsUpdate } from 'yjs';

import { globalBlockSuiteSchema } from '../../global-schema';
import type { WorkspaceListProvider } from '../../list';
import { createLocalBlobStorage } from '../local/blob';
import { createLocalStorage } from '../local/sync';
import { CLOUD_WORKSPACE_CHANGED_BROADCAST_CHANNEL_KEY } from './consts';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DebugLogger } from '@affine/debug';
import { fetchWithTraceReport } from '@affine/graphql';
import type { SyncStorage } from '@affine/workspace';

import type { SyncStorage } from '../../../engine/sync';
import { getIoManager } from '../../../utils/affine-io';
import { base64ToUint8Array, uint8ArrayToBase64 } from '../../../utils/base64';
import { getIoManager } from '../../utils/affine-io';
import { base64ToUint8Array, uint8ArrayToBase64 } from '../../utils/base64';
import { MultipleBatchSyncSender } from './batch-sync-sender';

const logger = new DebugLogger('affine:storage:socketio');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { setupEditorFlags } from '@affine/env/global';
import type { WorkspaceFactory } from '@affine/workspace';
import { BlobEngine, SyncEngine, WorkspaceEngine } from '@affine/workspace';
import { globalBlockSuiteSchema } from '@affine/workspace';
import { Workspace } from '@affine/workspace';
import { Workspace as BlockSuiteWorkspace } from '@blocksuite/store';
import { nanoid } from 'nanoid';

import { BlobEngine, SyncEngine, WorkspaceEngine } from '../../engine';
import type { WorkspaceFactory } from '../../factory';
import { globalBlockSuiteSchema } from '../../global-schema';
import { Workspace } from '../../workspace';
import { createBroadcastChannelAwarenessProvider } from '../local/awareness';
import { createLocalBlobStorage } from '../local/blob';
import { createStaticBlobStorage } from '../local/blob-static';
Expand Down
41 changes: 41 additions & 0 deletions packages/frontend/workspace-impl/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { WorkspaceList, WorkspaceManager } from '@affine/workspace';

import {
cloudWorkspaceFactory,
createCloudWorkspaceListProvider,
} from './cloud';
import {
createLocalWorkspaceListProvider,
LOCAL_WORKSPACE_LOCAL_STORAGE_KEY,
localWorkspaceFactory,
} from './local';

const list = new WorkspaceList([
createLocalWorkspaceListProvider(),
createCloudWorkspaceListProvider(),
]);

export const workspaceManager = new WorkspaceManager(list, [
localWorkspaceFactory,
cloudWorkspaceFactory,
]);

(window as any).workspaceManager = workspaceManager;

export * from './cloud';
export * from './local';

/**
* a hack for directly add local workspace to workspace list
* Used after copying sqlite database file to appdata folder
*/
export function _addLocalWorkspace(id: string) {
const allWorkspaceIDs: string[] = JSON.parse(
localStorage.getItem(LOCAL_WORKSPACE_LOCAL_STORAGE_KEY) ?? '[]'
);
allWorkspaceIDs.push(id);
localStorage.setItem(
LOCAL_WORKSPACE_LOCAL_STORAGE_KEY,
JSON.stringify(allWorkspaceIDs)
);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'fake-indexeddb/auto';

import { SyncEngine, SyncEngineStep, SyncPeerStep } from '@affine/workspace';
import { __unstableSchemas, AffineSchemas } from '@blocksuite/blocks/models';
import { Schema, Workspace } from '@blocksuite/store';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { Doc } from 'yjs';

import { createIndexedDBStorage } from '../../../impl/local/sync-indexeddb';
import { SyncEngine, SyncEngineStep, SyncPeerStep } from '../';
import { createIndexedDBStorage } from '..';
import { createTestStorage } from './test-storage';

const schema = new Schema();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'fake-indexeddb/auto';

import { SyncPeer, SyncPeerStep } from '@affine/workspace';
import { __unstableSchemas, AffineSchemas } from '@blocksuite/blocks/models';
import { Schema, Workspace } from '@blocksuite/store';
import { beforeEach, describe, expect, test, vi } from 'vitest';

import { createIndexedDBStorage } from '../../../impl/local/sync-indexeddb';
import { SyncPeer, SyncPeerStep } from '../';
import { createIndexedDBStorage } from '..';

const schema = new Schema();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SyncStorage } from '..';
import type { SyncStorage } from '@affine/workspace';

export function createTestStorage(origin: SyncStorage) {
const controler = {
Expand Down
Loading

0 comments on commit 4b36568

Please sign in to comment.