mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-21 09:38:01 +02:00
34 lines
664 B
JavaScript
34 lines
664 B
JavaScript
class EventDispatcher {
|
|
constructor() {
|
|
this.listeners_ = [];
|
|
}
|
|
|
|
dispatch(eventName, event = null) {
|
|
if (!this.listeners_[eventName]) return;
|
|
|
|
const ls = this.listeners_[eventName];
|
|
for (let i = 0; i < ls.length; i++) {
|
|
ls[i](event);
|
|
}
|
|
}
|
|
|
|
on(eventName, callback) {
|
|
if (!this.listeners_[eventName]) this.listeners_[eventName] = [];
|
|
this.listeners_[eventName].push(callback);
|
|
}
|
|
|
|
off(eventName, callback) {
|
|
if (!this.listeners_[eventName]) return;
|
|
|
|
const ls = this.listeners_[eventName];
|
|
for (let i = 0; i < ls.length; i++) {
|
|
if (ls[i] === callback) {
|
|
ls.splice(i, 1);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = EventDispatcher;
|