emit flag update notifications in chunks of 100

This commit is contained in:
Andris Reinman 2017-03-12 12:11:22 +02:00
parent c5f3fee871
commit d6b028997f

35
imap.js
View file

@ -457,6 +457,8 @@ server.onStore = function (path, update, session, callback) {
['uid', 1] ['uid', 1]
]); ]);
let notifyEntries = [];
let processNext = () => { let processNext = () => {
cursor.next((err, message) => { cursor.next((err, message) => {
if (err) { if (err) {
@ -464,6 +466,14 @@ server.onStore = function (path, update, session, callback) {
} }
if (!message) { if (!message) {
return cursor.close(() => { return cursor.close(() => {
if (notifyEntries.length) {
setImmediate(() => this.notifier.addEntries(username, path, notifyEntries, () => {
this.notifier.fire(username, path);
return callback(null, true);
}));
notifyEntries = [];
return;
}
this.notifier.fire(username, path); this.notifier.fire(username, path);
return callback(null, true); return callback(null, true);
}); });
@ -531,15 +541,34 @@ server.onStore = function (path, update, session, callback) {
_id: message._id _id: message._id
}, flagsupdate, {}, err => { }, flagsupdate, {}, err => {
if (err) { if (err) {
return cursor.close(() => callback(err)); return cursor.close(() => {
if (notifyEntries.length) {
setImmediate(() => this.notifier.addEntries(username, path, notifyEntries, () => {
this.notifier.fire(username, path);
return callback(err);
}));
notifyEntries = [];
return;
}
this.notifier.fire(username, path);
callback(err);
});
} }
this.notifier.addEntries(username, path, {
notifyEntries.push({
command: 'FETCH', command: 'FETCH',
ignore: session.id, ignore: session.id,
uid: message.uid, uid: message.uid,
flags: message.flags, flags: message.flags,
message: message._id message: message._id
}, processNext); });
if (notifyEntries.length > 100) {
setImmediate(() => this.notifier.addEntries(username, path, notifyEntries, processNext));
notifyEntries = [];
return;
}
setImmediate(() => processNext());
}); });
} else { } else {
processNext(); processNext();