diff --git a/www/admin/inner.js b/www/admin/inner.js index 9fab8020a..98d44d684 100644 --- a/www/admin/inner.js +++ b/www/admin/inner.js @@ -257,15 +257,18 @@ define([ if (!$ticket.length) { $ticket = APP.support.makeTicket($div, content, function () { var error = false; - hashesById[id].forEach(function (d) { - common.mailbox.dismiss(d, function (err) { - if (err) { - error = true; - console.error(err); - } + nThen(function (w) { + hashesById[id].forEach(function (d) { + common.mailbox.dismiss(d, w(function (err) { + if (err) { + error = true; + console.error(err); + } + })); }); + }).nThen(function () { + if (!error) { $ticket.remove(); } }); - if (!error) { $ticket.remove(); } }); } $ticket.append(APP.support.makeMessage(content, hash)); diff --git a/www/common/outer/mailbox.js b/www/common/outer/mailbox.js index 8e29333be..706f1c8c2 100644 --- a/www/common/outer/mailbox.js +++ b/www/common/outer/mailbox.js @@ -134,13 +134,8 @@ proxy.mailboxes = { // If the hash in in our history, get the index from the history: // - if the index is 0, we can change our lastKnownHash // - otherwise, just push to view - var idx; - if (box.history.some(function (el, i) { - if (hash === el) { - idx = i; - return true; - } - })) { + var idx = box.history.indexOf(hash); + if (idx !== -1) { if (idx === 0) { m.lastKnownHash = hash; box.history.shift(); @@ -153,15 +148,25 @@ proxy.mailboxes = { // Check the "viewed" array to see if we're able to bump lastKnownhash more var sliceIdx; var lastKnownHash; + var toForget = []; box.history.some(function (hash, i) { + // naming here is confusing... isViewed implies it's a boolean + // when in fact it's an index var isViewed = m.viewed.indexOf(hash); - if (isViewed !== -1) { - sliceIdx = i + 1; - m.viewed.splice(isViewed, 1); - lastKnownHash = hash; - return false; - } - return true; + + // iterate over your history until you hit an element you haven't viewed + if (isViewed === -1) { return true; } + // update the index that you'll use to slice off viewed parts of history + sliceIdx = i + 1; + // keep track of which hashes you should remove from your 'viewed' array + toForget.push(hash); + // prevent fetching dismissed messages on (re)connect + lastKnownHash = hash; + }); + + // remove all elements in 'toForget' from the 'viewed' array in one step + m.viewed = m.viewed.filter(function (hash) { + return toForget.indexOf(hash) === -1; }); if (sliceIdx) {