Skip to content

Commit

Permalink
Refactor TreeNode to use FlushQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Feb 16, 2015
1 parent 905ba7e commit 70a62b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
41 changes: 14 additions & 27 deletions src/TreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,41 @@
var EventEmitter = require('./EventEmitter');
var LeafSnapshot = require('./LeafSnapshot');
var ObjectSnapshot = require('./ObjectSnapshot');
var FlushQueue = require('./FlushQueue');
var utils = require('./utils');
var FakeRef = require('./FakeRef');

function TreeNode(key, parent){
this._parent = parent || null;
if (parent) {
this._rootQueue = parent._rootQueue;
this._flushQueue = parent._flushQueue.childRegistration(this, '_flush');
} else {
this._rootQueue = new FlushQueue();
this._flushQueue = this._rootQueue.childRegistration(this, '_flush');
}
this._events = new EventEmitter();
this._children = {};
this._valueChildren = {};
this._childrenToFlush = [];
this._valueSnap = null;
this._value = null;
this._pendingValue = null;
this._changed = false;
this._flushScheduled = false;
this._key = key;
this.initialized = false;
this._initializeNextFlush = false;
}

TreeNode.prototype.flushChanges = function(){
this._flushScheduled = false;
TreeNode.prototype.flushChanges = function() {
this._rootQueue.flush();
};

TreeNode.prototype._flush = function(){
var initializing = this._initializeNextFlush;
if(initializing) {
this._initializeNextFlush = false;
this.initialized = true;
}
var childrenToFlush = this._childrenToFlush;
if(childrenToFlush.length){
this._childrenToFlush = [];
childrenToFlush.forEach(function(child){
child.flushChanges();
});
}
var changed = this._changed;
if(changed || initializing){
this._changed = false;
Expand Down Expand Up @@ -90,7 +92,7 @@ TreeNode.prototype.setValue = function(value){
var initializeNextFlush = this._initializeNextFlush = !this.initialized;
var changed = this._changed = this._setValue(value);
if(changed || initializeNextFlush){
this._scheduleFlush();
this._flushQueue.schedule();
}
return changed;
};
Expand Down Expand Up @@ -119,21 +121,6 @@ TreeNode.prototype._setValue = function(value){
}
};

TreeNode.prototype._scheduleFlush = function(){
if(!this._flushScheduled){
this._flushScheduled = true;
var parent = this._parent;
if(parent){
parent._scheduleChildFlush(this);
}
}
};

TreeNode.prototype._scheduleChildFlush = function(child){
this._childrenToFlush.push(child);
this._scheduleFlush();
};

TreeNode.prototype._registerValue = function(){
if(this._parent) this._parent._registerValueChild(this);
};
Expand Down
2 changes: 1 addition & 1 deletion test/TreeNode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ describe('TreeNode',function(){
setAndFlush({a:'a'});
node.child('b',true).on('value',spy1);
expect(spy1).to.have.been.calledOnce.and.calledWith(snapVal(null));
expect(node._flushScheduled).to.equal(false);
expect(node._flushQueue._scheduled).to.equal(false);
});

it('are called when the child gets a value on initial flush', function(){
Expand Down

0 comments on commit 70a62b9

Please sign in to comment.