bare-worker 4.1.8 → 4.1.9

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.
@@ -1,5 +1,4 @@
1
1
  const Channel = require('bare-channel')
2
- const WorkerState = require('./worker-state')
3
2
  const MessagePort = require('./message-port')
4
3
 
5
4
  module.exports = class MessageChannel {
@@ -10,5 +9,3 @@ module.exports = class MessageChannel {
10
9
  this.port2 = new MessagePort(channel)
11
10
  }
12
11
  }
13
-
14
- if (WorkerState.parent) module.exports = WorkerState.parent.MessageChannel
@@ -1,6 +1,5 @@
1
1
  const EventEmitter = require('bare-events')
2
2
  const Channel = require('bare-channel')
3
- const WorkerState = require('./worker-state')
4
3
  const constants = require('./constants')
5
4
  const errors = require('./errors')
6
5
 
@@ -219,8 +218,6 @@ module.exports = exports = class MessagePort extends EventEmitter {
219
218
  static _ports = new Set()
220
219
  }
221
220
 
222
- if (WorkerState.parent) module.exports = WorkerState.parent.MessagePort
223
-
224
221
  Bare.on('beforeExit', (exitCode) => {
225
222
  for (const port of exports._ports) port._beforeExit(exitCode)
226
223
  })
@@ -1,17 +1,25 @@
1
1
  const Channel = require('bare-channel')
2
+ const MessagePort = require('./message-port')
2
3
 
3
4
  const state = Symbol.for('bare.worker.state')
4
5
  const kind = Symbol.for('bare.worker.state.kind')
5
6
 
6
- class WorkerState {
7
+ module.exports = class WorkerState {
7
8
  static get [kind]() {
8
9
  return 0 // Compatibility version
9
10
  }
10
11
 
11
- constructor() {
12
- this._MessageChannel = require('./message-channel')
13
- this._MessagePort = require('./message-port')
12
+ static get parent() {
13
+ const parent = global[state]
14
+
15
+ if (typeof parent === 'object' && parent !== null && parent[kind] === WorkerState[kind]) {
16
+ return parent
17
+ }
18
+
19
+ return null
20
+ }
14
21
 
22
+ constructor() {
15
23
  const { source, preloads, data, environmentData, channel: handle } = Bare.Thread.self.data
16
24
 
17
25
  this.source = source
@@ -19,9 +27,9 @@ class WorkerState {
19
27
  this.data = Bare.Thread.self.data = data
20
28
  this.environmentData = environmentData
21
29
 
22
- const channel = Channel.from(handle, { interfaces: [this._MessagePort] })
30
+ const channel = Channel.from(handle, { interfaces: [MessagePort] })
23
31
 
24
- this.port = new this._MessagePort(channel)
32
+ this.port = new MessagePort(channel)
25
33
  this.port._online()
26
34
 
27
35
  global[state] = this
@@ -30,24 +38,4 @@ class WorkerState {
30
38
  get [kind]() {
31
39
  return WorkerState[kind]
32
40
  }
33
-
34
- get MessageChannel() {
35
- return this._MessageChannel
36
- }
37
-
38
- get MessagePort() {
39
- return this._MessagePort
40
- }
41
- }
42
-
43
- module.exports = exports = WorkerState
44
-
45
- if (
46
- typeof global[state] === 'object' &&
47
- global[state] !== null &&
48
- global[state][kind] === WorkerState[kind]
49
- ) {
50
- exports.parent = global[state]
51
- } else {
52
- exports.parent = null
53
41
  }
@@ -8,17 +8,11 @@ Bare.on('newListener', onnewlistener)
8
8
  .on('uncaughtException', onerror)
9
9
  .on('unhandledRejection', onerror)
10
10
 
11
- const cache = Object.create(null)
12
-
13
11
  for (const [, source] of state.preloads) {
14
- Module.load(
15
- new URL(`bare:/worker/preload-${Math.random().toString(16).slice(2)}.bundle`),
16
- source,
17
- { cache }
18
- )
12
+ Module.load(new URL(`bare:/worker/preload-${Math.random().toString(16).slice(2)}.bundle`), source)
19
13
  }
20
14
 
21
- Module.load(new URL('bare:/worker.bundle'), state.source, { cache })
15
+ Module.load(new URL('bare:/worker.bundle'), state.source)
22
16
 
23
17
  function onnewlistener(name, fn) {
24
18
  if (fn === onremovelistener || fn === onerror) return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-worker",
3
- "version": "4.1.8",
3
+ "version": "4.1.9",
4
4
  "description": "Higher-level worker threads for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",