blockbench/js/util/event_system.js
JannisX11 a371e208f0 Fix event system no longer working with multiple hooks
Fixes custom formats not appearing on the start screen instantly
2023-04-21 18:03:02 +02:00

70 lines
1.5 KiB
JavaScript

class EventSystem {
constructor() {
this.events = {};
}
dispatchEvent(event_name, data) {
var list = this.events[event_name];
if (!list) return;
for (var i = 0; i < list.length; i++) {
list[i](data);
}
}
on(event_name, cb) {
if (typeof cb !== 'function') {
console.warn(cb, 'is not a function!');
return;
}
if (event_name.includes(' ')) {
let event_names = event_name.split(' ');
for (let name of event_names) {
if (!this.events[name]) {
this.events[name] = [];
}
this.events[name].safePush(cb);
}
return {
delete: () => {
for (let name of event_names) {
this.events[name].remove(cb);
}
}
}
} else {
if (!this.events[event_name]) {
this.events[event_name] = [];
}
this.events[event_name].safePush(cb);
return {
delete: () => {
this.events[event_name].remove(cb);
}
}
}
}
once(event_name, cb) {
if (typeof cb !== 'function') {
console.warn(cb, 'is not a function!');
return;
}
let listener = (data) => {
this.removeListener(event_name, listener);
cb(data);
}
return this.on(event_name, listener);
}
addListener(event_name, cb) {
return this.on(event_name, cb);
}
removeListener(event_name, cb) {
if (event_name.includes(' ')) {
let event_names = event_name.split(' ');
for (let name of event_names) {
if (this.events[name]) this.events[name].remove(cb);
}
} else if (this.events[event_name]) {
this.events[event_name].remove(cb);
}
}
}