From 876d4d9c947f4d31395eed14e461f44e4dcf585e Mon Sep 17 00:00:00 2001 From: EYHN Date: Thu, 23 Jan 2025 11:39:13 +0800 Subject: [PATCH] fix(android): fix android nbstore provider (#9865) --- packages/frontend/apps/android/package.json | 1 + packages/frontend/apps/android/src/app.tsx | 21 +++++++++++++++++- packages/frontend/apps/android/src/nbstore.ts | 22 +++++++++++++++++++ packages/frontend/apps/android/tsconfig.json | 1 + tools/utils/src/workspace.gen.ts | 1 + yarn.lock | 1 + 6 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 packages/frontend/apps/android/src/nbstore.ts diff --git a/packages/frontend/apps/android/package.json b/packages/frontend/apps/android/package.json index ab14a474fb..1dd529a2b2 100644 --- a/packages/frontend/apps/android/package.json +++ b/packages/frontend/apps/android/package.json @@ -11,6 +11,7 @@ "@affine/component": "workspace:*", "@affine/core": "workspace:*", "@affine/i18n": "workspace:*", + "@affine/nbstore": "workspace:*", "@blocksuite/affine": "workspace:*", "@blocksuite/icons": "2.2.2", "@capacitor/android": "^7.0.0", diff --git a/packages/frontend/apps/android/src/app.tsx b/packages/frontend/apps/android/src/app.tsx index 9c662d47bb..4ff63212a2 100644 --- a/packages/frontend/apps/android/src/app.tsx +++ b/packages/frontend/apps/android/src/app.tsx @@ -5,10 +5,15 @@ import { router } from '@affine/core/mobile/router'; import { configureCommonModules } from '@affine/core/modules'; import { I18nProvider } from '@affine/core/modules/i18n'; import { LifecycleService } from '@affine/core/modules/lifecycle'; -import { configureLocalStorageStateStorageImpls } from '@affine/core/modules/storage'; +import { + configureLocalStorageStateStorageImpls, + NbstoreProvider, +} from '@affine/core/modules/storage'; import { configureBrowserWorkbenchModule } from '@affine/core/modules/workbench'; import { configureBrowserWorkspaceFlavours } from '@affine/core/modules/workspace-engine'; +import { WorkerClient } from '@affine/nbstore/worker/client'; import { Framework, FrameworkRoot, getCurrentStore } from '@toeverything/infra'; +import { OpClient } from '@toeverything/infra/op'; import { Suspense } from 'react'; import { RouterProvider } from 'react-router-dom'; @@ -22,6 +27,20 @@ configureBrowserWorkbenchModule(framework); configureLocalStorageStateStorageImpls(framework); configureBrowserWorkspaceFlavours(framework); configureMobileModules(framework); +framework.impl(NbstoreProvider, { + openStore(_key, options) { + const worker = new Worker( + new URL(/* webpackChunkName: "nbstore" */ './nbstore.ts', import.meta.url) + ); + const client = new WorkerClient(new OpClient(worker), options); + return { + store: client, + dispose: () => { + worker.terminate(); + }, + }; + }, +}); const frameworkProvider = framework.provider(); // setup application lifecycle events, and emit application start event diff --git a/packages/frontend/apps/android/src/nbstore.ts b/packages/frontend/apps/android/src/nbstore.ts new file mode 100644 index 0000000000..7b3fefd3fe --- /dev/null +++ b/packages/frontend/apps/android/src/nbstore.ts @@ -0,0 +1,22 @@ +import '@affine/core/bootstrap/browser'; + +import { broadcastChannelStorages } from '@affine/nbstore/broadcast-channel'; +import { cloudStorages } from '@affine/nbstore/cloud'; +import { idbStorages } from '@affine/nbstore/idb'; +import { + WorkerConsumer, + type WorkerOps, +} from '@affine/nbstore/worker/consumer'; +import { type MessageCommunicapable, OpConsumer } from '@toeverything/infra/op'; + +const consumer = new WorkerConsumer([ + ...idbStorages, + ...broadcastChannelStorages, + ...cloudStorages, +]); + +const opConsumer = new OpConsumer( + globalThis as MessageCommunicapable +); + +consumer.bindConsumer(opConsumer); diff --git a/packages/frontend/apps/android/tsconfig.json b/packages/frontend/apps/android/tsconfig.json index 263e2a3871..7bdf03aa09 100644 --- a/packages/frontend/apps/android/tsconfig.json +++ b/packages/frontend/apps/android/tsconfig.json @@ -10,6 +10,7 @@ { "path": "../../component" }, { "path": "../../core" }, { "path": "../../i18n" }, + { "path": "../../../common/nbstore" }, { "path": "../../../../blocksuite/affine/all" }, { "path": "../../../common/infra" } ] diff --git a/tools/utils/src/workspace.gen.ts b/tools/utils/src/workspace.gen.ts index 6999702617..7b43cc9cb0 100644 --- a/tools/utils/src/workspace.gen.ts +++ b/tools/utils/src/workspace.gen.ts @@ -504,6 +504,7 @@ export const PackageList = [ 'packages/frontend/component', 'packages/frontend/core', 'packages/frontend/i18n', + 'packages/common/nbstore', 'blocksuite/affine/all', 'packages/common/infra', ], diff --git a/yarn.lock b/yarn.lock index fb74142c1b..1916a54ebe 100644 --- a/yarn.lock +++ b/yarn.lock @@ -222,6 +222,7 @@ __metadata: "@affine/component": "workspace:*" "@affine/core": "workspace:*" "@affine/i18n": "workspace:*" + "@affine/nbstore": "workspace:*" "@blocksuite/affine": "workspace:*" "@blocksuite/icons": "npm:2.2.2" "@capacitor/android": "npm:^7.0.0"