2
0
mirror of https://github.com/frappe/books.git synced 2025-01-25 16:18:33 +00:00
books/utils/observable.js

116 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-01-31 15:43:33 +05:30
module.exports = class Observable {
constructor() {
this._observable = {
isHot: {},
eventQueue: {},
listeners: {},
onceListeners: {}
}
}
2018-02-13 17:24:57 +05:30
on(event, listener) {
this._addListener('listeners', event, listener);
if (this._observable.socketClient) {
this._observable.socketClient.on(event, listener);
2018-02-19 22:11:10 +05:30
}
2018-01-31 18:26:21 +05:30
}
2018-01-31 15:43:33 +05:30
// remove listener
off(event, listener) {
for (let type of ['listeners', 'onceListeners']) {
let index = this._observable[type][event] && this._observable[type][event].indexOf(listener);
if (index) {
this._observable[type][event].splice(index, 1);
}
}
}
2018-02-13 17:24:57 +05:30
once(event, listener) {
this._addListener('onceListeners', event, listener);
2018-02-19 22:11:10 +05:30
}
async trigger(event, params, throttle=false) {
2018-02-23 21:47:55 +05:30
if (throttle) {
if (this._throttled(event, params, throttle)) return;
params = [params]
}
2018-02-23 21:47:55 +05:30
await this._executeTriggers(event, params);
}
2018-02-23 21:47:55 +05:30
async _executeTriggers(event, params) {
let response = await this._triggerEvent('listeners', event, params);
if (response === false) return false;
response = await this._triggerEvent('onceListeners', event, params);
if (response === false) return false;
2018-02-13 17:24:57 +05:30
// emit via socket
if (this._observable.socketServer) {
this._observable.socketServer.emit(event, params);
2018-02-19 22:11:10 +05:30
}
2018-02-13 17:24:57 +05:30
// clear once-listeners
if (this._observable.onceListeners && this._observable.onceListeners[event]) {
delete this._observable.onceListeners[event];
}
}
clearListeners() {
this._observable.listeners = {};
this._observable.onceListeners = {};
}
bindSocketClient(socket) {
// also send events with sockets
this._observable.socketClient = socket;
}
bindSocketServer(socket) {
// also send events with sockets
this._observable.socketServer = socket;
}
_throttled(event, params, throttle) {
if (this._observable.isHot[event]) {
2018-02-23 21:47:55 +05:30
// hot, add to queue
if (!this._observable.eventQueue[event]) this._observable.eventQueue[event] = [];
this._observable.eventQueue[event].push(params);
2018-02-23 21:47:55 +05:30
// aleady hot, quit
return true;
}
this._observable.isHot[event] = true;
2018-02-23 21:47:55 +05:30
// cool-off
setTimeout(() => {
this._observable.isHot[event] = false;
2018-02-23 21:47:55 +05:30
// flush queue
if (this._observable.eventQueue[event]) {
let _queuedParams = this._observable.eventQueue[event];
this._observable.eventQueue[event] = null;
2018-02-23 21:47:55 +05:30
this._executeTriggers(event, _queuedParams);
}
2018-02-23 21:47:55 +05:30
}, throttle);
return false;
}
_addListener(type, event, listener) {
if (!this._observable[type][event]) {
this._observable[type][event] = [];
}
this._observable[type][event].push(listener);
}
async _triggerEvent(type, event, params) {
if (this._observable[type][event]) {
for (let listener of this._observable[type][event]) {
2018-02-13 17:24:57 +05:30
await listener(params);
2018-01-31 15:43:33 +05:30
}
}
}
}