Permalink
Browse files

pull methods from view to builder

  • Loading branch information...
1 parent 7594af8 commit 9bdc981accc69a5d1206c4468c502018c8217e17 @petrjanda committed May 10, 2012
Showing with 86 additions and 85 deletions.
  1. +0 −78 lib/view.js
  2. +80 −1 lib/viewBuilder.js
  3. +6 −6 spec/denormalizerSpec.js
View
@@ -37,68 +37,10 @@ module.exports = View = function(uid, eventNames) {
// Data content of the view, used as DTO for client.
this.data = {};
- this.snapshots = true;
-
// Timestamp of last applied event.
this.lastEvent = 0;
}
-/*
- * Load the view data from the storage. The most recent snapshot is searched
- * along with events, which are newer, thus should be applied to get most up
- * to date data.
- *
- * @param {Boolean} Let it true if snapshots should be skipped.
- * @param {Function} Callback function to be triggered when ready.
- */
-View.prototype.build = function(reload, callback) {
- var self = this;
-
- if(typeof reload == 'function') {
- callback = reload,
- reload = false
- }
-
- if(!this.snapshots || reload) {
- this._loadEvents(callback);
- return;
- }
-
- this.load(function(data) {
- 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();
@@ -113,23 +55,3 @@ View.prototype.load = function(callback) {
})
}
-/*
- * Apply given event to the view.
- *
- * @param {Object} Event object.
- * @param {Function} Callback function.
- * @return {Void}
- */
-// DRY - refactor apply methods from aggregate and view to common place.
-View.prototype.apply = function(event, callback) {
- var name = event.name.charAt(0).toUpperCase() + event.name.slice(1),
- handler = this['on' + name];
-
- if(typeof handler != 'function') {
- throw new Error('There is no handler for \'' + name + '\' event!');
- }
-
- this.lastEvent = event.time;
-
- handler.call(this, event, callback);
-}
View
@@ -5,7 +5,86 @@ var util = require('util'),
module.exports = ViewBuilder = function(uid, eventNames) {
+ this.snapshots = true;
+
View.call(this, uid, eventNames);
}
-util.inherits(ViewBuilder, View);
+util.inherits(ViewBuilder, View);
+
+/*
+ * Load the view data from the storage. The most recent snapshot is searched
+ * along with events, which are newer, thus should be applied to get most up
+ * to date data.
+ *
+ * @param {Boolean} Let it true if snapshots should be skipped.
+ * @param {Function} Callback function to be triggered when ready.
+ */
+ViewBuilder.prototype.build = function(reload, callback) {
+ var self = this;
+
+ if(typeof reload == 'function') {
+ callback = reload,
+ reload = false
+ }
+
+ if(!this.snapshots || reload) {
+ this._loadEvents(callback);
+ return;
+ }
+
+ this.load(function(data) {
+ self._loadEvents(callback);
+ })
+}
+
+ViewBuilder.prototype._updateSnapshot = function(count) {
+ var storage = Storage.createStorage();
+
+ if(count > 0 && this.snapshots) {
+ storage.purgeView(this.uid);
+ storage.storeView(this);
+ }
+}
+
+/*
+ * Apply given event to the view.
+ *
+ * @param {Object} Event object.
+ * @param {Function} Callback function.
+ * @return {Void}
+ */
+// DRY - refactor apply methods from aggregate and view to common place.
+ViewBuilder.prototype.apply = function(event, callback) {
+ var name = event.name.charAt(0).toUpperCase() + event.name.slice(1),
+ handler = this['on' + name];
+
+ if(typeof handler != 'function') {
+ throw new Error('There is no handler for \'' + name + '\' event!');
+ }
+
+ this.lastEvent = event.time;
+
+ handler.call(this, event, callback);
+}
+
+ViewBuilder.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();
+ });
+};
@@ -10,15 +10,15 @@ describe('Denormalizer', function() {
describe('#registerView', function() {
it('should store view in the list of views', function() {
- var view = new View('foo', ['bar', 'baz']);
+ var view = new ViewBuilder('foo', ['bar', 'baz']);
denormalizer.registerView(view);
expect(denormalizer.views['foo']).toEqual(view);
})
it('should store view to the events map', function() {
- var view = new View('foo', ['bar', 'baz']);
+ var view = new ViewBuilder('foo', ['bar', 'baz']);
denormalizer.registerView(view);
@@ -27,7 +27,7 @@ describe('Denormalizer', function() {
})
it('should throw an exception if the view already exists', function() {
- var view = new View('foo', ['bar', 'baz']);
+ var view = new ViewBuilder('foo', ['bar', 'baz']);
denormalizer.registerView(view);
@@ -39,7 +39,7 @@ describe('Denormalizer', function() {
describe('#updateViews', function() {
it('should trigger .build function for all registered views', function() {
- var view = new View('foo', []);
+ var view = new ViewBuilder('foo', []);
spyOn(view, 'build');
denormalizer.registerView(view);
@@ -52,7 +52,7 @@ describe('Denormalizer', function() {
describe('#updateAllViews', function() {
it('should trigger .build function for all registered views', function() {
- var view = new View('foo', ['bar', 'baz']);
+ var view = new ViewBuilder('foo', ['bar', 'baz']);
spyOn(view, 'build');
denormalizer.registerView(view);
@@ -65,7 +65,7 @@ describe('Denormalizer', function() {
describe('#build', function() {
it('should trigger .load function for the view if exists', function() {
- var view = new View('foo', ['bar', 'baz']);
+ var view = new ViewBuilder('foo', ['bar', 'baz']);
spyOn(view, 'build');
denormalizer.registerView(view);

0 comments on commit 9bdc981

Please sign in to comment.