Skip to content

Commit 432b3d1

Browse files
committedFeb 23, 2021
worker: add support for platformData
The `worker.platformData` and `worker.setPlatformData()` APIs allow an arbitrary, cloneable JavaScript value to be set and passed to all new Worker instances spawned from the current context. It is similar to `workerData` except that `platformData` 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 nodejs#30992 but does not implement a complete fix. Signed-off-by: James M Snell <[email protected]>
1 parent 66ba0f1 commit 432b3d1

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed
 

‎doc/api/worker_threads.md

+40
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,32 @@ if (isMainThread) {
176176
}
177177
```
178178

179+
## `worker.platformData`
180+
<!-- YAML
181+
added: REPLACEME
182+
-->
183+
184+
An arbitrary JavaScript value that contains a clone of the data passed
185+
to the spawning threads `worker.setPlatformData()` function. The
186+
`worker.platformData` is similar to `worker.workerData` except that
187+
every new `Worker` receives it's own copy of `platformData` automatically.
188+
189+
```js
190+
const {
191+
Worker,
192+
isMainThread,
193+
setPlatformData,
194+
platformData
195+
} = require('worker_threads');
196+
197+
if (isMainThread) {
198+
setPlatformData('Hello World!');
199+
const worker = new Worker(__filename);
200+
} else {
201+
console.log(platformData); // Prints 'Hello, world!'.
202+
}
203+
```
204+
179205
## `worker.receiveMessageOnPort(port)`
180206
<!-- YAML
181207
added: v12.3.0
@@ -242,6 +268,20 @@ new Worker('process.env.SET_IN_WORKER = "foo"', { eval: true, env: SHARE_ENV })
242268
});
243269
```
244270

271+
## `worker.setPlatformData(value)`
272+
<!--YAML
273+
added: REPLACEME
274+
-->
275+
276+
* `value` {any} Any arbitrary, cloneable JavaScript value that will be cloned
277+
and passed automatically to all new `Worker` instances.
278+
279+
The `worker.setPlatformData()` API sets the value of the `worker.platformData`
280+
in all new `Worker` instances spawned from the current context.
281+
282+
Calling `worker.setPlatformData()` will have no impact on the value of
283+
`worker.platformData` on existing threads.
284+
245285
## `worker.threadId`
246286
<!-- YAML
247287
added: v10.5.0

‎lib/internal/main/worker_thread.js

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ port.on('message', (message) => {
105105
filename,
106106
doEval,
107107
workerData,
108+
platformData,
108109
publicPort,
109110
manifestSrc,
110111
manifestURL,
@@ -130,6 +131,7 @@ port.on('message', (message) => {
130131
}
131132
publicWorker.parentPort = publicPort;
132133
publicWorker.workerData = workerData;
134+
publicWorker.platformData = platformData;
133135

134136
// The counter is only passed to the workers created by the main thread, not
135137
// to workers created by other workers.

‎lib/internal/worker.js

+7
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ let debug = require('internal/util/debuglog').debuglog('worker', (fn) => {
9090

9191
let cwdCounter;
9292

93+
let platformData;
94+
9395
if (isMainThread) {
9496
cwdCounter = new Uint32Array(new SharedArrayBuffer(4));
9597
const originalChdir = process.chdir;
@@ -99,6 +101,9 @@ if (isMainThread) {
99101
};
100102
}
101103

104+
function setPlatformData(value) {
105+
platformData = value;
106+
}
102107
class Worker extends EventEmitter {
103108
constructor(filename, options = {}) {
104109
super();
@@ -228,6 +233,7 @@ class Worker extends EventEmitter {
228233
doEval,
229234
cwdCounter: cwdCounter || workerIo.sharedCwdCounter,
230235
workerData: options.workerData,
236+
platformData,
231237
publicPort: port2,
232238
manifestURL: getOptionValue('--experimental-policy') ?
233239
require('internal/process/policy').url :
@@ -489,6 +495,7 @@ module.exports = {
489495
SHARE_ENV,
490496
resourceLimits:
491497
!isMainThread ? makeResourceLimits(resourceLimitsRaw) : {},
498+
setPlatformData,
492499
threadId,
493500
Worker,
494501
};

‎lib/worker_threads.js

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const {
44
isMainThread,
55
SHARE_ENV,
66
resourceLimits,
7+
setPlatformData,
78
threadId,
89
Worker
910
} = require('internal/worker');
@@ -33,5 +34,7 @@ module.exports = {
3334
Worker,
3435
parentPort: null,
3536
workerData: null,
37+
platformData: null,
3638
BroadcastChannel,
39+
setPlatformData,
3740
};
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
require('../common');
4+
const {
5+
Worker,
6+
isMainThread,
7+
platformData,
8+
setPlatformData,
9+
} = require('worker_threads');
10+
11+
const { strictEqual } = require('assert');
12+
13+
if (isMainThread) {
14+
setPlatformData('foo');
15+
strictEqual(platformData, null);
16+
new Worker(__filename);
17+
} else {
18+
strictEqual(platformData, 'foo');
19+
}

0 commit comments

Comments
 (0)
Please sign in to comment.