import { inWorker } from "@/next/env"; import * as Comlink from "comlink"; import { wrap } from "comlink"; import { ElectronAPIsType } from "./types"; import { WorkerSafeElectronClient } from "./worker/client"; export interface LimitedElectronAPIs extends Pick {} class WorkerSafeElectronServiceImpl implements LimitedElectronAPIs { proxiedElectron: | Comlink.Remote | WorkerSafeElectronClient; ready: Promise; constructor() { this.ready = this.init(); } private async init() { if (inWorker()) { const workerSafeElectronClient = wrap(self); this.proxiedElectron = await new workerSafeElectronClient(); } else { this.proxiedElectron = new WorkerSafeElectronClient(); } } async convertToJPEG( inputFileData: Uint8Array, filename: string, ): Promise { await this.ready; return this.proxiedElectron.convertToJPEG(inputFileData, filename); } async logToDisk(message: string) { await this.ready; return this.proxiedElectron.logToDisk(message); } } export const WorkerSafeElectronService = new WorkerSafeElectronServiceImpl();