Skip to content

Commit

Permalink
fix: refactor once listener handling to remove deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
crhallberg authored Mar 5, 2024
1 parent c11e414 commit a5d3a73
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions themes/bootstrap3/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,28 @@ var VuFind = (function VuFind() {
listeners[event] = [];
}

const listenFn = !once ? fn : (...args) => {
fn(...args);
// Automatically remove a listener we only want to run once
unlisten(event, arguments.callee);
};
listeners[event].push(fn);

listeners[event].push(listenFn);
return () => unlisten(event, listenFn);
if (once) {
listeners[event].push(() => {
unlisten(event, fn);
});
}

return () => unlisten(event, fn);
}

function emit(event, ...args) {
if (typeof listeners[event] === "undefined") {
return;
}

listeners[event].forEach((fn) => fn(...args));
// iterate over a copy of the listeners array
// this prevents listeners from being skipped
// if the listener before it is removed during execution
for (const fn of Array.from(listeners[event])) {
fn(...args);
}
}

// Module control
Expand Down

0 comments on commit a5d3a73

Please sign in to comment.