diff --git a/src/utils/EventEmitter.ts b/src/utils/EventEmitter.ts index 96c3407a2..c6ac89ad5 100644 --- a/src/utils/EventEmitter.ts +++ b/src/utils/EventEmitter.ts @@ -11,7 +11,11 @@ export class EventEmitter { } emit(...args: [...T]): void { - for (const sub of this.subscribers) { + // It is necessary to make a copy of the subscribers list, because since + // the subscribers call arbitrary code, it can eventually call back in and + // subscribe or unsubscribe new listeners. We must only dispatch to the + // ones that were active at the time the event came in. + for (const sub of [...this.subscribers]) { sub(...args); } }