Correct live refresh routine for notifications

This commit is contained in:
Sam 2016-02-18 13:20:22 +11:00
parent 52a6682690
commit f9c5cded6f

View file

@ -49,34 +49,35 @@ export default {
const oldNotifications = stale.results.get('content'); const oldNotifications = stale.results.get('content');
const staleIndex = _.findIndex(oldNotifications, {id: lastNotification.id}); const staleIndex = _.findIndex(oldNotifications, {id: lastNotification.id});
if (staleIndex > -1) { if (staleIndex === -1) {
oldNotifications.splice(staleIndex, 1); // this gets a bit tricky, uread pms are bumped to front
var insertPosition = 0;
if (lastNotification.notification_type !== 6) {
insertPosition = _.findIndex(oldNotifications, function(n){
return n.notification_type !== 6 || n.read;
});
insertPosition = insertPosition === -1 ? oldNotifications.length - 1 : insertPosition;
}
oldNotifications.insertAt(insertPosition, Em.Object.create(lastNotification));
} }
// this gets a bit tricky, uread pms are bumped to front for (var idx=0; idx < data.recent.length; idx++) {
var insertPosition = 0; var old;
if (lastNotification.notification_type !== 6) { while(old = oldNotifications[idx]) {
insertPosition = _.findIndex(oldNotifications, function(n){ var info = data.recent[idx];
return n.notification_type !== 6 || n.read;
});
insertPosition = insertPosition === -1 ? oldNotifications.length - 1 : insertPosition;
}
oldNotifications.splice(insertPosition, 0, Em.Object.create(lastNotification));
var idx=0;
data.recent.forEach((info)=> {
var old = oldNotifications[idx];
if (old) {
if (old.get('id') !== info[0]) { if (old.get('id') !== info[0]) {
oldNotifications.splice(idx, 1); oldNotifications.removeAt(idx);
return; } else {
} else if (old.get('read') !== info[1]) { if (old.get('read') !== info[1]) {
old.set('read', info[1]); old.set('read', info[1]);
}
break;
} }
} }
idx += 1; if ( !old ) { break; }
}); }
} }
}, user.notification_channel_position); }, user.notification_channel_position);