Skip to content

Commit

Permalink
fix: Remove multiple handlers if they are registered (#2835)
Browse files Browse the repository at this point in the history
Fixed issue where removing handlers by function reference only removed the first registered one
  • Loading branch information
eonarheim authored Dec 1, 2023
1 parent 2febdf1 commit 538c9bd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions src/engine/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,11 @@ export class EventEmitter<TEventMap extends EventMap = any> {
off(eventName: string): void;
off<TEventName extends EventKey<TEventMap> | string>(eventName: TEventName, handler?: Handler<TEventMap[TEventName]>): 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];
}
Expand Down
28 changes: 28 additions & 0 deletions src/spec/EventEmitterSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 538c9bd

Please sign in to comment.