|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | const {
|
4 |
| - ObjectSetPrototypeOf, |
5 | 4 | ReflectApply,
|
6 | 5 | } = primordials;
|
7 | 6 |
|
8 | 7 | const EventEmitter = require('events');
|
9 | 8 |
|
10 | 9 | const { kEmptyObject } = require('internal/util');
|
11 | 10 |
|
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(); |
20 | 14 |
|
21 |
| - if (options === null || typeof options !== 'object') |
22 |
| - options = kEmptyObject; |
| 15 | + if (options === null || typeof options !== 'object') |
| 16 | + options = kEmptyObject; |
23 | 17 |
|
24 |
| - this.exitedAfterDisconnect = undefined; |
| 18 | + this.exitedAfterDisconnect = undefined; |
25 | 19 |
|
26 |
| - this.state = options.state || 'none'; |
27 |
| - this.id = options.id | 0; |
| 20 | + this.state = options.state || 'none'; |
| 21 | + this.id = options.id | 0; |
28 | 22 |
|
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 | + } |
37 | 32 | }
|
38 |
| -} |
39 | 33 |
|
40 |
| -ObjectSetPrototypeOf(Worker.prototype, EventEmitter.prototype); |
41 |
| -ObjectSetPrototypeOf(Worker, EventEmitter); |
| 34 | + kill() { |
| 35 | + ReflectApply(this.destroy, this, arguments); |
| 36 | + } |
42 | 37 |
|
43 |
| -Worker.prototype.kill = function() { |
44 |
| - ReflectApply(this.destroy, this, arguments); |
45 |
| -}; |
| 38 | + send() { |
| 39 | + return ReflectApply(this.process.send, this.process, arguments); |
| 40 | + } |
46 | 41 |
|
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 | + } |
50 | 45 |
|
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 | +} |
54 | 50 |
|
55 |
| -Worker.prototype.isConnected = function() { |
56 |
| - return this.process.connected; |
57 |
| -}; |
| 51 | +module.exports = Worker; |
0 commit comments