-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ext/node): worker_threads doesn't exit if there are message liste…
- Loading branch information
1 parent
e40f9a5
commit c342cd3
Showing
7 changed files
with
91 additions
and
36 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
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 |
---|---|---|
@@ -1,9 +1,19 @@ | ||
import { isMainThread, Worker } from "node:worker_threads"; | ||
import { isMainThread, parentPort, Worker } from "node:worker_threads"; | ||
|
||
function onMessageOneshot() { | ||
console.log("Got message from main thread!"); | ||
parentPort.off("message", onMessageOneshot); | ||
} | ||
|
||
if (isMainThread) { | ||
// This re-loads the current file inside a Worker instance. | ||
const w = new Worker(import.meta.filename); | ||
|
||
setTimeout(() => { | ||
w.postMessage("Hello! I am from the main thread."); | ||
}, 500); | ||
} else { | ||
console.log("Inside Worker!"); | ||
console.log(isMainThread); // Prints 'false'. | ||
parentPort.on("message", onMessageOneshot); | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
Inside Worker! | ||
false | ||
Got message from main thread! |