mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-10 05:29:08 +08:00
feat(nbstore): share worker between workspaces (#9947)
This commit is contained in:
@@ -5,12 +5,12 @@ import { cloudStorages } from '@affine/nbstore/cloud';
|
||||
import { idbStorages } from '@affine/nbstore/idb';
|
||||
import { idbV1Storages } from '@affine/nbstore/idb/v1';
|
||||
import {
|
||||
WorkerConsumer,
|
||||
type WorkerOps,
|
||||
StoreManagerConsumer,
|
||||
type WorkerManagerOps,
|
||||
} from '@affine/nbstore/worker/consumer';
|
||||
import { type MessageCommunicapable, OpConsumer } from '@toeverything/infra/op';
|
||||
|
||||
const consumer = new WorkerConsumer([
|
||||
const consumer = new StoreManagerConsumer([
|
||||
...idbStorages,
|
||||
...idbV1Storages,
|
||||
...broadcastChannelStorages,
|
||||
@@ -19,28 +19,14 @@ const consumer = new WorkerConsumer([
|
||||
|
||||
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);
|
||||
consumer.bindConsumer(new OpConsumer<WorkerManagerOps>(port));
|
||||
};
|
||||
} else {
|
||||
// if in worker
|
||||
const opConsumer = new OpConsumer<WorkerOps>(
|
||||
globalThis as MessageCommunicapable
|
||||
consumer.bindConsumer(
|
||||
new OpConsumer<WorkerManagerOps>(globalThis as MessageCommunicapable)
|
||||
);
|
||||
|
||||
consumer.bindConsumer(opConsumer);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user