Debounce updates to notifications and other events

Wait for one second of silence before displaying new notifications,
updating the unread count and conversation list previews.

Fixes #470
This commit is contained in:
lilia 2016-04-11 11:24:18 -07:00
parent ca28ba69d6
commit bb2868f1ec
3 changed files with 12 additions and 8 deletions

View file

@ -13,9 +13,12 @@
var inboxCollection = new (Backbone.Collection.extend({ var inboxCollection = new (Backbone.Collection.extend({
initialize: function() { initialize: function() {
this.on('change:timestamp change:name change:number', this.sort); this.on('change:timestamp change:name change:number', this.sort);
this.on('add remove change:unreadCount', this.updateUnreadCount);
this.listenTo(conversations, 'add change:active_at', this.addActive); this.listenTo(conversations, 'add change:active_at', this.addActive);
this.on('add remove change:unreadCount',
_.debounce(this.updateUnreadCount.bind(this), 1000)
);
}, },
comparator: function(m1, m2) { comparator: function(m1, m2) {
var timestamp1 = m1.get('timestamp'); var timestamp1 = m1.get('timestamp');
@ -48,7 +51,7 @@
this.remove(model); this.remove(model);
} }
}, },
updateUnreadCount: function(model, count) { updateUnreadCount: function() {
var newUnreadCount = _.reduce( var newUnreadCount = _.reduce(
this.map(function(m) { return m.get('unreadCount'); }), this.map(function(m) { return m.get('unreadCount'); }),
function(item, memo) { function(item, memo) {

View file

@ -14,7 +14,7 @@
Whisper.Notifications = new (Backbone.Collection.extend({ Whisper.Notifications = new (Backbone.Collection.extend({
initialize: function() { initialize: function() {
this.on('add', this.onAdd); this.on('add', _.debounce(this.update.bind(this), 1000));
this.on('remove', this.onRemove); this.on('remove', this.onRemove);
}, },
onclick: function() { onclick: function() {
@ -30,6 +30,10 @@
this.clear(); this.clear();
}, },
update: function() { update: function() {
extension.notification.clear();
if (this.length === 0) {
return;
}
var setting = storage.get('notification-setting') || 'message'; var setting = storage.get('notification-setting') || 'message';
if (setting === SETTINGS.OFF) { if (setting === SETTINGS.OFF) {
return; return;
@ -116,10 +120,6 @@
var setting = this.getSetting(); var setting = this.getSetting();
return (setting === SETTINGS.MESSAGE || setting === SETTINGS.NAME); return (setting === SETTINGS.MESSAGE || setting === SETTINGS.NAME);
}, },
onAdd: function() {
extension.notification.clear();
this.update();
},
onRemove: function() { onRemove: function() {
if (this.length === 0) { if (this.length === 0) {
extension.notification.clear(); extension.notification.clear();

View file

@ -16,7 +16,8 @@
'click': 'select' 'click': 'select'
}, },
initialize: function() { initialize: function() {
this.listenTo(this.model, 'change', this.render); // auto update // auto update
this.listenTo(this.model, 'change', _.debounce(this.render.bind(this), 1000));
this.listenTo(this.model, 'destroy', this.remove); // auto update this.listenTo(this.model, 'destroy', this.remove); // auto update
this.listenTo(this.model, 'opened', this.markSelected); // auto update this.listenTo(this.model, 'opened', this.markSelected); // auto update
extension.windows.onClosed(this.stopListening.bind(this)); extension.windows.onClosed(this.stopListening.bind(this));