-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
56 lines (41 loc) · 1.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* global Bare */
const Channel = require('bare-channel')
const MessageChannel = require('./lib/message-channel')
const MessagePort = require('./lib/message-port')
const constants = require('./lib/constants')
const { Thread } = Bare
module.exports = exports = class Worker extends MessagePort {
constructor(filename, opts = {}) {
const channel = new Channel({ interfaces: [MessagePort] })
super(channel)
this._state = constants.state.REFED
this._thread = new Thread(require.resolve('./lib/worker-thread'), {
data: {
channel: channel.handle,
filename,
data: opts.workerData
}
})
this._exitCode = 0
this.on('close', this._onexit).start()
}
terminate() {
this._terminate()
}
[Symbol.for('bare.inspect')]() {
return {
__proto__: { constructor: Worker },
detached: this.detached
}
}
_onexit() {
this._thread.join()
this.emit('exit', this._exitCode)
}
}
exports.Worker = exports
exports.MessageChannel = MessageChannel
exports.MessagePort = MessagePort
exports.isMainThread = Thread.isMainThread
exports.parentPort = null
exports.workerData = null