-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0592cd
commit 98d64d7
Showing
2 changed files
with
69 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,59 @@ | ||
import type { JsonValue } from '@bufbuild/protobuf'; | ||
import { ConnectError } from '@connectrpc/connect'; | ||
import { errorToJson } from '@connectrpc/connect/protocol-connect'; | ||
import { | ||
ActionBuildRequest, | ||
ActionBuildResponse, | ||
isActionBuildRequest, | ||
isOffscreenRequest, | ||
} from '@penumbra-zone/types/src/internal-msg/offscreen'; | ||
|
||
chrome.runtime.onMessage.addListener((req, _sender, respond) => { | ||
if (!isOffscreenRequest(req)) return false; | ||
if (isActionBuildRequest(req.request)) { | ||
if (isOffscreenRequest(req)) { | ||
const { type, request } = req; | ||
void (async () => { | ||
const response = spawnWorker(request); | ||
const res = await response | ||
.then(data => ({ type, data })) | ||
.catch((e: Error) => ({ | ||
type, | ||
error: `Offscreen: ${e.message}`, | ||
})); | ||
respond(res); | ||
})(); | ||
if (isActionBuildRequest(request)) { | ||
void spawnActionBuildWorker(request) | ||
.then( | ||
data => ({ type, data }), | ||
e => ({ | ||
type, | ||
error: errorToJson(ConnectError.from(e), undefined), | ||
}), | ||
) | ||
.then(respond); | ||
return true; | ||
} | ||
} | ||
return true; | ||
return false; | ||
}); | ||
|
||
const spawnWorker = (req: ActionBuildRequest): Promise<JsonValue> => { | ||
return new Promise((resolve, reject) => { | ||
const worker = new Worker(new URL('../wasm-build-action.ts', import.meta.url)); | ||
const spawnActionBuildWorker = (req: ActionBuildRequest) => { | ||
const worker = new Worker(new URL('../wasm-build-action.ts', import.meta.url)); | ||
return new Promise<ActionBuildResponse>((resolve, reject) => { | ||
worker.addEventListener( | ||
'message', | ||
(e: MessageEvent) => resolve(e.data as ActionBuildResponse), | ||
{ once: true }, | ||
); | ||
|
||
const onWorkerMessage = (e: MessageEvent) => { | ||
resolve(e.data as JsonValue); | ||
worker.removeEventListener('error', onWorkerError); | ||
worker.terminate(); | ||
}; | ||
worker.addEventListener( | ||
'error', | ||
({ error, filename, lineno, colno, message }: ErrorEvent) => | ||
reject( | ||
error instanceof Error | ||
? error | ||
: new Error(`Worker ErrorEvent ${filename}:${lineno}:${colno} ${message}`), | ||
), | ||
{ once: true }, | ||
); | ||
|
||
const onWorkerError = (ev: ErrorEvent) => { | ||
const { filename, lineno, colno, message } = ev; | ||
reject( | ||
ev.error instanceof Error | ||
? ev.error | ||
: new Error(`Worker ErrorEvent ${filename}:${lineno}:${colno} ${message}`), | ||
); | ||
worker.removeEventListener('message', onWorkerMessage); | ||
worker.terminate(); | ||
}; | ||
worker.addEventListener( | ||
'messageerror', | ||
(ev: MessageEvent) => reject(ConnectError.from(ev.data ?? ev)), | ||
|
||
worker.addEventListener('message', onWorkerMessage, { once: true }); | ||
worker.addEventListener('error', onWorkerError, { once: true }); | ||
{ once: true }, | ||
); | ||
|
||
// Send data to web worker | ||
worker.postMessage(req); | ||
}); | ||
}).finally(() => worker.terminate()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters