Skip to content

Commit 0e2d432

Browse files
committed
lib: modernise cluster/worker
1 parent 230c181 commit 0e2d432

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

lib/internal/cluster/worker.js

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,51 @@
11
'use strict';
22

33
const {
4-
ObjectSetPrototypeOf,
54
ReflectApply,
65
} = primordials;
76

87
const EventEmitter = require('events');
98

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

12-
module.exports = Worker;
13-
14-
// Common Worker implementation shared between the cluster primary and workers.
15-
function Worker(options) {
16-
if (!(this instanceof Worker))
17-
return new Worker(options);
18-
19-
ReflectApply(EventEmitter, this, []);
11+
class Worker extends EventEmitter {
12+
constructor(options) {
13+
super();
2014

21-
if (options === null || typeof options !== 'object')
22-
options = kEmptyObject;
15+
if (options === null || typeof options !== 'object')
16+
options = kEmptyObject;
2317

24-
this.exitedAfterDisconnect = undefined;
18+
this.exitedAfterDisconnect = undefined;
2519

26-
this.state = options.state || 'none';
27-
this.id = options.id | 0;
20+
this.state = options.state || 'none';
21+
this.id = options.id | 0;
2822

29-
if (options.process) {
30-
this.process = options.process;
31-
this.process.on('error', (code, signal) =>
32-
this.emit('error', code, signal),
33-
);
34-
this.process.on('message', (message, handle) =>
35-
this.emit('message', message, handle),
36-
);
23+
if (options.process) {
24+
this.process = options.process;
25+
this.process.on('error', (code, signal) =>
26+
this.emit('error', code, signal),
27+
);
28+
this.process.on('message', (message, handle) =>
29+
this.emit('message', message, handle),
30+
);
31+
}
3732
}
38-
}
3933

40-
ObjectSetPrototypeOf(Worker.prototype, EventEmitter.prototype);
41-
ObjectSetPrototypeOf(Worker, EventEmitter);
34+
kill() {
35+
ReflectApply(this.destroy, this, arguments);
36+
}
4237

43-
Worker.prototype.kill = function() {
44-
ReflectApply(this.destroy, this, arguments);
45-
};
38+
send() {
39+
return ReflectApply(this.process.send, this.process, arguments);
40+
}
4641

47-
Worker.prototype.send = function() {
48-
return ReflectApply(this.process.send, this.process, arguments);
49-
};
42+
isDead() {
43+
return this.process.exitCode != null || this.process.signalCode != null;
44+
}
5045

51-
Worker.prototype.isDead = function() {
52-
return this.process.exitCode != null || this.process.signalCode != null;
53-
};
46+
isConnected() {
47+
return this.process.connected;
48+
}
49+
}
5450

55-
Worker.prototype.isConnected = function() {
56-
return this.process.connected;
57-
};
51+
module.exports = Worker;

0 commit comments

Comments
 (0)