diff --git a/CHANGELOG.md b/CHANGELOG.md index 41cf2ffb..46d5b71b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,15 +23,16 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +- Fixed issue where removing handlers by function reference only removed the first registered one - Fixed issue where play button was hidden when going fullscreen mode -- Fix issue where screen resizing caused artifacts on the loading screen -- Fix bug in `useCanvas2DFallback()` where `antialiasing` settings could be lost -- Fix bug in `useCanvas2DFallback()` where opacity was not respected in `save +- Fixed issue where screen resizing caused artifacts on the loading screen +- Fixed bug in `useCanvas2DFallback()` where `antialiasing` settings could be lost +- Fixed bug in `useCanvas2DFallback()` where opacity was not respected in `save - Fixed typo in trigger event signature `entertrigger` should have been `enter` - Fixed typo in trigger event signature `exittrigger` should have been `exit` - Fixed typo in animation event signature `ended` should have been `end` -- Fixed issue where some excalibur `clear()`` implementations modified the collection they were iterating over -- Fixed async issue where sound could not be stopped if stop()/start() were called in rapid succession +- Fixed issue where some excalibur `clear()` implementations modified the collection they were iterating over +- Fixed async issue where sound could not be stopped if `stop()`/`start()` were called in rapid succession - Fixed issue with input mapper where `keyboard.wasPressed(...)` did not fire - Fixed issue issue where TileMaps would not properly draw Tiles when setup in screen space coordinates - Fixed issue where the ex.Line graphics bounds were incorrect causing erroneous offscreen culling diff --git a/src/engine/EventEmitter.ts b/src/engine/EventEmitter.ts index 0fd48978..144bd7e9 100644 --- a/src/engine/EventEmitter.ts +++ b/src/engine/EventEmitter.ts @@ -49,14 +49,11 @@ export class EventEmitter { off(eventName: string): void; off | string>(eventName: TEventName, handler?: Handler): void { if (handler) { - const listenerIndex = this._listeners[eventName]?.indexOf(handler); - if (listenerIndex > -1) { - this._listeners[eventName]?.splice(listenerIndex, 1); - } - const onceIndex = this._listenersOnce[eventName]?.indexOf(handler); - if (onceIndex > -1) { - this._listenersOnce[eventName]?.splice(onceIndex, 1); - } + const newListeners = this._listeners[eventName]?.filter(h => h !== handler); + this._listeners[eventName] = newListeners; + + const newOnceListeners = this._listenersOnce[eventName]?.filter(h => h !== handler); + this._listenersOnce[eventName] = newOnceListeners; } else { delete this._listeners[eventName]; } diff --git a/src/spec/EventEmitterSpec.ts b/src/spec/EventEmitterSpec.ts index d3e89c06..f95adb4b 100644 --- a/src/spec/EventEmitterSpec.ts +++ b/src/spec/EventEmitterSpec.ts @@ -83,6 +83,34 @@ describe('An EventEmitter', () => { expect(handler).toHaveBeenCalledTimes(3); }); + it('can be switched off by name and handler for multiple installs', () => { + const emitter = new ex.EventEmitter(); + const handler = jasmine.createSpy('handler'); + + emitter.on('myevent2', handler); + emitter.on('myevent2', handler); + + emitter.off('myevent2', handler); + + emitter.emit('myevent2'); + + expect(handler).toHaveBeenCalledTimes(0); + }); + + it('can be switched off by name and handler for multiple installs in once', () => { + const emitter = new ex.EventEmitter(); + const handler = jasmine.createSpy('handler'); + + emitter.once('myevent2', handler); + emitter.once('myevent2', handler); + + emitter.off('myevent2', handler); + + emitter.emit('myevent2'); + + expect(handler).toHaveBeenCalledTimes(0); + }); + it('can be switched off by name and handler for "once"', () => { const emitter = new ex.EventEmitter(); const handler = jasmine.createSpy('handler');