2
0
mirror of https://github.com/frappe/books.git synced 2025-01-27 09:08:24 +00:00
books/utils/observable.js

20 lines
446 B
JavaScript
Raw Normal View History

2018-01-31 15:43:33 +05:30
module.exports = class Observable {
2018-01-31 18:26:21 +05:30
constructor() {
this._handlers = {};
}
2018-01-31 15:43:33 +05:30
on(event, fn) {
if (!this._handlers[event]) {
2018-01-31 18:26:21 +05:30
this._handlers[event] = [];
}
2018-01-31 15:43:33 +05:30
this._handlers[event].push(fn);
}
async trigger(event, params) {
if (this._handlers[event]) {
for (let handler of this._handlers[event]) {
await handler(params);
}
}
}
}