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.
Trying to make use of a "one-off" event with the braintree dropin.
The expectation is that this will work:
Problem is, the event array gets mutated in the when
dropIn.offgets called during anemitphase, which shrinks the array, such that theusualevent 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
oneoroncefunction to handle the removal of the event after it being called is anice to have, not required though.