-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker: add setEnvironmentData/getEnvironmentData
These APIs allow arbitrary, cloneable JavaScript values to be set and passed to all new Worker instances spawned from the current context. It is similar to `workerData` except that environment data is set independently of the `new Worker()` constructor, and the the value is passed automatically to all new Workers. This is a *partial* fix of #30992 but does not implement a complete fix. Signed-off-by: James M Snell <[email protected]> PR-URL: #37486 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
- Loading branch information
1 parent
5d79373
commit 0d135e8
Showing
5 changed files
with
114 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const { | ||
Worker, | ||
getEnvironmentData, | ||
setEnvironmentData, | ||
threadId, | ||
} = require('worker_threads'); | ||
|
||
const { | ||
deepStrictEqual, | ||
strictEqual, | ||
} = require('assert'); | ||
|
||
if (!process.env.HAS_STARTED_WORKER) { | ||
process.env.HAS_STARTED_WORKER = 1; | ||
setEnvironmentData('foo', 'bar'); | ||
setEnvironmentData('hello', { value: 'world' }); | ||
setEnvironmentData(1, 2); | ||
strictEqual(getEnvironmentData(1), 2); | ||
setEnvironmentData(1); // Delete it, key won't show up in the worker. | ||
new Worker(__filename); | ||
setEnvironmentData('hello'); // Delete it. Has no impact on the worker. | ||
} else { | ||
strictEqual(getEnvironmentData('foo'), 'bar'); | ||
deepStrictEqual(getEnvironmentData('hello'), { value: 'world' }); | ||
strictEqual(getEnvironmentData(1), undefined); | ||
|
||
// Recurse to make sure the environment data is inherited | ||
if (threadId <= 2) | ||
new Worker(__filename); | ||
} |