This commit is contained in:
Manav Rathi 2024-04-08 20:38:59 +05:30
parent 0be549c91b
commit 5339b1aa89
No known key found for this signature in database
8 changed files with 31 additions and 16 deletions

View file

@ -20,7 +20,6 @@
"blazeface-back": "^0.0.9",
"bs58": "^5.0.0",
"chrono-node": "^2.2.6",
"comlink": "^4.3.0",
"date-fns": "^2",
"debounce": "^2.0.0",
"density-clustering": "^1.3.0",

View file

@ -1,7 +1,9 @@
import { inWorker } from "@/next/env";
import isElectron from "is-electron";
import ElectronAPIs from "./electron";
import { isDevBuild } from "./env";
import * as web from "./log-web";
import { logToDisk as webLogToDisk } from "./log-web";
import { workerBridge } from "./worker/worker-bridge";
/**
* Write a {@link message} to the on-disk log.
@ -11,7 +13,19 @@ import * as web from "./log-web";
*/
export const logToDisk = (message: string) => {
if (isElectron()) ElectronAPIs.logToDisk(message);
else web.logToDisk(message);
else if (inWorker()) workerLogToDisk(message);
else webLogToDisk(message);
};
const workerLogToDisk = (message: string) => {
workerBridge.logToDisk(message).catch((e) => {
console.error(
"Failed to log a message from worker",
e,
"\nThe message was",
message,
);
});
};
const logError = (message: string, e?: unknown) => {

View file

@ -7,6 +7,7 @@
"@emotion/styled": "^11.11",
"@mui/icons-material": "^5.15",
"@mui/material": "^5.15",
"comlink": "^4.4",
"get-user-locale": "^2.3",
"i18next": "^23.10",
"i18next-resources-to-backend": "^1.2.0",

View file

@ -1,7 +1,6 @@
import ElectronAPIs from "@/next/electron";
import log, { logToDisk } from "@/next/log";
import { Remote, expose, wrap } from "comlink";
import { logError } from "../sentry";
import { expose, wrap, type Remote } from "comlink";
export class ComlinkWorker<T extends new () => InstanceType<T>> {
public remote: Promise<Remote<InstanceType<T>>>;
@ -12,11 +11,13 @@ export class ComlinkWorker<T extends new () => InstanceType<T>> {
this.name = name;
this.worker = worker;
this.worker.onerror = (errorEvent) => {
logError(Error(errorEvent.message), "Got error event from worker", {
errorEvent: JSON.stringify(errorEvent),
name: this.name,
});
this.worker.onerror = (ev) => {
log.error(
`Got error event from worker: ${JSON.stringify({
errorEvent: JSON.stringify(ev),
name: this.name,
})}`,
);
};
log.debug(() => `Initiated ${this.name}`);
const comlink = wrap<T>(this.worker);
@ -39,7 +40,7 @@ export class ComlinkWorker<T extends new () => InstanceType<T>> {
* create.
*
* Inside the worker's code, this can be accessed by using the sibling
* `workerBridge` object by importing `worker-bridge.ts`.
* `workerBridge` object after importing it from `worker-bridge.ts`.
*/
const workerBridge = {
logToDisk,

View file

@ -1,5 +1,5 @@
import { wrap } from "comlink";
import type { WorkerBridge } from "./comlinkWorker";
import type { WorkerBridge } from "./comlink-worker";
/**
* The web worker side handle to the {@link WorkerBridge} exposed by the main
@ -7,6 +7,6 @@ import type { WorkerBridge } from "./comlinkWorker";
*
* This file is meant to be run inside a worker. Accessing the properties of
* this object will be transparently (but asynchrorously) relayed to the
* implementation of the {@link WorkerBridge} in `comlinkWorker.ts`.
* implementation of the {@link WorkerBridge} in `comlink-worker.ts`.
*/
export const workerBridge = wrap<WorkerBridge>(globalThis);

View file

@ -1,5 +1,5 @@
import { ComlinkWorker } from "@/next/worker/comlinkWorker";
import { Remote } from "comlink";
import { ComlinkWorker } from "../worker/comlinkWorker";
import { DedicatedCryptoWorker } from "./internal/crypto.worker";
class ComlinkCryptoWorker {

View file

@ -1,7 +1,7 @@
import { inWorker, isDevBuild } from "@/next/env";
import { logToDisk } from "@/next/log";
import { workerBridge } from "@/next/worker/worker-bridge";
import { logError } from "@ente/shared/sentry";
import { workerBridge } from "../worker/worker-bridge";
export function addLogLine(
log: string | number | boolean,

View file

@ -1777,7 +1777,7 @@ combined-stream@^1.0.8:
dependencies:
delayed-stream "~1.0.0"
comlink@^4.3.0:
comlink@^4.4:
version "4.4.1"
resolved "https://registry.yarnpkg.com/comlink/-/comlink-4.4.1.tgz#e568b8e86410b809e8600eb2cf40c189371ef981"
integrity sha512-+1dlx0aY5Jo1vHy/tSsIGpSkN4tS9rZSW8FIhG0JH/crs9wwweswIo/POr451r7bZww3hFbPAKnTpimzL/mm4Q==