Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: allow synchronously draining message ports #26686

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ The above example spawns a Worker thread for each `parse()` call. In actual
practice, use a pool of Workers instead for these kinds of tasks. Otherwise, the
overhead of creating Workers would likely exceed their benefit.

## worker.drainMessagePort(port)
<!-- YAML
added: REPLACEME
-->

* `port` {MessagePort}

Make `port` emit all currently pending messages synchronously.

```js
const { MessageChannel, drainMessagePort } = require('worker_threads');
const { port1, port2 } = new MessageChannel();

port1.postMessage({ hello: 'world' });
port2.once('message', (message) => console.log(message));

drainMessagePort(port2);
console.log('done emitting messages'); // This is printed *after* the message.
```

## worker.isMainThread
<!-- YAML
added: v10.5.0
Expand Down
1 change: 1 addition & 0 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ function pipeWithoutWarning(source, dest) {
}

module.exports = {
drainMessagePort,
ownsProcessState,
isMainThread,
threadId,
Expand Down
2 changes: 2 additions & 0 deletions lib/worker_threads.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
drainMessagePort,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: FWIW, would it be better if we require drainMessagePort from internal/worker/io directly instead of internal/worker (then doesn't need to export in internal/worker), since it was exported from internal/worker/io originally.

module.exports = {
drainMessagePort,

isMainThread,
threadId,
Worker
Expand All @@ -13,6 +14,7 @@ const {
} = require('internal/worker/io');

module.exports = {
drainMessagePort,
isMainThread,
MessagePort,
MessageChannel,
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-worker-message-port-drain-manual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
require('../common');
const assert = require('assert');
const { MessageChannel, drainMessagePort } = require('worker_threads');

const { port1, port2 } = new MessageChannel();
const messages = [];
port2.on('message', (message) => messages.push(message));

const message = { hello: 'world' };

assert.deepStrictEqual(messages, []);
port1.postMessage(message);
port1.postMessage(message);
assert.deepStrictEqual(messages, []);
drainMessagePort(port2);
assert.deepStrictEqual(messages, [ message, message ]);

port1.close();