From 89296d4155d84fee401725016bdd87ac274799f1 Mon Sep 17 00:00:00 2001 From: yfzhe Date: Mon, 23 Dec 2024 22:11:09 +0800 Subject: [PATCH] service: some refactors --- .../webassembly-playground/src/service/lib.ts | 13 ++++---- .../src/service/worker.ts | 30 +++++++++---------- .../webassembly-playground/src/state/index.ts | 5 +--- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/packages/webassembly-playground/src/service/lib.ts b/packages/webassembly-playground/src/service/lib.ts index f7c57b0..4b1f59d 100644 --- a/packages/webassembly-playground/src/service/lib.ts +++ b/packages/webassembly-playground/src/service/lib.ts @@ -17,10 +17,12 @@ export type CompileLog = { log: string; }; -function postMessageToWorker( - sw: ServiceWorker, - message: Message, -): Promise { +async function postMessageToServiceWorker(message: Message): Promise { + const sw = navigator.serviceWorker.controller; + if (!sw) { + throw new Error("Service worker is not ready"); + } + return new Promise((resolve) => { const ch = new MessageChannel(); ch.port1.onmessage = (evt) => resolve(evt.data as R); @@ -29,11 +31,10 @@ function postMessageToWorker( } export async function compile( - sw: ServiceWorker, files: Array, features: WasmFeatures, ): Promise> { - return postMessageToWorker>(sw, { + return postMessageToServiceWorker>({ type: MessageType.Compile, files, features, diff --git a/packages/webassembly-playground/src/service/worker.ts b/packages/webassembly-playground/src/service/worker.ts index 2c219cf..45d8d1b 100644 --- a/packages/webassembly-playground/src/service/worker.ts +++ b/packages/webassembly-playground/src/service/worker.ts @@ -10,21 +10,24 @@ import { MessageType, type CompileLog, type Message } from "./lib"; import type { File } from "../types"; import type { WasmFeatures } from "../features"; import { extname, getMimeType } from "../lib/file"; -import { assert } from "../lib/util"; -let wabt: Awaited> | undefined; +// This type is not exported, so define it by our own. +type WabtModule = Awaited>; -const fileStorage: Map = new Map(); - -async function setupWabt() { - return initWabt().then((_wabt) => { - wabt = _wabt; - }); +let _wabt: WabtModule | undefined; +async function getWabt() { + if (!_wabt) { + const wabt = await initWabt(); + _wabt = wabt; + } + return _wabt; } +const fileStorage: Map = new Map(); + self.addEventListener("install", (evt) => { self.skipWaiting(); - evt.waitUntil(setupWabt()); + evt.waitUntil(getWabt()); }); self.addEventListener("activate", () => { @@ -32,14 +35,9 @@ self.addEventListener("activate", () => { }); async function compile(files: Array, features: WasmFeatures) { - if (!wabt) { - await setupWabt(); - } - assert(wabt); - - fileStorage.clear(); - + const wabt = await getWabt(); let logs: Array = []; + fileStorage.clear(); for (const file of files) { const { filename, content } = file; diff --git a/packages/webassembly-playground/src/state/index.ts b/packages/webassembly-playground/src/state/index.ts index cf9b4a0..cf7887d 100644 --- a/packages/webassembly-playground/src/state/index.ts +++ b/packages/webassembly-playground/src/state/index.ts @@ -32,13 +32,10 @@ const _previewIdAtom = atom(null); export const previewIdAtom = atom((get) => get(_previewIdAtom)); export const runAtom = atom(null, async (get, set) => { - const sw = navigator.serviceWorker.controller; - if (!sw) return; - const files = get(filesAtom); const features = get(featuresAtom); - const logs = await compile(sw, files, features); + const logs = await compile(files, features); set(compileLogsAtom, logs); if (logs.some((log) => log.result === "err")) {