bare-worker 3.1.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +11 -3
- package/lib/message-port.js +3 -0
- package/lib/preloads.js +15 -0
- package/lib/worker-thread.js +3 -1
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -2,6 +2,7 @@ const Channel = require('bare-channel')
|
|
|
2
2
|
const MessageChannel = require('./lib/message-channel')
|
|
3
3
|
const MessagePort = require('./lib/message-port')
|
|
4
4
|
const constants = require('./lib/constants')
|
|
5
|
+
const preloads = require('./lib/preloads')
|
|
5
6
|
const { Thread } = Bare
|
|
6
7
|
|
|
7
8
|
module.exports = exports = class Worker extends MessagePort {
|
|
@@ -17,14 +18,15 @@ module.exports = exports = class Worker extends MessagePort {
|
|
|
17
18
|
channel: channel.handle,
|
|
18
19
|
filename,
|
|
19
20
|
data: opts.workerData,
|
|
20
|
-
imports: module.imports
|
|
21
|
+
imports: module.imports,
|
|
22
|
+
preloads
|
|
21
23
|
}
|
|
22
24
|
})
|
|
23
25
|
|
|
24
26
|
this._exitCode = 0
|
|
25
27
|
this._terminating = null
|
|
26
28
|
|
|
27
|
-
this.
|
|
29
|
+
this.start()
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
terminate() {
|
|
@@ -48,7 +50,9 @@ module.exports = exports = class Worker extends MessagePort {
|
|
|
48
50
|
}
|
|
49
51
|
}
|
|
50
52
|
|
|
51
|
-
|
|
53
|
+
_onclose() {
|
|
54
|
+
super._onclose()
|
|
55
|
+
|
|
52
56
|
this._thread.join()
|
|
53
57
|
|
|
54
58
|
if (this._terminating !== null) this._terminating.resolve(this._exitCode)
|
|
@@ -67,3 +71,7 @@ exports.isMainThread = Thread.isMainThread
|
|
|
67
71
|
exports.parentPort = null
|
|
68
72
|
|
|
69
73
|
exports.workerData = null
|
|
74
|
+
|
|
75
|
+
exports.preload = function preload(filename) {
|
|
76
|
+
preloads.add(filename)
|
|
77
|
+
}
|
package/lib/message-port.js
CHANGED
|
@@ -190,6 +190,7 @@ module.exports = exports = class MessagePort extends EventEmitter {
|
|
|
190
190
|
MessagePort._ports.delete(this)
|
|
191
191
|
|
|
192
192
|
this._state |= constants.state.CLOSED
|
|
193
|
+
|
|
193
194
|
this.emit('close')
|
|
194
195
|
}
|
|
195
196
|
|
|
@@ -199,12 +200,14 @@ module.exports = exports = class MessagePort extends EventEmitter {
|
|
|
199
200
|
|
|
200
201
|
_ononline() {
|
|
201
202
|
this._state |= constants.state.ONLINE
|
|
203
|
+
|
|
202
204
|
this.emit('online')
|
|
203
205
|
}
|
|
204
206
|
|
|
205
207
|
_onerror(err) {
|
|
206
208
|
this._exitCode = 1
|
|
207
209
|
this._terminate()
|
|
210
|
+
|
|
208
211
|
this.emit('error', err)
|
|
209
212
|
}
|
|
210
213
|
|
package/lib/preloads.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const { Thread } = Bare
|
|
2
|
+
|
|
3
|
+
const preloads = new Set()
|
|
4
|
+
|
|
5
|
+
if (
|
|
6
|
+
Thread.self &&
|
|
7
|
+
typeof Thread.self.data === 'object' &&
|
|
8
|
+
Thread.self.data !== null &&
|
|
9
|
+
typeof Thread.self.data.preloads === 'object' &&
|
|
10
|
+
Thread.self.data.preloads !== null
|
|
11
|
+
) {
|
|
12
|
+
for (const filename of Thread.self.data.preloads) preloads.add(filename)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
module.exports = preloads
|
package/lib/worker-thread.js
CHANGED
|
@@ -6,7 +6,9 @@ const MessagePort = require('./message-port')
|
|
|
6
6
|
const worker = require('..')
|
|
7
7
|
const { Thread } = Bare
|
|
8
8
|
|
|
9
|
-
const { channel: handle, filename, data, imports } = Thread.self.data
|
|
9
|
+
const { channel: handle, filename, data, imports, preloads } = Thread.self.data
|
|
10
|
+
|
|
11
|
+
for (const preload of preloads) require(preload)
|
|
10
12
|
|
|
11
13
|
const channel = Channel.from(handle, { interfaces: [MessagePort] })
|
|
12
14
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-worker",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "Higher-level worker threads for JavaScript",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./index.js",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"homepage": "https://github.com/holepunchto/bare-worker#readme",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"bare-channel": "^5.
|
|
31
|
+
"bare-channel": "^5.1.5",
|
|
32
32
|
"bare-events": "^2.2.1",
|
|
33
33
|
"bare-module": "^5.0.0",
|
|
34
34
|
"bare-os": "^3.0.1",
|