I am currently working on an event/listener manager that has the following function:
var addListener = function(event, listener) {
myListeners[event].push(listener); //assume this code works
}
However, I now need to modify it so that it appears as follows:
var addListener = function(event, listener, fireFirst) {
if(fireFirst) {
myListenersToFireFirst[event].push(listener);
} else {
myListenersToFireSecond[event].push(listener);
}
}
This adjustment is being made in order for the fireEvent
function to first execute the listeners stored in the myListenersToFireFirst
array before moving on to the second array.
The revised function will demonstrate something along these lines:
var fireEvent = function(event) {
var firstListeners = myListenersToFireFirst[event];
//for each listener in firstListeners, call `apply` on it
var secondListeners = myListenersToFireSecond[event];
//for each listener in secondListeners, call `apply` on it
}
Would you say this approach is the most efficient way to achieve ordering priority when firing listener-events in JavaScript? Is there a more elegant solution for creating this list of listener-event execution priorities?