2019-01-08 14:44:06 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-04-01 15:09:59 -04:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
import { sendAsync, sendSync } from "./dispatch";
|
2019-03-30 14:45:36 -04:00
|
|
|
import * as msg from "gen/cli/msg_generated";
|
2019-01-08 14:44:06 -05:00
|
|
|
import * as flatbuffers from "./flatbuffers";
|
|
|
|
import { assert, log } from "./util";
|
2019-04-01 15:09:59 -04:00
|
|
|
import { TextDecoder, TextEncoder } from "./text_encoding";
|
2019-02-19 21:42:19 -05:00
|
|
|
import { window } from "./window";
|
2019-01-08 14:44:06 -05:00
|
|
|
|
2019-04-01 15:09:59 -04:00
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
|
|
|
export function encodeMessage(data: any): Uint8Array {
|
|
|
|
const dataJson = JSON.stringify(data);
|
|
|
|
return encoder.encode(dataJson);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function decodeMessage(dataIntArray: Uint8Array): any {
|
|
|
|
const dataJson = decoder.decode(dataIntArray);
|
|
|
|
return JSON.parse(dataJson);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createWorker(specifier: string): number {
|
|
|
|
const builder = flatbuffers.createBuilder();
|
|
|
|
const specifier_ = builder.createString(specifier);
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.CreateWorker.createCreateWorker(builder, specifier_);
|
2019-04-01 15:09:59 -04:00
|
|
|
const baseRes = sendSync(builder, msg.Any.CreateWorker, inner);
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(
|
|
|
|
msg.Any.CreateWorkerRes === baseRes!.innerType(),
|
|
|
|
`base.innerType() unexpectedly is ${baseRes!.innerType()}`
|
|
|
|
);
|
|
|
|
const res = new msg.CreateWorkerRes();
|
|
|
|
assert(baseRes!.inner(res) != null);
|
|
|
|
return res.rid();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function hostGetWorkerClosed(rid: number): Promise<void> {
|
|
|
|
const builder = flatbuffers.createBuilder();
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.HostGetWorkerClosed.createHostGetWorkerClosed(builder, rid);
|
2019-04-01 15:09:59 -04:00
|
|
|
await sendAsync(builder, msg.Any.HostGetWorkerClosed, inner);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hostPostMessage(rid: number, data: any): void {
|
|
|
|
const dataIntArray = encodeMessage(data);
|
|
|
|
const builder = flatbuffers.createBuilder();
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.HostPostMessage.createHostPostMessage(builder, rid);
|
2019-04-01 15:09:59 -04:00
|
|
|
const baseRes = sendSync(
|
|
|
|
builder,
|
|
|
|
msg.Any.HostPostMessage,
|
|
|
|
inner,
|
|
|
|
dataIntArray
|
|
|
|
);
|
|
|
|
assert(baseRes != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function hostGetMessage(rid: number): Promise<any> {
|
|
|
|
const builder = flatbuffers.createBuilder();
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.HostGetMessage.createHostGetMessage(builder, rid);
|
2019-04-01 15:09:59 -04:00
|
|
|
const baseRes = await sendAsync(builder, msg.Any.HostGetMessage, inner);
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(
|
|
|
|
msg.Any.HostGetMessageRes === baseRes!.innerType(),
|
|
|
|
`base.innerType() unexpectedly is ${baseRes!.innerType()}`
|
|
|
|
);
|
|
|
|
const res = new msg.HostGetMessageRes();
|
|
|
|
assert(baseRes!.inner(res) != null);
|
|
|
|
|
|
|
|
const dataArray = res.dataArray();
|
|
|
|
if (dataArray != null) {
|
|
|
|
return decodeMessage(dataArray);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stuff for workers
|
|
|
|
export let onmessage: (e: { data: any }) => void = (): void => {};
|
|
|
|
|
|
|
|
export function postMessage(data: any): void {
|
|
|
|
const dataIntArray = encodeMessage(data);
|
2019-01-08 14:44:06 -05:00
|
|
|
const builder = flatbuffers.createBuilder();
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.WorkerPostMessage.createWorkerPostMessage(builder);
|
2019-04-01 15:09:59 -04:00
|
|
|
const baseRes = sendSync(
|
2019-01-08 14:44:06 -05:00
|
|
|
builder,
|
|
|
|
msg.Any.WorkerPostMessage,
|
|
|
|
inner,
|
2019-04-01 15:09:59 -04:00
|
|
|
dataIntArray
|
2019-01-08 14:44:06 -05:00
|
|
|
);
|
|
|
|
assert(baseRes != null);
|
|
|
|
}
|
|
|
|
|
2019-04-01 15:09:59 -04:00
|
|
|
export async function getMessage(): Promise<any> {
|
2019-01-08 14:44:06 -05:00
|
|
|
log("getMessage");
|
|
|
|
const builder = flatbuffers.createBuilder();
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.WorkerGetMessage.createWorkerGetMessage(
|
|
|
|
builder,
|
|
|
|
0 /* unused */
|
|
|
|
);
|
2019-04-01 15:09:59 -04:00
|
|
|
const baseRes = await sendAsync(builder, msg.Any.WorkerGetMessage, inner);
|
2019-01-08 14:44:06 -05:00
|
|
|
assert(baseRes != null);
|
|
|
|
assert(
|
|
|
|
msg.Any.WorkerGetMessageRes === baseRes!.innerType(),
|
|
|
|
`base.innerType() unexpectedly is ${baseRes!.innerType()}`
|
|
|
|
);
|
|
|
|
const res = new msg.WorkerGetMessageRes();
|
|
|
|
assert(baseRes!.inner(res) != null);
|
|
|
|
|
|
|
|
const dataArray = res.dataArray();
|
2019-04-01 15:09:59 -04:00
|
|
|
if (dataArray != null) {
|
|
|
|
return decodeMessage(dataArray);
|
2019-01-08 14:44:06 -05:00
|
|
|
} else {
|
2019-04-01 15:09:59 -04:00
|
|
|
return null;
|
2019-01-08 14:44:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 15:09:59 -04:00
|
|
|
export let isClosing = false;
|
2019-01-08 14:44:06 -05:00
|
|
|
|
|
|
|
export function workerClose(): void {
|
|
|
|
isClosing = true;
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
export async function workerMain(): Promise<void> {
|
2019-01-08 14:44:06 -05:00
|
|
|
log("workerMain");
|
|
|
|
|
|
|
|
while (!isClosing) {
|
|
|
|
const data = await getMessage();
|
|
|
|
if (data == null) {
|
|
|
|
log("workerMain got null message. quitting.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (window["onmessage"]) {
|
|
|
|
const event = { data };
|
|
|
|
window.onmessage(event);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-01 15:09:59 -04:00
|
|
|
|
|
|
|
export interface Worker {
|
|
|
|
onerror?: () => void;
|
|
|
|
onmessage?: (e: { data: any }) => void;
|
|
|
|
onmessageerror?: () => void;
|
|
|
|
postMessage(data: any): void;
|
2019-04-05 15:57:59 -04:00
|
|
|
closed: Promise<void>;
|
2019-04-01 15:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export class WorkerImpl implements Worker {
|
|
|
|
private readonly rid: number;
|
|
|
|
private isClosing: boolean = false;
|
2019-04-05 15:57:59 -04:00
|
|
|
private readonly isClosedPromise: Promise<void>;
|
2019-04-01 15:09:59 -04:00
|
|
|
public onerror?: () => void;
|
|
|
|
public onmessage?: (data: any) => void;
|
|
|
|
public onmessageerror?: () => void;
|
|
|
|
|
|
|
|
constructor(specifier: string) {
|
|
|
|
this.rid = createWorker(specifier);
|
|
|
|
this.run();
|
2019-04-05 15:57:59 -04:00
|
|
|
this.isClosedPromise = hostGetWorkerClosed(this.rid);
|
|
|
|
this.isClosedPromise.then(() => {
|
2019-04-01 15:09:59 -04:00
|
|
|
this.isClosing = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-05 15:57:59 -04:00
|
|
|
get closed(): Promise<void> {
|
|
|
|
return this.isClosedPromise;
|
|
|
|
}
|
|
|
|
|
2019-04-01 15:09:59 -04:00
|
|
|
postMessage(data: any): void {
|
|
|
|
hostPostMessage(this.rid, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async run(): Promise<void> {
|
|
|
|
while (!this.isClosing) {
|
|
|
|
const data = await hostGetMessage(this.rid);
|
|
|
|
if (data == null) {
|
|
|
|
log("worker got null message. quitting.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// TODO(afinch7) stop this from eating messages before onmessage has been assigned
|
|
|
|
if (this.onmessage) {
|
|
|
|
const event = { data };
|
|
|
|
this.onmessage(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|