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

process: add 'worker' event #38659

Closed
wants to merge 2 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/api/process.md
Expand Up @@ -457,6 +457,15 @@ of the custom deprecation.
The `*-deprecation` command-line flags only affect warnings that use the name
`'DeprecationWarning'`.

### Event: `'worker'`
<!-- YAML
added: REPLACEME
-->

* `worker` {Worker} The {Worker} that was created.

The `'worker'` event is emitted after a new {Worker} thread has been created.

#### Emitting custom warnings

See the [`process.emitWarning()`][process_emit_warning] method for issuing
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/worker.js
Expand Up @@ -266,6 +266,8 @@ class Worker extends EventEmitter {
};
// Actually start the new thread now that everything is in place.
this[kHandle].startThread();

process.nextTick(() => process.emit('worker', this));
}

[kOnExit](code, customErr, customErrReason) {
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-worker-event.js
@@ -0,0 +1,14 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const {
Worker,
threadId: parentThreadId,
} = require('worker_threads');

process.on('worker', common.mustCall(({ threadId }) => {
assert.strictEqual(threadId, parentThreadId + 1);
}));

new Worker('', { eval: true });
1 change: 1 addition & 0 deletions tools/doc/type-parser.js
Expand Up @@ -223,6 +223,7 @@ const customTypesMap = {
'vm.SourceTextModule': 'vm.html#vm_class_vm_sourcetextmodule',

'MessagePort': 'worker_threads.html#worker_threads_class_messageport',
'Worker': 'worker_threads.html#worker_threads_class_worker',

'X509Certificate': 'crypto.html#crypto_class_x509certificate',

Expand Down