Skip to content

Commit

Permalink
fix: Fix crash due to unhandled Promise exception #20473
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Jan 3, 2024
1 parent 7e37365 commit e6bcf91
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/eventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,14 @@ export default class EventBus {
this.on('stateChange', callback, key);
}

private on(event: string, callback: (...args: unknown[]) => void, key: ListenerKey): void {
private on(event: string, callback: (...args: unknown[]) => (Promise<void> | void), key: ListenerKey): void {
if (!this.callbacksByExtension[key.constructor.name]) this.callbacksByExtension[key.constructor.name] = [];
this.callbacksByExtension[key.constructor.name].push({event, callback});
this.emitter.on(event, callback);
const wrappedCallback = (...args: unknown[]): void => {
// Wrap callback as it may return a Promise which can throw an exception
Promise.resolve(callback(...args)).catch();
};
this.callbacksByExtension[key.constructor.name].push({event, callback: wrappedCallback});
this.emitter.on(event, wrappedCallback);
}

public removeListeners(key: ListenerKey): void {
Expand Down

0 comments on commit e6bcf91

Please sign in to comment.