bare-worker 3.0.1 → 3.1.1

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 CHANGED
@@ -22,12 +22,22 @@ module.exports = exports = class Worker extends MessagePort {
22
22
  })
23
23
 
24
24
  this._exitCode = 0
25
+ this._terminating = null
25
26
 
26
- this.on('close', this._onexit).start()
27
+ this.start()
27
28
  }
28
29
 
29
30
  terminate() {
31
+ if (this._terminating !== null) return this._terminating.promise
32
+
33
+ if (this._state & constants.state.CLOSED) {
34
+ return Promise.resolve(this._exitCode)
35
+ }
36
+
37
+ this._terminating = Promise.withResolvers()
30
38
  this._terminate()
39
+
40
+ return this._terminating.promise
31
41
  }
32
42
 
33
43
  [Symbol.for('bare.inspect')]() {
@@ -38,8 +48,13 @@ module.exports = exports = class Worker extends MessagePort {
38
48
  }
39
49
  }
40
50
 
41
- _onexit() {
51
+ _onclose() {
52
+ super._onclose()
53
+
42
54
  this._thread.join()
55
+
56
+ if (this._terminating !== null) this._terminating.resolve(this._exitCode)
57
+
43
58
  this.emit('exit', this._exitCode)
44
59
  }
45
60
  }
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-worker",
3
- "version": "3.0.1",
3
+ "version": "3.1.1",
4
4
  "description": "Higher-level worker threads for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",