mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 04:48:53 +00:00
feat(core): new worker workspace engine (#9257)
This commit is contained in:
@@ -5,17 +5,18 @@ import { configureCommonModules } from '@affine/core/modules';
|
||||
import { I18nProvider } from '@affine/core/modules/i18n';
|
||||
import { LifecycleService } from '@affine/core/modules/lifecycle';
|
||||
import { OpenInAppGuard } from '@affine/core/modules/open-in-app';
|
||||
import { configureLocalStorageStateStorageImpls } from '@affine/core/modules/storage';
|
||||
import { PopupWindowProvider } from '@affine/core/modules/url';
|
||||
import { configureIndexedDBUserspaceStorageProvider } from '@affine/core/modules/userspace';
|
||||
import { configureBrowserWorkbenchModule } from '@affine/core/modules/workbench';
|
||||
import {
|
||||
configureBrowserWorkspaceFlavours,
|
||||
configureIndexedDBWorkspaceEngineStorageProvider,
|
||||
} from '@affine/core/modules/workspace-engine';
|
||||
configureLocalStorageStateStorageImpls,
|
||||
NbstoreProvider,
|
||||
} from '@affine/core/modules/storage';
|
||||
import { PopupWindowProvider } from '@affine/core/modules/url';
|
||||
import { configureBrowserWorkbenchModule } from '@affine/core/modules/workbench';
|
||||
import { configureBrowserWorkspaceFlavours } from '@affine/core/modules/workspace-engine';
|
||||
import createEmotionCache from '@affine/core/utils/create-emotion-cache';
|
||||
import { WorkerClient } from '@affine/nbstore/worker/client';
|
||||
import { CacheProvider } from '@emotion/react';
|
||||
import { Framework, FrameworkRoot, getCurrentStore } from '@toeverything/infra';
|
||||
import { OpClient } from '@toeverything/infra/op';
|
||||
import { Suspense } from 'react';
|
||||
import { RouterProvider } from 'react-router-dom';
|
||||
|
||||
@@ -30,8 +31,41 @@ configureCommonModules(framework);
|
||||
configureBrowserWorkbenchModule(framework);
|
||||
configureLocalStorageStateStorageImpls(framework);
|
||||
configureBrowserWorkspaceFlavours(framework);
|
||||
configureIndexedDBWorkspaceEngineStorageProvider(framework);
|
||||
configureIndexedDBUserspaceStorageProvider(framework);
|
||||
framework.impl(NbstoreProvider, {
|
||||
openStore(key, options) {
|
||||
if (window.SharedWorker) {
|
||||
const worker = new SharedWorker(
|
||||
new URL(
|
||||
/* webpackChunkName: "nbstore" */ './nbstore.ts',
|
||||
import.meta.url
|
||||
),
|
||||
{ name: key }
|
||||
);
|
||||
const client = new WorkerClient(new OpClient(worker.port), options);
|
||||
return {
|
||||
store: client,
|
||||
dispose: () => {
|
||||
worker.port.postMessage({ type: '__close__' });
|
||||
worker.port.close();
|
||||
},
|
||||
};
|
||||
} else {
|
||||
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();
|
||||
},
|
||||
};
|
||||
}
|
||||
},
|
||||
});
|
||||
framework.impl(PopupWindowProvider, {
|
||||
open: (target: string) => {
|
||||
const targetUrl = new URL(target);
|
||||
|
||||
46
packages/frontend/apps/web/src/nbstore.ts
Normal file
46
packages/frontend/apps/web/src/nbstore.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
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 { idbV1Storages } from '@affine/nbstore/idb/v1';
|
||||
import {
|
||||
WorkerConsumer,
|
||||
type WorkerOps,
|
||||
} from '@affine/nbstore/worker/consumer';
|
||||
import { type MessageCommunicapable, OpConsumer } from '@toeverything/infra/op';
|
||||
|
||||
const consumer = new WorkerConsumer([
|
||||
...idbStorages,
|
||||
...idbV1Storages,
|
||||
...broadcastChannelStorages,
|
||||
...cloudStorages,
|
||||
]);
|
||||
|
||||
if ('onconnect' in globalThis) {
|
||||
// if in shared worker
|
||||
let activeConnectionCount = 0;
|
||||
|
||||
(globalThis as any).onconnect = (event: MessageEvent) => {
|
||||
activeConnectionCount++;
|
||||
const port = event.ports[0];
|
||||
port.addEventListener('message', (event: MessageEvent) => {
|
||||
if (event.data.type === '__close__') {
|
||||
activeConnectionCount--;
|
||||
if (activeConnectionCount === 0) {
|
||||
globalThis.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const opConsumer = new OpConsumer<WorkerOps>(port);
|
||||
consumer.bindConsumer(opConsumer);
|
||||
};
|
||||
} else {
|
||||
// if in worker
|
||||
const opConsumer = new OpConsumer<WorkerOps>(
|
||||
globalThis as MessageCommunicapable
|
||||
);
|
||||
|
||||
consumer.bindConsumer(opConsumer);
|
||||
}
|
||||
Reference in New Issue
Block a user