1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-03 08:35:29 +02:00
joplin/ReactNativeClient/lib/event-dispatcher.js
Laurent Cozic 55c5ddedf4 Revert "Applied prettier to code base"
This reverts commit c4f19465a6.
2018-03-09 20:59:12 +00:00

35 lines
665 B
JavaScript

class EventDispatcher {
constructor() {
this.listeners_ = [];
}
dispatch(eventName, event = null) {
if (!this.listeners_[eventName]) return;
let 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;
let ls = this.listeners_[eventName];
for (let i = 0; i < ls.length; i++) {
if (ls[i] === callback) {
ls.splice(i, 1);
return;
}
}
}
}
module.exports = { EventDispatcher };