mirror of
https://github.com/The-Low-Code-Foundation/OpenNoodl.git
synced 2026-01-11 23:02:56 +01:00
Co-Authored-By: Eric Tuvesson <eric.tuvesson@gmail.com> Co-Authored-By: mikaeltellhed <2311083+mikaeltellhed@users.noreply.github.com> Co-Authored-By: kotte <14197736+mrtamagotchi@users.noreply.github.com> Co-Authored-By: Anders Larsson <64838990+anders-topp@users.noreply.github.com> Co-Authored-By: Johan <4934465+joolsus@users.noreply.github.com> Co-Authored-By: Tore Knudsen <18231882+torekndsn@users.noreply.github.com> Co-Authored-By: victoratndl <99176179+victoratndl@users.noreply.github.com>
69 lines
1.6 KiB
JavaScript
69 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
function EventSender() {
|
|
this.listeners = {};
|
|
this.listenersWithRefs = {};
|
|
}
|
|
|
|
EventSender.prototype.on = function (eventName, callback, ref) {
|
|
if (ref) {
|
|
if (!this.listenersWithRefs.hasOwnProperty(eventName)) {
|
|
this.listenersWithRefs[eventName] = new Map();
|
|
}
|
|
|
|
if (!this.listenersWithRefs[eventName].get(ref)) {
|
|
this.listenersWithRefs[eventName].set(ref, []);
|
|
}
|
|
|
|
this.listenersWithRefs[eventName].get(ref).push(callback);
|
|
} else {
|
|
if (!this.listeners.hasOwnProperty(eventName)) {
|
|
this.listeners[eventName] = [];
|
|
}
|
|
|
|
this.listeners[eventName].push(callback);
|
|
}
|
|
};
|
|
|
|
EventSender.prototype.removeListenersWithRef = function (ref) {
|
|
Object.keys(this.listenersWithRefs).forEach((eventName) => {
|
|
const listeners = this.listenersWithRefs[eventName];
|
|
if (listeners.has(ref)) {
|
|
listeners.delete(ref);
|
|
}
|
|
});
|
|
};
|
|
|
|
EventSender.prototype.removeAllListeners = function (eventName) {
|
|
if (eventName) {
|
|
delete this.listeners[eventName];
|
|
delete this.listenersWithRefs[eventName];
|
|
} else {
|
|
this.listeners = {};
|
|
this.listenersWithRefs = {};
|
|
}
|
|
};
|
|
|
|
EventSender.prototype.emit = async function (eventName, data) {
|
|
const array = this.listeners[eventName];
|
|
|
|
if (array) {
|
|
for (let i = 0; i < array.length; i++) {
|
|
const callback = array[i];
|
|
await Promise.resolve(callback.call(null, data));
|
|
}
|
|
}
|
|
|
|
const map = this.listenersWithRefs[eventName];
|
|
|
|
if (map) {
|
|
for (const [ref, callbacks] of map) {
|
|
for (const callback of callbacks) {
|
|
await Promise.resolve(callback.call(ref, data));
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
module.exports = EventSender;
|