Skip to content

Commit

Permalink
lib: modernise cluster/worker
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsankhfr committed Jul 9, 2024
1 parent 230c181 commit 0e2d432
Showing 1 changed file with 31 additions and 37 deletions.
68 changes: 31 additions & 37 deletions lib/internal/cluster/worker.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,51 @@
'use strict';

const {
ObjectSetPrototypeOf,
ReflectApply,
} = primordials;

const EventEmitter = require('events');

const { kEmptyObject } = require('internal/util');

module.exports = Worker;

// Common Worker implementation shared between the cluster primary and workers.
function Worker(options) {
if (!(this instanceof Worker))
return new Worker(options);

ReflectApply(EventEmitter, this, []);
class Worker extends EventEmitter {
constructor(options) {
super();

if (options === null || typeof options !== 'object')
options = kEmptyObject;
if (options === null || typeof options !== 'object')
options = kEmptyObject;

this.exitedAfterDisconnect = undefined;
this.exitedAfterDisconnect = undefined;

this.state = options.state || 'none';
this.id = options.id | 0;
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),
);
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);
kill() {
ReflectApply(this.destroy, this, arguments);
}

Worker.prototype.kill = function() {
ReflectApply(this.destroy, this, arguments);
};
send() {
return ReflectApply(this.process.send, this.process, arguments);
}

Worker.prototype.send = function() {
return ReflectApply(this.process.send, this.process, arguments);
};
isDead() {
return this.process.exitCode != null || this.process.signalCode != null;
}

Worker.prototype.isDead = function() {
return this.process.exitCode != null || this.process.signalCode != null;
};
isConnected() {
return this.process.connected;
}
}

Worker.prototype.isConnected = function() {
return this.process.connected;
};
module.exports = Worker;

0 comments on commit 0e2d432

Please sign in to comment.