mirror of
https://github.com/JannisX11/blockbench.git
synced 2024-12-09 04:50:29 +08:00
a371e208f0
Fixes custom formats not appearing on the start screen instantly
70 lines
1.5 KiB
JavaScript
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);
|
|
}
|
|
}
|
|
} |