Skip to content

Commit

Permalink
Fix asyncEventEmitter to not silence unhandled exceptions raised in e…
Browse files Browse the repository at this point in the history
…vent handlers
  • Loading branch information
samwillis committed May 9, 2024
1 parent 8ba900a commit 2aaa322
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/five-rats-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"electric-sql": patch
---

Fix asyncEventEmitter to not silence unhandled exceptions raised in event handlers.
13 changes: 12 additions & 1 deletion clients/typescript/src/util/asyncEventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,18 @@ export class AsyncEventEmitter<Events extends EventMap> {
// deep copy because once listeners mutate the `this.listeners` array as they remove themselves
// which breaks the `map` which iterates over that same array while the contents may shift
const ls = [...listeners]
const listenerProms = ls.map(async (listener) => await listener(...args))
const listenerProms = ls.map(async (listener) => {
try {
await listener(...args)
} catch (e) {
// If a listener throws an error, we re-throw it asynchronously so that the queue can continue
// to be processed, this ensures that the exception isn't swallowed by allSettled below.
// It will likely be caught by a global error handler, or be logged to the console.
queueMicrotask(() => {
throw e
})
}
})

Promise
// wait for all listeners to finish,
Expand Down

0 comments on commit 2aaa322

Please sign in to comment.