diff --git a/doc/api/process.md b/doc/api/process.md index 1eff6a5b261abc..e0ab670747516b 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -457,6 +457,15 @@ of the custom deprecation. The `*-deprecation` command-line flags only affect warnings that use the name `'DeprecationWarning'`. +### Event: `'worker'` + + +* `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 diff --git a/lib/internal/worker.js b/lib/internal/worker.js index f1da0d4ded4cea..f2414ebeec4aae 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -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) { diff --git a/test/parallel/test-worker-event.js b/test/parallel/test-worker-event.js new file mode 100644 index 00000000000000..01e95ead8316cb --- /dev/null +++ b/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 }); diff --git a/tools/doc/type-parser.js b/tools/doc/type-parser.js index f2b953358d03ce..4fd91f4b478149 100644 --- a/tools/doc/type-parser.js +++ b/tools/doc/type-parser.js @@ -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',