Skip to content

Commit

Permalink
Apply modifying entries to the queue
Browse files Browse the repository at this point in the history
Imagine the following scenario:
 * An editor posts an entry
 * The user gets a nag with 1 new entry
 * The editor deletes the just-added entry
 * The entry is deleted, but since the queue isn't
touched, the user still sees the same message.

In this commit we apply all new modifying (update/delete)
entries to the queue. If we get a delete entry for an
entry in the queue, we remove it from the queue. If we
get an update entry, we just update the html of the
entry in the queue. Both of these trigger a re-render
of the fixed nag and it's always correct.
  • Loading branch information
nb committed Jan 4, 2013
1 parent 21c5db6 commit 11646fe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions HACKING.md
Expand Up @@ -7,6 +7,7 @@ In this file you'll find technical overview of how the liveblog works.
* Liveblog post – a WordPress post, which has the liveblog checbox checked, shows the liveblog entries in real time, and offers authorized users to insert new entries.
* Refresh interval – how often the client side checks for entries' updates.
* Nag – when there's a new update, we show the nag to the users, instead of loading the new entries directly. The nag contains a link to load the new entries.
* Modifying Entry – an entry, which is not an actual entry, but updates or deletes (replaces) an existing entry.

# Major Design Decisions
* **Each entry is a comment** – adding a lot of posts quickly leads to too much cache invalidations. Comments don't have cache entry per comment, so it's much easier to create a scalable liveblog.
Expand Down
33 changes: 27 additions & 6 deletions js/liveblog.js
Expand Up @@ -28,7 +28,27 @@ window.liveblog = {};
}
liveblog.display_entries(this.models);
this.reset([]);
}
},
applyModifyingEntries: function(entries) {
console.log(entries);
var collection = this;
_.each(entries, function(entry) {
collection.applyModifyingEntry(entry);
});
},
applyModifyingEntry: function(entry) {
var existing = this.get(entry.id);
if (!existing) {
return;
}
console.log(entry);
if ("delete" == entry.type) {
this.remove(existing);
}
if ("update" == entry.type) {
existing.set("html", entry.html);
}
},
});

liveblog.FixedNagView = Backbone.View.extend({
Expand Down Expand Up @@ -166,7 +186,7 @@ window.liveblog = {};
};

liveblog.get_recent_entries_success = function( response, status, xhr ) {
var new_entries, existing_entries;
var added, modifying;

liveblog.consecutive_failures_count = 0;

Expand All @@ -183,11 +203,12 @@ window.liveblog = {};
if ( liveblog.is_at_the_top() && liveblog.queue.isEmpty() ) {
liveblog.display_entries( response.entries );
} else {
new_entries = _.filter(response.entries, function(entry) { return 'new' == entry.type; } );
existing_entries = _.filter(response.entries, function(entry) { return 'update' == entry.type || 'delete' == entry.type; } );
liveblog.queue.add(new_entries);
added = _.filter(response.entries, function(entry) { return 'new' == entry.type; } );
modifying = _.filter(response.entries, function(entry) { return 'update' == entry.type || 'delete' == entry.type; } );
liveblog.queue.add(added);
liveblog.queue.applyModifyingEntries(modifying);
// updating and deleting entries is rare enough, so that we can screw the user's scroll and not queue those events
liveblog.display_entries(existing_entries);
liveblog.display_entries(modifying);
}
}

Expand Down

0 comments on commit 11646fe

Please sign in to comment.