summaryrefslogtreecommitdiff
path: root/js/workers.ts
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2019-08-05 04:23:41 -0700
committerRyan Dahl <ry@tinyclouds.org>2019-08-05 07:23:41 -0400
commitddee2dff14772ade16e282ad18eda6f5054ce94e (patch)
tree473cdf935b8d0254a7ab8e0c89fd407201793ac6 /js/workers.ts
parentaaa7a3eac4df0de9a93dc8fc4717d38212a3de5b (diff)
Provide option to delete Deno namespace in worker (#2717)
Diffstat (limited to 'js/workers.ts')
-rw-r--r--js/workers.ts31
1 files changed, 27 insertions, 4 deletions
diff --git a/js/workers.ts b/js/workers.ts
index 1531b960b..f008ccecf 100644
--- a/js/workers.ts
+++ b/js/workers.ts
@@ -20,10 +20,17 @@ export function decodeMessage(dataIntArray: Uint8Array): any {
return JSON.parse(dataJson);
}
-function createWorker(specifier: string): number {
+function createWorker(
+ specifier: string,
+ includeDenoNamespace: boolean
+): number {
const builder = flatbuffers.createBuilder();
const specifier_ = builder.createString(specifier);
- const inner = msg.CreateWorker.createCreateWorker(builder, specifier_);
+ const inner = msg.CreateWorker.createCreateWorker(
+ builder,
+ specifier_,
+ includeDenoNamespace
+ );
const baseRes = sendSync(builder, msg.Any.CreateWorker, inner);
assert(baseRes != null);
assert(
@@ -149,6 +156,18 @@ export interface Worker {
closed: Promise<void>;
}
+// 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;
+}
+
export class WorkerImpl implements Worker {
private readonly rid: number;
private isClosing: boolean = false;
@@ -157,8 +176,12 @@ export class WorkerImpl implements Worker {
public onmessage?: (data: any) => void;
public onmessageerror?: () => void;
- constructor(specifier: string) {
- this.rid = createWorker(specifier);
+ constructor(specifier: string, options?: DenoWorkerOptions) {
+ let includeDenoNamespace = true;
+ if (options && options.noDenoNamespace) {
+ includeDenoNamespace = false;
+ }
+ this.rid = createWorker(specifier, includeDenoNamespace);
this.run();
this.isClosedPromise = hostGetWorkerClosed(this.rid);
this.isClosedPromise.then(