diff options
Diffstat (limited to 'cli/js/workers.ts')
-rw-r--r-- | cli/js/workers.ts | 71 |
1 files changed, 13 insertions, 58 deletions
diff --git a/cli/js/workers.ts b/cli/js/workers.ts index 2a5d4d190..fb63a3260 100644 --- a/cli/js/workers.ts +++ b/cli/js/workers.ts @@ -29,7 +29,7 @@ function createWorker( hasSourceCode: boolean, sourceCode: Uint8Array, name?: string -): { id: number; loaded: boolean } { +): { id: number } { return sendSync(dispatch.OP_CREATE_WORKER, { specifier, hasSourceCode, @@ -38,22 +38,6 @@ function createWorker( }); } -async function hostGetWorkerLoaded(id: number): Promise<any> { - return await sendAsync(dispatch.OP_HOST_GET_WORKER_LOADED, { id }); -} - -async function hostPollWorker(id: number): Promise<any> { - return await sendAsync(dispatch.OP_HOST_POLL_WORKER, { id }); -} - -function hostCloseWorker(id: number): void { - sendSync(dispatch.OP_HOST_CLOSE_WORKER, { id }); -} - -function hostResumeWorker(id: number): void { - sendSync(dispatch.OP_HOST_RESUME_WORKER, { id }); -} - function hostPostMessage(id: number, data: any): void { const dataIntArray = encodeMessage(data); sendSync(dispatch.OP_HOST_POST_MESSAGE, { id }, dataIntArray); @@ -85,8 +69,6 @@ export interface WorkerOptions { export class WorkerImpl extends EventTarget implements Worker { private readonly id: number; private isClosing = false; - private messageBuffer: any[] = []; - private ready = false; public onerror?: (e: any) => void; public onmessage?: (data: any) => void; public onmessageerror?: () => void; @@ -125,14 +107,13 @@ export class WorkerImpl extends EventTarget implements Worker { } */ - const { id, loaded } = createWorker( + const { id } = createWorker( specifier, hasSourceCode, sourceCode, options?.name ); this.id = id; - this.ready = loaded; this.poll(); } @@ -158,27 +139,19 @@ export class WorkerImpl extends EventTarget implements Worker { } async poll(): Promise<void> { - // If worker has not been immediately executed - // then let's await it's readiness - if (!this.ready) { - const result = await hostGetWorkerLoaded(this.id); - - if (result.error) { - if (!this.handleError(result.error)) { - throw new Error(result.error.message); - } - return; + while (!this.isClosing) { + const data = await hostGetMessage(this.id); + if (data == null) { + log("worker got null message. quitting."); + break; + } + if (this.onmessage) { + const event = { data }; + this.onmessage(event); } } - // drain messages - for (const data of this.messageBuffer) { - hostPostMessage(this.id, data); - } - this.messageBuffer = []; - this.ready = true; - this.run(); - + /* while (true) { const result = await hostPollWorker(this.id); @@ -194,32 +167,14 @@ export class WorkerImpl extends EventTarget implements Worker { break; } } + */ } postMessage(data: any): void { - if (!this.ready) { - this.messageBuffer.push(data); - return; - } - hostPostMessage(this.id, data); } terminate(): void { throw new Error("Not yet implemented"); } - - private async run(): Promise<void> { - while (!this.isClosing) { - const data = await hostGetMessage(this.id); - if (data == null) { - log("worker got null message. quitting."); - break; - } - if (this.onmessage) { - const event = { data }; - this.onmessage(event); - } - } - } } |