Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZMS-109 #597

Merged
merged 4 commits into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 32 additions & 10 deletions lib/imap-notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
});
Expand Down