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.
fix(ext/node): add basic node:worker_threads support (denoland#19192)
This PR restores `node:worker_threads` implementation and test cases from [`[email protected]/node`](https://github.com/denoland/deno_std/blob/0.175.0/node/worker_threads.ts). --------- Co-authored-by: Bartek Iwańczuk <[email protected]>
- Loading branch information
1 parent
3d86594
commit 26f42a2
Showing
6 changed files
with
327 additions
and
87 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,34 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
import { | ||
getEnvironmentData, | ||
isMainThread, | ||
parentPort, | ||
threadId, | ||
workerData, | ||
} from "node:worker_threads"; | ||
import { once } from "node:events"; | ||
|
||
async function message(expectedMessage) { | ||
const [message] = await once(parentPort, "message"); | ||
if (message !== expectedMessage) { | ||
console.log(`Expected the message "${expectedMessage}", but got`, message); | ||
// fail test | ||
parentPort.close(); | ||
} | ||
} | ||
|
||
await message("Hello, how are you my thread?"); | ||
|
||
parentPort.postMessage("I'm fine!"); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
|
||
parentPort.postMessage({ | ||
isMainThread, | ||
threadId, | ||
workerData: Array.isArray(workerData) && | ||
workerData[workerData.length - 1] instanceof MessagePort | ||
? workerData.slice(0, -1) | ||
: workerData, | ||
envData: [getEnvironmentData("test"), getEnvironmentData(1)], | ||
}); |
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
Oops, something went wrong.