Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion shepherd.js/src/evented.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class Evented {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
trigger(event: string, ...args: any[]) {
if (!isUndefined(this.bindings) && this.bindings[event]) {
this.bindings[event]?.forEach((binding, index) => {
this.bindings[event]?.slice().forEach((binding, index) => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another approach I considered was to iterate the bindings in reverse order with a for loop. This would allow us to avoid making a copy of the array, but could have an observable impact for existing users of the package. I thought it would be best to avoid what could be considered a breaking change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bausmeier what if instead of creating a copy, we made a new array like const listenersToRemove = [] and then pushed the ones to remove into that, then after the forEach is done remove them all?

const { ctx, handler, once } = binding;

const context = ctx || this;
Expand Down
8 changes: 8 additions & 0 deletions test/unit/evented.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ describe('Evented', () => {
step: { id: 'test', text: 'A step' }
});
});

it('does not skip event bindings after removing an event binding', () => {
testEvent.once('testOn', () => true);
const handlerSpy = jest.fn();
testEvent.on('testOn', handlerSpy);
testEvent.trigger('testOn');
expect(handlerSpy).toHaveBeenCalled();
});
});

describe('off()', () => {
Expand Down