forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compile): Add support for web workers in standalone mode
This change enables the use of web workers in binaries produced by `deno compile`. This requires the main module to be present in the compiled module graph, which is why if it is not already, it must be explicitly added as an additional root (see denoland#17663).
- Loading branch information
1 parent
1a4d647
commit becdde1
Showing
6 changed files
with
218 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
worker.js imported from main thread | ||
Starting worker | ||
Hello from worker! | ||
Received 42 | ||
Closing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import "./worker.ts"; | ||
|
||
console.log("Starting worker"); | ||
const worker = new Worker( | ||
new URL("./worker.ts", import.meta.url), | ||
{ type: "module" }, | ||
); | ||
|
||
setTimeout(() => { | ||
worker.postMessage(42); | ||
}, 500); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// This time ./worker.ts is not in the module map, so the worker | ||
// initialization will fail unless worker.js is passed as a side module. | ||
|
||
const worker = new Worker( | ||
new URL("./worker.ts", import.meta.url), | ||
{ type: "module" }, | ||
); | ||
|
||
setTimeout(() => { | ||
worker.postMessage(42); | ||
}, 500); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/// <reference no-default-lib="true" /> | ||
/// <reference lib="deno.worker" /> | ||
|
||
if (import.meta.main) { | ||
console.log("Hello from worker!"); | ||
|
||
addEventListener("message", (evt) => { | ||
console.log(`Received ${evt.data}`); | ||
console.log("Closing"); | ||
self.close(); | ||
}); | ||
} else { | ||
console.log("worker.js imported from main thread"); | ||
} |