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 */
|
2019-09-02 17:07:11 -04:00
|
|
|
import * as dispatch from "./dispatch.ts";
|
|
|
|
import { sendAsync, sendSync } from "./dispatch_json.ts";
|
|
|
|
import { log } from "./util.ts";
|
|
|
|
import { TextDecoder, TextEncoder } from "./text_encoding.ts";
|
|
|
|
import { window } from "./window.ts";
|
|
|
|
import { blobURLMap } from "./url.ts";
|
|
|
|
import { blobBytesWeakMap } from "./blob.ts";
|
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);
|
|
|
|
}
|
|
|
|
|
2019-08-05 07:23:41 -04:00
|
|
|
function createWorker(
|
|
|
|
specifier: string,
|
2019-08-06 09:22:11 -04:00
|
|
|
includeDenoNamespace: boolean,
|
|
|
|
hasSourceCode: boolean,
|
|
|
|
sourceCode: Uint8Array
|
2019-08-05 07:23:41 -04:00
|
|
|
): number {
|
2019-08-26 08:50:21 -04:00
|
|
|
return sendSync(dispatch.OP_CREATE_WORKER, {
|
|
|
|
specifier,
|
2019-08-06 09:22:11 -04:00
|
|
|
includeDenoNamespace,
|
|
|
|
hasSourceCode,
|
2019-08-26 08:50:21 -04:00
|
|
|
sourceCode: new TextDecoder().decode(sourceCode)
|
|
|
|
});
|
2019-04-01 15:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function hostGetWorkerClosed(rid: number): Promise<void> {
|
2019-08-26 08:50:21 -04:00
|
|
|
await sendAsync(dispatch.OP_HOST_GET_WORKER_CLOSED, { rid });
|
2019-04-01 15:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function hostPostMessage(rid: number, data: any): void {
|
|
|
|
const dataIntArray = encodeMessage(data);
|
2019-08-26 08:50:21 -04:00
|
|
|
sendSync(dispatch.OP_HOST_POST_MESSAGE, { rid }, dataIntArray);
|
2019-04-01 15:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function hostGetMessage(rid: number): Promise<any> {
|
2019-08-26 08:50:21 -04:00
|
|
|
const res = await sendAsync(dispatch.OP_HOST_GET_MESSAGE, { rid });
|
|
|
|
|
|
|
|
if (res.data != null) {
|
|
|
|
return decodeMessage(new Uint8Array(res.data));
|
2019-04-01 15:09:59 -04:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stuff for workers
|
2019-09-07 12:27:18 -04:00
|
|
|
export const onmessage: (e: { data: any }) => void = (): void => {};
|
2019-04-01 15:09:59 -04:00
|
|
|
|
|
|
|
export function postMessage(data: any): void {
|
|
|
|
const dataIntArray = encodeMessage(data);
|
2019-08-26 08:50:21 -04:00
|
|
|
sendSync(dispatch.OP_WORKER_POST_MESSAGE, {}, dataIntArray);
|
2019-01-08 14:44:06 -05:00
|
|
|
}
|
|
|
|
|
2019-04-01 15:09:59 -04:00
|
|
|
export async function getMessage(): Promise<any> {
|
2019-01-08 14:44:06 -05:00
|
|
|
log("getMessage");
|
2019-08-26 08:50:21 -04:00
|
|
|
const res = await sendAsync(dispatch.OP_WORKER_GET_MESSAGE);
|
|
|
|
if (res.data != null) {
|
|
|
|
return decodeMessage(new Uint8Array(res.data));
|
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;
|
|
|
|
}
|
2019-04-08 17:10:00 -04:00
|
|
|
|
2019-01-08 14:44:06 -05:00
|
|
|
if (window["onmessage"]) {
|
|
|
|
const event = { data };
|
2019-10-03 07:23:29 -04:00
|
|
|
const result: void | Promise<void> = window.onmessage(event);
|
|
|
|
if (result && "then" in result) {
|
|
|
|
await result;
|
|
|
|
}
|
2019-04-08 17:10:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!window["onmessage"]) {
|
2019-01-08 14:44:06 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-05 07:23:41 -04:00
|
|
|
// TODO(kevinkassimo): Maybe implement reasonable web worker options?
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
|
|
export interface WorkerOptions {}
|
|
|
|
|
|
|
|
/** Extended Deno Worker initialization options.
|
|
|
|
* `noDenoNamespace` hides global `window.Deno` namespace for
|
|
|
|
* spawned worker and nested workers spawned by it (default: false).
|
|
|
|
*/
|
|
|
|
export interface DenoWorkerOptions extends WorkerOptions {
|
|
|
|
noDenoNamespace?: boolean;
|
|
|
|
}
|
|
|
|
|
2019-04-01 15:09:59 -04:00
|
|
|
export class WorkerImpl implements Worker {
|
|
|
|
private readonly rid: number;
|
2019-09-07 12:27:18 -04:00
|
|
|
private isClosing = 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;
|
|
|
|
|
2019-08-05 07:23:41 -04:00
|
|
|
constructor(specifier: string, options?: DenoWorkerOptions) {
|
2019-08-06 09:22:11 -04:00
|
|
|
let hasSourceCode = false;
|
|
|
|
let sourceCode = new Uint8Array();
|
|
|
|
|
2019-08-05 07:23:41 -04:00
|
|
|
let includeDenoNamespace = true;
|
|
|
|
if (options && options.noDenoNamespace) {
|
|
|
|
includeDenoNamespace = false;
|
|
|
|
}
|
2019-08-06 09:22:11 -04:00
|
|
|
// Handle blob URL.
|
|
|
|
if (specifier.startsWith("blob:")) {
|
|
|
|
hasSourceCode = true;
|
|
|
|
const b = blobURLMap.get(specifier);
|
|
|
|
if (!b) {
|
|
|
|
throw new Error("No Blob associated with the given URL is found");
|
|
|
|
}
|
|
|
|
const blobBytes = blobBytesWeakMap.get(b!);
|
|
|
|
if (!blobBytes) {
|
|
|
|
throw new Error("Invalid Blob");
|
|
|
|
}
|
|
|
|
sourceCode = blobBytes!;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.rid = createWorker(
|
|
|
|
specifier,
|
|
|
|
includeDenoNamespace,
|
|
|
|
hasSourceCode,
|
|
|
|
sourceCode
|
|
|
|
);
|
2019-04-01 15:09:59 -04:00
|
|
|
this.run();
|
2019-04-05 15:57:59 -04:00
|
|
|
this.isClosedPromise = hostGetWorkerClosed(this.rid);
|
2019-04-21 16:40:10 -04:00
|
|
|
this.isClosedPromise.then(
|
|
|
|
(): void => {
|
|
|
|
this.isClosing = true;
|
|
|
|
}
|
|
|
|
);
|
2019-04-01 15:09:59 -04:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|