@superhero/eventflow-spoke 4.0.4 → 4.0.6

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/index.js +44 -15
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -284,27 +284,56 @@ export default class Spoke
284
284
 
285
285
  return new Promise(async (accept, reject) =>
286
286
  {
287
- const waitTimeout = setTimeout(() =>
287
+ const timeoutId = setTimeout(() =>
288
288
  {
289
- const error = new Error(`wait timed out (${timeout}) for ${domain} › ${pid} › ${eventNames.join(' | ')}`)
290
- error.code = 'E_EVENTFLOW_WAIT_TIMEOUT'
291
- reject(error)
289
+ unsubscribe().catch(reject).then(() =>
290
+ {
291
+ const error = new Error(`wait timed out (${timeout}) for ${domain} › ${pid} › ${eventNames.join(' | ')}`)
292
+ error.code = 'E_EVENTFLOW_WAIT_TIMEOUT'
293
+ reject(error)
294
+ })
292
295
  }, timeout)
293
296
 
294
- const subscriber = (event) =>
295
- {
296
- if(event.pid === pid)
297
+ const
298
+ unsubscribe = async () =>
297
299
  {
298
- Promise.allSettled(eventNames.map((name) => this.unsubscribe(domain, name, subscriber)))
299
- .then(() =>
300
- {
301
- clearTimeout(waitTimeout)
302
- accept(event)
303
- })
300
+ const
301
+ unsubscribers = eventNames.map((name) => this.unsubscribe(domain, name, subscriber)),
302
+ results = await Promise.allSettled(unsubscribers),
303
+ rejections = results.filter((result) => 'rejected' === result.status)
304
+
305
+ if(rejections.length)
306
+ {
307
+ const error = new Error(`unsubscribe failed: ${domain} › ${pid} › ${eventNames.join(' | ')}`)
308
+ error.code = 'E_EVENTFLOW_WAIT_UNSUBSCRIBE_FAILED'
309
+ error.cause = rejections.map((result) => result.reason)
310
+ throw error
311
+ }
312
+ },
313
+ subscriber = async (event) =>
314
+ {
315
+ if(event.pid === pid)
316
+ {
317
+ clearTimeout(timeoutId)
318
+ await unsubscribe().catch(reject)
319
+ accept(event)
320
+ }
304
321
  }
305
- }
306
322
 
307
- await Promise.allSettled(eventNames.map((name) => this.subscribe(domain, name, subscriber)))
323
+ const
324
+ subscribers = eventNames.map((name) => this.subscribe(domain, name, subscriber)),
325
+ results = await Promise.allSettled(subscribers),
326
+ rejections = results.filter((result) => 'rejected' === result.status)
327
+
328
+ if(rejections.length)
329
+ {
330
+ clearTimeout(timeoutId)
331
+ await unsubscribe().catch(reject)
332
+ const error = new Error(`subscribe failed: ${domain} › ${pid} › ${eventNames.join(' | ')}`)
333
+ error.code = 'E_EVENTFLOW_WAIT_SUBSCRIBE_FAILED'
334
+ error.cause = rejections.map((result) => result.reason)
335
+ reject(error)
336
+ }
308
337
  })
309
338
  }
310
339
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superhero/eventflow-spoke",
3
- "version": "4.0.4",
3
+ "version": "4.0.6",
4
4
  "description": "Eventflow spoke is the client component in the eventflow ecosystem.",
5
5
  "keywords": [
6
6
  "eventflow",
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "@superhero/deep": "^4.2.0",
19
- "@superhero/eventflow-certificates": "^4.0.5",
19
+ "@superhero/eventflow-certificates": "^4.0.7",
20
20
  "@superhero/eventflow-db": "^4.2.3",
21
21
  "@superhero/id-name-generator": "^4.0.0",
22
22
  "@superhero/log": "^4.0.1",