feat(electron): expose electron apis to web worker (#9441)

fix AF-2044
This commit is contained in:
pengx17
2024-12-31 03:17:02 +00:00
parent 6883cc2ded
commit 887732179e
8 changed files with 165 additions and 22 deletions

View File

@@ -1,3 +1,4 @@
import { getElectronAPIs } from '@affine/electron-api/web-worker';
import type {
AttachmentBlockModel,
BookmarkBlockModel,
@@ -43,6 +44,11 @@ const LRU_CACHE_SIZE = 5;
// lru cache for ydoc instances, last used at the end of the array
const lruCache = [] as { doc: YDoc; hash: string }[];
const electronAPIs = BUILD_CONFIG.isElectron ? getElectronAPIs() : null;
// @ts-expect-error test
globalThis.__electronAPIs = electronAPIs;
async function digest(data: Uint8Array) {
if (
globalThis.crypto &&

View File

@@ -1,4 +1,5 @@
import { DebugLogger } from '@affine/debug';
import { connectWebWorker } from '@affine/electron-api/web-worker';
import { MANUALLY_STOP, throwIfAborted } from '@toeverything/infra';
import type {
@@ -12,6 +13,7 @@ const logger = new DebugLogger('affine:indexer-worker');
export async function createWorker(abort: AbortSignal) {
let worker: Worker | null = null;
let electronApiCleanup: (() => void) | null = null;
while (throwIfAborted(abort)) {
try {
worker = await new Promise<Worker>((resolve, reject) => {
@@ -29,6 +31,11 @@ export async function createWorker(abort: AbortSignal) {
}
});
worker.postMessage({ type: 'init', msgId: 0 } as WorkerIngoingMessage);
if (BUILD_CONFIG.isElectron) {
electronApiCleanup = connectWebWorker(worker);
}
setTimeout(() => {
reject('timeout');
}, 1000 * 30 /* 30 sec */);
@@ -97,6 +104,7 @@ export async function createWorker(abort: AbortSignal) {
dispose: () => {
terminateAbort.abort(MANUALLY_STOP);
worker.terminate();
electronApiCleanup?.();
},
};
}

View File

@@ -9,12 +9,12 @@ import {
import { useSortable } from '@dnd-kit/sortable';
import { useLiveData, useService } from '@toeverything/infra';
import { assignInlineVars } from '@vanilla-extract/dynamic';
import type { SetStateAction } from 'jotai';
import type {
Dispatch,
HTMLAttributes,
PropsWithChildren,
RefObject,
SetStateAction,
} from 'react';
import {
memo,