Skip to content

Commit 57eb257

Browse files
committed
Initialization of web worker is fixed.
1 parent 4fb648b commit 57eb257

File tree

5 files changed

+48
-23
lines changed

5 files changed

+48
-23
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ async function initTwelf(editor: EditorView) {
8383
}
8484

8585
(document.getElementById('twelf-response') as HTMLTextAreaElement).value = '';
86-
const twelfService = await mkTwelfWorker();
86+
const worker = await mkTwelfWorker();
8787

8888
async function execAndShowStatus(text: string): Promise<void> {
89-
const result = await twelfService.exec(text);
89+
const result = await worker.exec(text);
9090
showStatus(result.status);
9191
showErrors(result.errors);
9292
(document.getElementById('twelf-response') as HTMLTextAreaElement).value = result.output.join('');

src/twelf-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Status, TwelfError, TwelfWorkerResponse } from "./twelf-worker-types";
1+
import { Status, TwelfError, TwelfExecResponse } from "./twelf-worker-types";
22
import { WasiSnapshotPreview1, args_get, args_sizes_get, clock_time_get, environ_sizes_get, fd_write } from "./wasi";
33

44
type TwelfExports = {
@@ -60,7 +60,7 @@ export class TwelfService {
6060

6161
constructor(public instance: WebAssembly.Instance, public output: string[]) { }
6262

63-
async exec(input: string): Promise<TwelfWorkerResponse> {
63+
async exec(input: string): Promise<TwelfExecResponse> {
6464
this.output.splice(0); // Erase output
6565

6666
const exports = this.instance.exports as TwelfExports;

src/twelf-worker-types.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ export type TwelfError = {
1010

1111
export type WithId<T> = { id: number, body: T };
1212

13-
export type TwelfWorkerRequest = {
13+
export type TwelfExecRequest = {
1414
input: string,
1515
}
1616

17-
export type TwelfWorkerResponse = {
17+
export type TwelfExecResponse = {
1818
status: Status,
1919
output: string[],
2020
errors: TwelfError[],
2121
}
22+
23+
export type TwelfReadyResponse = {};
24+
25+
export type TwelfResponse =
26+
| { t: 'ready', id: number, response: TwelfReadyResponse }
27+
| { t: 'execResponse', id: number, response: TwelfExecResponse }
28+
;

src/twelf-worker.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,53 @@
1-
import { TwelfWorkerRequest, TwelfWorkerResponse, WithId } from "./twelf-worker-types";
1+
import { TwelfExecRequest, TwelfExecResponse, TwelfResponse, WithId } from "./twelf-worker-types";
22

3-
export function mkTwelfWorker(): TwelfWorker {
4-
return new TwelfWorker();
3+
export async function mkTwelfWorker(): Promise<TwelfWorker> {
4+
const worker = new TwelfWorker();
5+
await worker.isReady();
6+
return worker;
57
}
68

79
class TwelfWorker {
810
requestIdCounter: number = 0;
9-
responseMap: Record<number, (result: TwelfWorkerResponse) => void> = {};
11+
responseMap: Record<number, (result: TwelfResponse) => void>;
1012
worker: Worker;
13+
_readyPromise: Promise<void>;
1114

1215
constructor() {
1316
this.worker = new Worker('./assets/worker.js');
1417
this.worker.onmessage = (msg) => {
15-
const data: WithId<TwelfWorkerResponse> = msg.data;
16-
this.responseMap[data.id](data.body);
18+
const data: TwelfResponse = msg.data;
19+
this.responseMap[data.id](data);
1720
}
21+
this.responseMap = {};
22+
const readyPromise = new Promise<void>((res, rej) => {
23+
this.responseMap[-1] = msg => {
24+
res();
25+
}
26+
});
27+
this._readyPromise = readyPromise;
1828
}
1929

20-
mkRequest(input: string): WithId<TwelfWorkerRequest> {
30+
mkRequest(input: string): WithId<TwelfExecRequest> {
2131
return {
2232
id: this.requestIdCounter++,
2333
body: { input },
2434
};
2535
}
2636

27-
exec(input: string): Promise<TwelfWorkerResponse> {
37+
async exec(input: string): Promise<TwelfExecResponse> {
2838
const req = this.mkRequest(input);
29-
const prom = new Promise<TwelfWorkerResponse>((res, rej) => {
39+
const prom = new Promise<TwelfResponse>((res, rej) => {
3040
this.responseMap[req.id] = res;
3141
});
3242
this.worker.postMessage(req);
33-
return prom;
43+
const p = await prom;
44+
if (p.t != 'execResponse') {
45+
throw new Error(`expected execReponse but got ${p.t}`);
46+
}
47+
return p.response;
48+
}
49+
50+
isReady(): Promise<void> {
51+
return this._readyPromise;
3452
}
3553
}

src/worker.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { mkTwelfService } from "./twelf-service";
2-
import { Status, TwelfWorkerRequest, TwelfWorkerResponse, WithId } from "./twelf-worker-types";
2+
import { Status, TwelfExecRequest, TwelfExecResponse, TwelfResponse, WithId } from "./twelf-worker-types";
33

44
async function go() {
55
const service = await mkTwelfService('./twelf.wasm');
66

7-
// XXX What happens if we get a message before twelf wasm is loaded?
8-
// Should send a "we're all initialized" message back to host.
7+
function post(r: TwelfResponse): void {
8+
self.postMessage(r);
9+
}
910

1011
self.onmessage = async event => {
11-
const { body, id } = event.data as WithId<TwelfWorkerRequest>;
12-
const response: WithId<TwelfWorkerResponse> =
13-
{ id, body: await service.exec(body.input) };
14-
self.postMessage(response);
12+
const { body, id } = event.data as WithId<TwelfExecRequest>;
13+
post({ t: 'execResponse', id, response: await service.exec(body.input) });
1514
};
1615

16+
post({ t: 'ready', id: -1, response: {} });
1717
}
1818

1919
go();

0 commit comments

Comments
 (0)