Skip to content

Commit

Permalink
[Squash] Address feedback
Browse files Browse the repository at this point in the history
1. Rename to `setEnvironmentData()`/`environmentData`
2. Make `environmentData` a `Map`
3. `setEnvironmentData()` accepts a key and value.
4. Marked experimental
  • Loading branch information
jasnell committed Feb 23, 2021
1 parent 432b3d1 commit 044ddc8
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 61 deletions.
73 changes: 41 additions & 32 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,35 @@ Worker threads inherit non-process-specific options by default. Refer to
[`Worker constructor options`][] to know how to customize worker thread options,
specifically `argv` and `execArgv` options.

## `worker.environmentData`
<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental
Within a worker thread, `worker.environmentData` is a {Map} containing a clone
of data passed to the spawning thread's `worker.setEnvironmentData()` function.
The `worker.environmentData` is similar to `worker.workerData` except that
every new `Worker` receives it's own copy of `worker.environmentData`
automatically.

```js
const {
Worker,
isMainThread,
setEnvironmentData,
environmentData
} = require('worker_threads');

if (isMainThread) {
setEnvironmentData('Hello', 'World!');
const worker = new Worker(__filename);
} else {
console.log(environmentData.get('Hello')); // Prints 'World!'.
}
```

## `worker.isMainThread`
<!-- YAML
added: v10.5.0
Expand Down Expand Up @@ -176,32 +205,6 @@ if (isMainThread) {
}
```

## `worker.platformData`
<!-- YAML
added: REPLACEME
-->

An arbitrary JavaScript value that contains a clone of the data passed
to the spawning threads `worker.setPlatformData()` function. The
`worker.platformData` is similar to `worker.workerData` except that
every new `Worker` receives it's own copy of `platformData` automatically.

```js
const {
Worker,
isMainThread,
setPlatformData,
platformData
} = require('worker_threads');

if (isMainThread) {
setPlatformData('Hello World!');
const worker = new Worker(__filename);
} else {
console.log(platformData); // Prints 'Hello, world!'.
}
```

## `worker.receiveMessageOnPort(port)`
<!-- YAML
added: v12.3.0
Expand Down Expand Up @@ -268,19 +271,25 @@ new Worker('process.env.SET_IN_WORKER = "foo"', { eval: true, env: SHARE_ENV })
});
```

## `worker.setPlatformData(value)`
## `worker.setEnvironmentData(key[, value])`
<!--YAML
added: REPLACEME
-->

> Stability: 1 - Experimental
* `key` {any} Any arbitrary, cloneable JavaScript value that can be used as a
{Map} key.
* `value` {any} Any arbitrary, cloneable JavaScript value that will be cloned
and passed automatically to all new `Worker` instances.
and passed automatically to all new `Worker` instances. If `value` is passed
as `undefined`, any previously set value for the `key` will be deleted.

The `worker.setPlatformData()` API sets the value of the `worker.platformData`
in all new `Worker` instances spawned from the current context.
The `worker.setEnvironmentData()` API sets the content of the
`worker.environmentData` in all new `Worker` instances spawned from the current
context.

Calling `worker.setPlatformData()` will have no impact on the value of
`worker.platformData` on existing threads.
Calling `worker.setEnvironmentData()` will have no impact on the value of
`worker.environmentData` on existing threads.

## `worker.threadId`
<!-- YAML
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ port.on('message', (message) => {
filename,
doEval,
workerData,
platformData,
environmentData,
publicPort,
manifestSrc,
manifestURL,
Expand All @@ -131,7 +131,7 @@ port.on('message', (message) => {
}
publicWorker.parentPort = publicPort;
publicWorker.workerData = workerData;
publicWorker.platformData = platformData;
publicWorker.environmentData = environmentData;

// The counter is only passed to the workers created by the main thread, not
// to workers created by other workers.
Expand Down
15 changes: 10 additions & 5 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
ReflectApply,
RegExpPrototypeTest,
SafeArrayIterator,
SafeMap,
String,
Symbol,
SymbolFor,
Expand Down Expand Up @@ -90,7 +91,7 @@ let debug = require('internal/util/debuglog').debuglog('worker', (fn) => {

let cwdCounter;

let platformData;
const environmentData = new SafeMap();

if (isMainThread) {
cwdCounter = new Uint32Array(new SharedArrayBuffer(4));
Expand All @@ -101,9 +102,13 @@ if (isMainThread) {
};
}

function setPlatformData(value) {
platformData = value;
function setEnvironmentData(key, value) {
if (value === undefined)
environmentData.delete(key);
else
environmentData.set(key, value);
}

class Worker extends EventEmitter {
constructor(filename, options = {}) {
super();
Expand Down Expand Up @@ -233,7 +238,7 @@ class Worker extends EventEmitter {
doEval,
cwdCounter: cwdCounter || workerIo.sharedCwdCounter,
workerData: options.workerData,
platformData,
environmentData,
publicPort: port2,
manifestURL: getOptionValue('--experimental-policy') ?
require('internal/process/policy').url :
Expand Down Expand Up @@ -495,7 +500,7 @@ module.exports = {
SHARE_ENV,
resourceLimits:
!isMainThread ? makeResourceLimits(resourceLimitsRaw) : {},
setPlatformData,
setEnvironmentData,
threadId,
Worker,
};
6 changes: 3 additions & 3 deletions lib/worker_threads.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {
isMainThread,
SHARE_ENV,
resourceLimits,
setPlatformData,
setEnvironmentData,
threadId,
Worker
} = require('internal/worker');
Expand Down Expand Up @@ -34,7 +34,7 @@ module.exports = {
Worker,
parentPort: null,
workerData: null,
platformData: null,
environmentData: null,
BroadcastChannel,
setPlatformData,
setEnvironmentData,
};
28 changes: 28 additions & 0 deletions test/parallel/test-worker-environmentdata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

require('../common');
const {
Worker,
isMainThread,
environmentData,
setEnvironmentData,
} = require('worker_threads');

const {
deepStrictEqual,
strictEqual,
} = require('assert');

if (isMainThread) {
setEnvironmentData('foo', 'bar');
setEnvironmentData('hello', { value: 'world' });
setEnvironmentData(1, 2);
setEnvironmentData(1); // Delete it, key won't show up in the worker.
strictEqual(environmentData, null);
new Worker(__filename);
setEnvironmentData('hello'); // Delete it. Has no impact on the worker.
} else {
strictEqual(environmentData.get('foo'), 'bar');
deepStrictEqual(environmentData.get('hello'), { value: 'world' });
strictEqual(environmentData.get(1), undefined);
}
19 changes: 0 additions & 19 deletions test/parallel/test-worker-platform-data.js

This file was deleted.

0 comments on commit 044ddc8

Please sign in to comment.