Skip to content

Commit

Permalink
service: some refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
yfzhe committed Dec 23, 2024
1 parent 5d40962 commit 89296d4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
13 changes: 7 additions & 6 deletions packages/webassembly-playground/src/service/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ export type CompileLog = {
log: string;
};

function postMessageToWorker<R>(
sw: ServiceWorker,
message: Message,
): Promise<R> {
async function postMessageToServiceWorker<R>(message: Message): Promise<R> {
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);
Expand All @@ -29,11 +31,10 @@ function postMessageToWorker<R>(
}

export async function compile(
sw: ServiceWorker,
files: Array<File>,
features: WasmFeatures,
): Promise<Array<CompileLog>> {
return postMessageToWorker<Array<CompileLog>>(sw, {
return postMessageToServiceWorker<Array<CompileLog>>({
type: MessageType.Compile,
files,
features,
Expand Down
30 changes: 14 additions & 16 deletions packages/webassembly-playground/src/service/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,34 @@ 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<ReturnType<typeof initWabt>> | undefined;
// This type is not exported, so define it by our own.
type WabtModule = Awaited<ReturnType<typeof initWabt>>;

const fileStorage: Map<string, string | Uint8Array> = 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<string, string | Uint8Array> = new Map();

self.addEventListener("install", (evt) => {
self.skipWaiting();
evt.waitUntil(setupWabt());
evt.waitUntil(getWabt());
});

self.addEventListener("activate", () => {
self.clients.claim();
});

async function compile(files: Array<File>, features: WasmFeatures) {
if (!wabt) {
await setupWabt();
}
assert(wabt);

fileStorage.clear();

const wabt = await getWabt();
let logs: Array<CompileLog> = [];
fileStorage.clear();

for (const file of files) {
const { filename, content } = file;
Expand Down
5 changes: 1 addition & 4 deletions packages/webassembly-playground/src/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@ const _previewIdAtom = atom<string | null>(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")) {
Expand Down

0 comments on commit 89296d4

Please sign in to comment.