@platformatic/itc 2.0.0-alpha.3 → 2.0.0-alpha.4
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/lib/itc.js +23 -6
- 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
|
-
|
|
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.
|
|
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.
|
|
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 =
|
|
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.
|
|
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.
|
|
175
|
+
this._send(response)
|
|
159
176
|
return
|
|
160
177
|
}
|
|
161
178
|
|