SIGN IN SIGN UP
2024-01-06 12:14:49 +01:00
class Events {
on(event, callback) {
2024-01-06 17:02:39 +01:00
if (event.includes(" ")) {
2024-01-06 15:56:40 +01:00
for (var name of event.split(" ")) {
2024-01-06 11:51:57 +01:00
this.on(name, callback);
}
} else {
2024-01-06 15:56:40 +01:00
this._callbacks ||= {};
this._callbacks[event] ||= [];
this._callbacks[event].push(callback);
}
return this;
2024-01-06 12:14:49 +01:00
}
2013-10-24 20:25:52 +02:00
off(event, callback) {
let callbacks, index;
2024-01-06 17:02:39 +01:00
if (event.includes(" ")) {
2024-01-06 15:56:40 +01:00
for (var name of event.split(" ")) {
2024-01-06 11:51:57 +01:00
this.off(name, callback);
}
} else if (
2024-01-06 15:56:40 +01:00
(callbacks = this._callbacks?.[event]) &&
2024-01-06 11:51:57 +01:00
(index = callbacks.indexOf(callback)) >= 0
) {
callbacks.splice(index, 1);
2024-01-06 11:51:57 +01:00
if (!callbacks.length) {
delete this._callbacks[event];
}
}
return this;
2024-01-06 12:14:49 +01:00
}
2013-10-24 20:25:52 +02:00
trigger(event, ...args) {
this.eventInProgress = { name: event, args };
const callbacks = this._callbacks?.[event];
if (callbacks) {
2024-01-06 17:23:46 +01:00
for (const callback of callbacks.slice(0)) {
2024-01-06 11:51:57 +01:00
if (typeof callback === "function") {
callback(...args);
2024-01-06 11:51:57 +01:00
}
}
}
this.eventInProgress = null;
2024-01-06 11:51:57 +01:00
if (event !== "all") {
2024-01-06 15:56:40 +01:00
this.trigger("all", event, ...args);
2024-01-06 11:51:57 +01:00
}
return this;
2024-01-06 12:14:49 +01:00
}
2013-10-24 20:25:52 +02:00
removeEvent(event) {
if (this._callbacks != null) {
2024-01-06 15:56:40 +01:00
for (var name of event.split(" ")) {
2024-01-06 11:51:57 +01:00
delete this._callbacks[name];
}
}
return this;
2024-01-06 12:14:49 +01:00
}
}