@platformatic/itc 2.0.0-alpha.3 → 2.0.0-alpha.5

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.
Files changed (2) hide show
  1. package/lib/itc.js +23 -6
  2. package/package.json +1 -1
package/lib/itc.js CHANGED
@@ -38,7 +38,8 @@ class ITC extends EventEmitter {
38
38
  }
39
39
 
40
40
  const request = this.#generateRequest(name, message)
41
- this.port.postMessage(request)
41
+
42
+ this._send(request)
42
43
 
43
44
  const responsePromise = once(this.#requestEmitter, request.reqId).then(([response]) => response)
44
45
 
@@ -49,7 +50,7 @@ class ITC extends EventEmitter {
49
50
  }
50
51
 
51
52
  async notify (name, message) {
52
- this.port.postMessage(this.#generateNotification(name, message))
53
+ this._send(this.#generateNotification(name, message))
53
54
  }
54
55
 
55
56
  handle (message, handler) {
@@ -62,7 +63,7 @@ class ITC extends EventEmitter {
62
63
  }
63
64
  this.#listening = true
64
65
 
65
- this.port.on('message', message => {
66
+ this._setupListener(message => {
66
67
  const messageType = message.type
67
68
  if (messageType === PLT_ITC_REQUEST_TYPE) {
68
69
  this.#handleRequest(message)
@@ -80,7 +81,7 @@ class ITC extends EventEmitter {
80
81
  }
81
82
  })
82
83
 
83
- this.#closePromise = once(this.port, 'close').then(() => {
84
+ this.#closePromise = this._createClosePromise().then(() => {
84
85
  this.#listening = false
85
86
  const error = new errors.MessagePortClosed()
86
87
  return { error, data: null }
@@ -93,6 +94,22 @@ class ITC extends EventEmitter {
93
94
  return
94
95
  }
95
96
 
97
+ this._close()
98
+ }
99
+
100
+ _setupListener (listener) {
101
+ this.port.on('message', listener)
102
+ }
103
+
104
+ _send (request) {
105
+ this.port.postMessage(request)
106
+ }
107
+
108
+ _createClosePromise () {
109
+ return once(this.port, 'close')
110
+ }
111
+
112
+ _close () {
96
113
  this.port.close()
97
114
  }
98
115
 
@@ -130,7 +147,7 @@ class ITC extends EventEmitter {
130
147
  this.#handling = false
131
148
  }
132
149
 
133
- this.port.postMessage(response)
150
+ this._send(response)
134
151
 
135
152
  if (this.#closeAfterCurrentRequest) {
136
153
  this.close()
@@ -155,7 +172,7 @@ class ITC extends EventEmitter {
155
172
  response = this.#parseResponse(response)
156
173
  } catch (error) {
157
174
  response = this.#generateUnhandledErrorResponse(error)
158
- this.port.postMessage(response)
175
+ this._send(response)
159
176
  return
160
177
  }
161
178
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/itc",
3
- "version": "2.0.0-alpha.3",
3
+ "version": "2.0.0-alpha.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Matteo Collina <hello@matteocollina.com>",