Permalink
Browse files

extract couple private helper methods

  • Loading branch information...
1 parent 21c6ae8 commit 3d621b8ba6ee518defa846466f598cea6f35eb73 @petrjanda committed Apr 30, 2012
Showing with 33 additions and 31 deletions.
  1. +33 −31 lib/view.js
View
@@ -52,51 +52,53 @@ module.exports = View = function(uid, eventNames) {
* @param {Function} Callback function to be triggered when ready.
*/
View.prototype.build = function(reload, callback) {
- var self = this,
- storage = Storage.createStorage();
+ var self = this;
if(typeof reload == 'function') {
callback = reload,
reload = false
}
- var start = new Date().getTime(),
-
- loadEvents = function() {
- repo.getEventsByName(self.eventNames, self.lastEvent + 1, function(events) {
- var count = events.length;
-
- var next = function() {
- var event = events.shift();
-
- if(event) {
- self.apply(event, next);
- } else {
- if(count > 0 && self.snapshots) {
- storage.purgeView(self.uid);
- storage.storeView(self);
- }
-
- if(callback) {
- callback.call(self);
- }
- }
- };
-
- next();
- });
- };
-
if(!this.snapshots || reload) {
- loadEvents();
+ this._loadEvents(callback);
return;
}
this.load(function(data) {
- loadEvents();
+ self._loadEvents(callback);
})
}
+View.prototype._loadEvents = function(callback) {
+ var self = this;
+
+ repo.getEventsByName(self.eventNames, self.lastEvent + 1, function(events) {
+ var count = events.length;
+
+ var next = function() {
+ var event = events.shift();
+
+ if(event) {
+ self.apply(event, next);
+ } else {
+ self._updateSnapshot(count);
+ if(callback) callback.call(self);
+ }
+ };
+
+ next();
+ });
+};
+
+View.prototype._updateSnapshot = function(count) {
+ var storage = Storage.createStorage();
+
+ if(count > 0 && this.snapshots) {
+ storage.purgeView(this.uid);
+ storage.storeView(this);
+ }
+}
+
View.prototype.load = function(callback) {
var self = this,
storage = Storage.createStorage();

0 comments on commit 3d621b8

Please sign in to comment.