diff --git a/lib/imap-notifier.js b/lib/imap-notifier.js index 1e7dd09c..6055810c 100644 --- a/lib/imap-notifier.js +++ b/lib/imap-notifier.js @@ -78,17 +78,39 @@ class ImapNotifier extends EventEmitter { this.subscriber.on('message', (channel, message) => { if (channel === 'wd_events') { let data; - try { - data = JSON.parse(message); - } catch (E) { - return; + // if e present at beginning, check if p also is present + // if no p -> no json parse + // if p -> json parse ONLY p + // if e not in beginning but p is -> json parse whole + + let needFullParse = true; + + if (message.length === 32 && message[2] === 'e' && message[5] === '"' && message[6 + 24] === '"') { + // there is only e, no p -> no need for full parse + needFullParse = false; + } + + if (!needFullParse) { + // get e and continue + data = { e: message.slice(6, 6 + 24) }; + } else { + // full parse + try { + data = JSON.parse(message); + } catch (E) { + return; + } } - if (data.e && !data.p) { - // events without payload are scheduled, these are notifications about changes in journal - scheduleDataEvent(data.e); - } else if (data.e) { - // events with payload are triggered immediatelly, these are actions for doing something - this._listeners.emit(data.e, data.p); + + if (this._listeners._events[data.e]?.length > 0) { + // do not schedule or fire/emit empty events + if (data.e && !data.p) { + // events without payload are scheduled, these are notifications about changes in journal + scheduleDataEvent(data.e); + } else if (data.e) { + // events with payload are triggered immediatelly, these are actions for doing something + this._listeners.emit(data.e, data.p); + } } } });