1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

BREAKING: make Worker.deno unstable (#5128)

This commit makes "Worker.deno" option unstable.

Added new manual entry "docs/runtime/workers.md".

Removed stale workers tests.
This commit is contained in:
Bartek Iwańczuk 2020-05-07 21:15:59 +02:00 committed by GitHub
parent 2b66b8a03e
commit aca21dad1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 60 additions and 81 deletions

View file

@ -184,6 +184,9 @@ fn op_create_worker(
}; };
let args_name = args.name; let args_name = args.name;
let use_deno_namespace = args.use_deno_namespace; let use_deno_namespace = args.use_deno_namespace;
if use_deno_namespace {
state.check_unstable("Worker.deno");
}
let parent_state = state.clone(); let parent_state = state.clone();
let mut state = state.borrow_mut(); let mut state = state.borrow_mut();
let global_state = state.global_state.clone(); let global_state = state.global_state.clone();

View file

@ -1,23 +0,0 @@
const w1 = new Worker("./039_worker_deno_ns/has_ns.ts", { type: "module" });
const w2 = new Worker("./039_worker_deno_ns/no_ns.ts", { type: "module" });
let w1MsgCount = 0;
let w2MsgCount = 0;
w1.onmessage = (msg): void => {
console.log(msg.data);
w1MsgCount++;
if (w1MsgCount === 1) {
w1.postMessage("CONTINUE");
} else {
w2.postMessage("START");
}
};
w2.onmessage = (msg): void => {
console.log(msg.data);
w2MsgCount++;
if (w2MsgCount === 1) {
w2.postMessage("CONTINUE");
} else {
Deno.exit(0);
}
};
w1.postMessage("START");

View file

@ -1,4 +0,0 @@
has_ns.ts: is window.Deno available: true
[SPAWNED BY has_ns.ts] maybe_ns.ts: is window.Deno available: true
no_ns.ts: is window.Deno available: false
[SPAWNED BY no_ns.ts] maybe_ns.ts: is window.Deno available: false

View file

@ -1,10 +0,0 @@
onmessage = (msg): void => {
if (msg.data === "START") {
postMessage("has_ns.ts: is window.Deno available: " + !!window.Deno);
} else {
const worker = new Worker("./maybe_ns.ts");
worker.onmessage = (msg): void => {
postMessage("[SPAWNED BY has_ns.ts] " + msg.data);
};
}
};

View file

@ -1 +0,0 @@
postMessage("maybe_ns.ts: is window.Deno available: " + !!window.Deno);

View file

@ -1,10 +0,0 @@
onmessage = (msg): void => {
if (msg.data === "START") {
postMessage("no_ns.ts: is window.Deno available: " + !!window.Deno);
} else {
const worker = new Worker("./maybe_ns.ts");
worker.onmessage = (msg): void => {
postMessage("[SPAWNED BY no_ns.ts] " + msg.data);
};
}
};

View file

@ -1,6 +0,0 @@
const b = new Blob(["console.log('code from Blob'); postMessage('DONE')"]);
const blobURL = URL.createObjectURL(b);
const worker = new Worker(blobURL);
worker.onmessage = (): void => {
Deno.exit(0);
};

View file

@ -1 +0,0 @@
code from Blob

View file

@ -1,3 +0,0 @@
const b = new Blob(['throw new Error("hello");']);
const blobURL = URL.createObjectURL(b);
new Worker(blobURL);

View file

@ -1,3 +0,0 @@
[WILDCARD]error: Uncaught Error: hello
[WILDCARD]__anonymous__:1:7
at [WILDCARD]__anonymous__:1:7

View file

@ -985,6 +985,7 @@ fn workers() {
.arg("test") .arg("test")
.arg("--reload") .arg("--reload")
.arg("--allow-net") .arg("--allow-net")
.arg("--unstable")
.arg("workers_test.ts") .arg("workers_test.ts")
.spawn() .spawn()
.unwrap() .unwrap()
@ -1080,17 +1081,6 @@ itest!(_038_checkjs {
output: "038_checkjs.js.out", output: "038_checkjs.js.out",
}); });
// TODO(bartlomieju): re-enable
itest_ignore!(_039_worker_deno_ns {
args: "run --reload 039_worker_deno_ns.ts",
output: "039_worker_deno_ns.ts.out",
});
itest_ignore!(_040_worker_blob {
args: "run --reload 040_worker_blob.ts",
output: "040_worker_blob.ts.out",
});
itest!(_041_dyn_import_eval { itest!(_041_dyn_import_eval {
args: "eval import('./subdir/mod4.js').then(console.log)", args: "eval import('./subdir/mod4.js').then(console.log)",
output: "041_dyn_import_eval.out", output: "041_dyn_import_eval.out",
@ -1471,14 +1461,6 @@ itest!(error_local_static_import_from_remote_js {
output: "error_local_static_import_from_remote.js.out", output: "error_local_static_import_from_remote.js.out",
}); });
// TODO(bartlomieju) Re-enable
itest_ignore!(error_worker_dynamic {
args: "run --reload error_worker_dynamic.ts",
check_stderr: true,
exit_code: 1,
output: "error_worker_dynamic.ts.out",
});
itest!(exit_error42 { itest!(exit_error42 {
exit_code: 42, exit_code: 42,
args: "run --reload exit_error42.ts", args: "run --reload exit_error42.ts",

54
docs/runtime/workers.md Normal file
View file

@ -0,0 +1,54 @@
## Workers
Deno supports
[`Web Worker API`](https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker).
Workers can be used to run code on multiple threads. Each instance of `Worker`
is run on a separate thread, dedicated only to that worker.
Currently Deno supports only `module` type workers; thus it's essential to pass
`type: "module"` option when creating new worker:
```ts
// Good
new Worker("./worker.js", { type: "module" });
// Bad
new Worker("./worker.js");
new Worker("./worker.js", { type: "classic" });
```
### Using Deno in worker
**UNSTABLE**: This feature is unstable and requires `--unstable` flag
By default `Deno` namespace is not available in worker scope.
To add `Deno` namespace pass `deno: true` option when creating new worker:
```ts
// main.js
const worker = new Worker("./worker.js", { type: "module", deno: true });
worker.postMessage({ filename: "./log.txt" });
// worker.js
self.onmessage = async (e) => {
const { filename } = e.data;
const text = await Deno.readTextFile(filename);
console.log(text);
self.close();
};
// log.txt
hello world
```
```shell
$ deno run --allow-read --unstable main.js
hello world
```
When `Deno` namespace is available in worker scope; the worker inherits parent
process permissions (the ones specified using `--allow-*` flags).
We intend to make permissions configurable for workers.

View file

@ -18,7 +18,8 @@
"children": { "children": {
"program_lifecycle": "Program Lifecycle", "program_lifecycle": "Program Lifecycle",
"compiler_apis": "Compiler APIs", "compiler_apis": "Compiler APIs",
"unstable": "Unstable APIs" "unstable": "Unstable APIs",
"workers": "Workers"
} }
}, },
"linking_to_external_code": { "linking_to_external_code": {