@jsenv/core
Version:
Tool to develop, test and build js projects
19 lines (18 loc) • 410 B
JavaScript
export const createEventEmitter = () => {
const callbackSet = new Set();
const on = (callback) => {
callbackSet.add(callback);
return () => {
callbackSet.delete(callback);
};
};
const off = (callback) => {
callbackSet.delete(callback);
};
const emit = (...args) => {
for (const callback of callbackSet) {
callback(...args);
}
};
return { on, off, emit };
};