Skip to content

Commit

Permalink
fix: Fix bug resulting from single queue in Dispatch (Famous#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderGugel committed Jul 7, 2015
1 parent f2e7318 commit b5604a7
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions core/Dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var PathUtils = require('./Path');
function Dispatch () {
this._nodes = {}; // a container for constant time lookup of nodes

this._queue = []; // The queue is used for two purposes
// The queue is used for two purposes
// 1. It is used to list indicies in the
// Nodes path which are then used to lookup
// a node in the scene graph.
Expand Down Expand Up @@ -73,24 +73,14 @@ Dispatch.prototype._setUpdater = function _setUpdater (updater) {
*
* @param {Node} node from which to add children to the queue
*/
Dispatch.prototype.addChildrenToQueue = function addChildrenToQueue (node) {
function addChildrenToQueue (node, queue) {
var children = node.getChildren();
var child;
for (var i = 0, len = children.length ; i < len ; i++) {
child = children[i];
if (child) this._queue.push(child);
if (child) queue.push(child);
}
};

/**
* Returns the next item in the Dispatch's queue.
*
* @method next
* @return {Node} next node in the queue
*/
Dispatch.prototype.next = function next () {
return this._queue.shift();
};
}

/**
* Returns the next node in the queue, but also adds its children to
Expand All @@ -100,12 +90,12 @@ Dispatch.prototype.next = function next () {
* @method breadthFirstNext
* @return {Node | undefined} the next node in the traversal if one exists
*/
Dispatch.prototype.breadthFirstNext = function breadthFirstNext () {
var child = this._queue.shift();
if (!child) return void 0;
this.addChildrenToQueue(child);
function breadthFirstNext (queue) {
var child = queue.shift();
if (!child) return void 0;
addChildrenToQueue(child, queue);
return child;
};
}

/**
* Calls the onMount method for the node at a given path and
Expand Down Expand Up @@ -249,12 +239,13 @@ Dispatch.prototype.show = function show (path) {

if (node.onShow) node.onShow();

this.addChildrenToQueue(node);
var queue = [];

addChildrenToQueue(node, queue);
var child;

while ((child = this.breadthFirstNext()))
while ((child = breadthFirstNext(queue)))
this.show(child.getLocation());

};

/**
Expand All @@ -277,10 +268,12 @@ Dispatch.prototype.hide = function hide (path) {

if (node.onHide) node.onHide();

this.addChildrenToQueue(node);
var queue = [];

addChildrenToQueue(node, queue);
var child;

while ((child = this.breadthFirstNext()))
while ((child = breadthFirstNext(queue)))
this.hide(child.getLocation());

};
Expand All @@ -296,8 +289,7 @@ Dispatch.prototype.hide = function hide (path) {
Dispatch.prototype.lookupNode = function lookupNode (location) {
if (!location) throw new Error('lookupNode must be called with a path');

this._queue.length = 0;
var path = this._queue;
var path = [];

_splitTo(location, path);

Expand Down Expand Up @@ -327,10 +319,12 @@ Dispatch.prototype.dispatch = function dispatch (path, event, payload) {
if (!node)
throw new Error('No node registered at path: ' + path);

this.addChildrenToQueue(node);
var queue = [];
addChildrenToQueue(node, queue);

var child;

while ((child = this.breadthFirstNext()))
while ((child = breadthFirstNext(queue)))
if (child.onReceive)
child.onReceive(event, payload);

Expand Down

0 comments on commit b5604a7

Please sign in to comment.