mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-19 02:56:23 +08:00
feat(core): new worker workspace engine (#9257)
This commit is contained in:
@@ -248,53 +248,3 @@ export const events = {
|
||||
...mainAPIs.events,
|
||||
...helperAPIs.events,
|
||||
};
|
||||
|
||||
/**
|
||||
* Create MessagePort that can be used by web workers
|
||||
*
|
||||
* !!!
|
||||
* SHOULD ONLY BE USED IN RENDERER PROCESS
|
||||
* !!!
|
||||
*/
|
||||
export function requestWebWorkerPort() {
|
||||
const ch = new MessageChannel();
|
||||
const localPort = ch.port1;
|
||||
const remotePort = ch.port2;
|
||||
|
||||
// todo: should be able to let the web worker use the electron APIs directly for better performance
|
||||
const flattenedAPIs = Object.entries(apis).flatMap(([namespace, api]) => {
|
||||
return Object.entries(api as any).map(([method, fn]) => [
|
||||
`${namespace}:${method}`,
|
||||
fn,
|
||||
]);
|
||||
});
|
||||
|
||||
AsyncCall(Object.fromEntries(flattenedAPIs), {
|
||||
channel: createMessagePortChannel(localPort),
|
||||
log: false,
|
||||
});
|
||||
|
||||
const cleanup = () => {
|
||||
remotePort.close();
|
||||
localPort.close();
|
||||
};
|
||||
|
||||
const portId = crypto.randomUUID();
|
||||
|
||||
setTimeout(() => {
|
||||
// @ts-expect-error this function should only be evaluated in the renderer process
|
||||
window.postMessage(
|
||||
{
|
||||
type: 'electron:request-api-port',
|
||||
portId,
|
||||
ports: [remotePort],
|
||||
},
|
||||
'*',
|
||||
[remotePort]
|
||||
);
|
||||
});
|
||||
|
||||
localPort.start();
|
||||
|
||||
return { portId, cleanup };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user