diff --git a/core/Dispatch.js b/core/Dispatch.js index 0f5fc13f..b42f3812 100644 --- a/core/Dispatch.js +++ b/core/Dispatch.js @@ -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. @@ -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 @@ -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 @@ -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()); - }; /** @@ -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()); }; @@ -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); @@ -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);