mirror of
https://github.com/nodejs/node.git
synced 2025-05-11 14:29:19 +00:00

Store all primordials as properties of the primordials object. Static functions are prefixed by the constructor's name and prototype methods are prefixed by the constructor's name followed by "Prototype". For example: primordials.Object.keys becomes primordials.ObjectKeys. PR-URL: https://github.com/nodejs/node/pull/30610 Refs: https://github.com/nodejs/node/issues/29766 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
55 lines
1.2 KiB
JavaScript
55 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
ObjectSetPrototypeOf,
|
|
} = primordials;
|
|
|
|
const EventEmitter = require('events');
|
|
|
|
module.exports = Worker;
|
|
|
|
// Common Worker implementation shared between the cluster master and workers.
|
|
function Worker(options) {
|
|
if (!(this instanceof Worker))
|
|
return new Worker(options);
|
|
|
|
EventEmitter.call(this);
|
|
|
|
if (options === null || typeof options !== 'object')
|
|
options = {};
|
|
|
|
this.exitedAfterDisconnect = undefined;
|
|
|
|
this.state = options.state || 'none';
|
|
this.id = options.id | 0;
|
|
|
|
if (options.process) {
|
|
this.process = options.process;
|
|
this.process.on('error', (code, signal) =>
|
|
this.emit('error', code, signal)
|
|
);
|
|
this.process.on('message', (message, handle) =>
|
|
this.emit('message', message, handle)
|
|
);
|
|
}
|
|
}
|
|
|
|
ObjectSetPrototypeOf(Worker.prototype, EventEmitter.prototype);
|
|
ObjectSetPrototypeOf(Worker, EventEmitter);
|
|
|
|
Worker.prototype.kill = function() {
|
|
this.destroy.apply(this, arguments);
|
|
};
|
|
|
|
Worker.prototype.send = function() {
|
|
return this.process.send.apply(this.process, arguments);
|
|
};
|
|
|
|
Worker.prototype.isDead = function() {
|
|
return this.process.exitCode != null || this.process.signalCode != null;
|
|
};
|
|
|
|
Worker.prototype.isConnected = function() {
|
|
return this.process.connected;
|
|
};
|