@platformatic/itc 3.14.0 → 3.15.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.
Files changed (2) hide show
  1. package/lib/index.js +15 -8
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -179,7 +179,7 @@ export function sanitize (data, transferList) {
179
179
  }
180
180
 
181
181
  export class ITC extends EventEmitter {
182
- #requestEmitter
182
+ #waitingRequests
183
183
  #handlers
184
184
  #listening
185
185
  #handling
@@ -200,16 +200,13 @@ export class ITC extends EventEmitter {
200
200
  // Without it, it's impossible to know which "side" of the ITC is being used.
201
201
  this.name = name
202
202
  this.port = port
203
- this.#requestEmitter = new EventEmitter()
203
+ this.#waitingRequests = new Map()
204
204
  this.#handlers = new Map()
205
205
  this.#listening = false
206
206
  this.#handling = false
207
207
  this.#closeAfterCurrentRequest = false
208
208
  this.#throwOnMissingHandler = throwOnMissingHandler ?? true
209
209
 
210
- // Make sure the emitter handle a lot of listeners at once before raising a warning
211
- this.#requestEmitter.setMaxListeners(1e3)
212
-
213
210
  /*
214
211
  There some contexts in which a message is sent and the event loop empties up while waiting for a response.
215
212
  For instance @platformatic/astro when doing build with custom commands.
@@ -243,19 +240,24 @@ export class ITC extends EventEmitter {
243
240
  throw new SendBeforeListen()
244
241
  }
245
242
 
243
+ let reqId
246
244
  try {
247
245
  this._enableKeepAlive()
248
246
 
249
247
  const request = generateRequest(name, message)
250
248
  this._send(request, options)
251
249
 
252
- const responsePromise = once(this.#requestEmitter, request.reqId).then(([response]) => response)
250
+ const promiseWithResolvers = Promise.withResolvers()
251
+ reqId = request.reqId
252
+ this.#waitingRequests.set(request.reqId, promiseWithResolvers)
253
253
 
254
- const { error, data } = await Unpromise.race([responsePromise, this.#closePromise])
254
+ const { error, data } = await Unpromise.race([promiseWithResolvers.promise, this.#closePromise])
255
255
 
256
256
  if (error !== null) throw error
257
257
  return data
258
258
  } finally {
259
+ // Clean up the waiting requests map even if an error occurred
260
+ this.#waitingRequests.delete(reqId)
259
261
  this._manageKeepAlive()
260
262
  }
261
263
  }
@@ -388,7 +390,12 @@ export class ITC extends EventEmitter {
388
390
  }
389
391
 
390
392
  _emitResponse (response) {
391
- this.#requestEmitter.emit(response.reqId, response)
393
+ const pending = this.#waitingRequests.get(response.reqId)
394
+ if (!pending) {
395
+ return
396
+ }
397
+
398
+ pending.resolve(response)
392
399
  }
393
400
 
394
401
  _enableKeepAlive () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/itc",
3
- "version": "3.14.0",
3
+ "version": "3.15.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",