Skip to content

"One-off" events are difficult because the mutable version of the events array is used during the emit phase #21

@arthur5005

Description

@arthur5005

Trying to make use of a "one-off" event with the braintree dropin.

The expectation is that this will work:

dropIn.on('changeActiveView', () => { console.log('usual changeActiveView handler'); });

//... later in the code

const emitOneOff = () => {
  console.log('foo');
  dropIn.off('changeActiveView', emitOneOff);
}
dropIn.on('changeActiveView', emitOneOff);

Problem is, the event array gets mutated in the when dropIn.off gets called during an emit phase, which shrinks the array, such that the usual event handler is ignored (dropped effectively) when the event is emitted.

This problem is usually dealt with (by many event-emitter libraries) by making a copy of the array during the emit phase, and iterating over an immutable version of that array to ensure all events are called.

Once implemented, a handy one or once function to handle the removal of the event after it being called is a nice to have, not required though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions