mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-25 14:28:51 +08:00
feat(core): new worker workspace engine (#9257)
This commit is contained in:
@@ -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