Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix onSizeChange #493

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
038ee0b
refact: Refactor geometries
alexanderGugel Jul 27, 2015
e2904b4
refact: Use switch; No longer hardcode gl constants
alexanderGugel Jul 27, 2015
dcd7773
[chore] update deps
Jul 27, 2015
34b3848
doc: Fix Node#requestUpdate doc
alexanderGugel Jul 23, 2015
891a57e
fix: Support sparse arrays in radixSort
alexanderGugel Jul 23, 2015
782be3d
refact: Remove obsolete data clone
alexanderGugel Jul 17, 2015
19e22ed
fix: Return gl context in getWebGLContext if available
alexanderGugel Jul 15, 2015
6b64c68
breaks: Emit Dispatch#dispatch events on source node
alexanderGugel Jul 7, 2015
961014e
fix: Fix Dispatch#dispatch bug
alexanderGugel Jul 7, 2015
c8251f1
break: Make opacity propagate
alexanderGugel Jun 17, 2015
4071b7b
feat: Add Opacity and OpacitySystem to Node
alexanderGugel Jun 18, 2015
3174885
feat: Use local opacity in DOMElement
alexanderGugel Jun 18, 2015
ec5fc8e
test: Add test case for Opacity#calculate
alexanderGugel Jun 18, 2015
ecd6480
fix: Correctly register and retrieve Opacity
alexanderGugel Jun 18, 2015
61d3f57
fix: Correctly register opacities in OpacitySystem
alexanderGugel Jun 18, 2015
3366ba2
fix: Request global opacity in Mesh
alexanderGugel Jun 18, 2015
50244d3
test: Group tests instead of t.comment
alexanderGugel Jun 18, 2015
705ba67
fix: Fix Node#setOpacity, Node#getOpacity
alexanderGugel Jun 29, 2015
e53f69f
doc: Fix OpacitySystem docs
alexanderGugel Jun 30, 2015
39c0e72
fix: Implement Opacity#setCalculateWorldOpacity
alexanderGugel Jul 14, 2015
79401a3
fix: Fix bug resulting from single queue in Dispatch (#365)
alexanderGugel Jul 7, 2015
a231ee6
doc: Fix JSDoc
alexanderGugel Jul 7, 2015
e263343
perf: Reuse queue
alexanderGugel Jul 7, 2015
4e67678
test: Test basic Dispatch.dispatch
alexanderGugel Jul 7, 2015
4324e17
chore: Fix typo
alexanderGugel Jul 7, 2015
2c55477
test: Add test case for conflicting events in Dispatch queue
alexanderGugel Jul 8, 2015
6b56cb5
test: Isolate Dispatch using rewire
alexanderGugel Jul 23, 2015
6224618
test: Fix source node dispatch test case
alexanderGugel Jul 31, 2015
90c1220
fix: Fix onSizeChange
alexanderGugel Sep 9, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 94 additions & 53 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 All @@ -64,49 +64,6 @@ Dispatch.prototype._setUpdater = function _setUpdater (updater) {
for (var key in this._nodes) this._nodes[key]._setUpdater(updater);
};

/**
* Enque the children of a node within the dispatcher. Does not clear
* the dispatchers queue first.
*
* @method addChildrenToQueue
* @return {void}
*
* @param {Node} node from which to add children to the queue
*/
Dispatch.prototype.addChildrenToQueue = function addChildrenToQueue (node) {
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);
}
};

/**
* 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
* the end of the queue. Continually calling this method will result
* in a breadth first traversal of the render tree.
*
* @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);
return child;
};

/**
* Calls the onMount method for the node at a given path and
* properly registers all of that nodes children to their proper
Expand Down Expand Up @@ -254,13 +211,15 @@ Dispatch.prototype.show = function show (path) {
if (components[i] && components[i].onShow)
components[i].onShow();

var queue = allocQueue();

this.addChildrenToQueue(node);
addChildrenToQueue(node, queue);
var child;

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

deallocQueue(queue);
};

/**
Expand Down Expand Up @@ -288,13 +247,15 @@ Dispatch.prototype.hide = function hide (path) {
if (components[i] && components[i].onHide)
components[i].onHide();

var queue = allocQueue();

this.addChildrenToQueue(node);
addChildrenToQueue(node, queue);
var child;

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

deallocQueue(queue);
};

/**
Expand All @@ -308,14 +269,16 @@ 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 = allocQueue();

_splitTo(location, path);

for (var i = 0, len = path.length ; i < len ; i++)
path[i] = this._nodes[path[i]];

path.length = 0;
deallocQueue(path);

return path[path.length - 1];
};

Expand All @@ -336,16 +299,31 @@ Dispatch.prototype.dispatch = function dispatch (path, event, payload) {
if (!event) throw new Error('dispatch requires an event name as it\'s second argument');

var node = this._nodes[path];

if (!node) return;

this.addChildrenToQueue(node);
payload.node = node;

var queue = allocQueue();
queue.push(node);

var child;
var components;
var i;
var len;

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

components = child.getComponents();

for (i = 0, len = components.length ; i < len ; i++)
if (components[i] && components[i].onReceive)
components[i].onReceive(event, payload);
}

deallocQueue(queue);
};

/**
Expand Down Expand Up @@ -391,6 +369,32 @@ Dispatch.prototype.dispatchUIEvent = function dispatchUIEvent (path, event, payl
}
};

var queues = [];

/**
* Helper method used for allocating a new queue or reusing a previously freed
* one if possible.
*
* @private
*
* @return {Array} allocated queue.
*/
function allocQueue() {
return queues.pop() || [];
}

/**
* Helper method used for freeing a previously allocated queue.
*
* @private
*
* @param {Array} queue the queue to be relased to the pool.
* @return {undefined} undefined
*/
function deallocQueue(queue) {
queues.push(queue);
}

/**
* _splitTo is a private method which takes a path and splits it at every '/'
* pushing the result into the supplied array. This is a destructive change.
Expand Down Expand Up @@ -419,4 +423,41 @@ function _splitTo (string, target) {
return target;
}

/**
* Enque the children of a node within the dispatcher. Does not clear
* the dispatchers queue first.
*
* @method addChildrenToQueue
*
* @param {Node} node from which to add children to the queue
* @param {Array} queue the queue used for retrieving the new child from
*
* @return {void}
*/
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) queue.push(child);
}
}

/**
* Returns the next node in the queue, but also adds its children to
* the end of the queue. Continually calling this method will result
* in a breadth first traversal of the render tree.
*
* @method breadthFirstNext
* @param {Array} queue the queue used for retrieving the new child from
* @return {Node | undefined} the next node in the traversal if one exists
*/
function breadthFirstNext (queue) {
var child = queue.shift();
if (!child) return void 0;
addChildrenToQueue(child, queue);
return child;
}


module.exports = new Dispatch();
2 changes: 2 additions & 0 deletions core/FamousEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var UIManager = require('../renderers/UIManager');
var Compositor = require('../renderers/Compositor');
var RequestAnimationFrameLoop = require('../render-loops/RequestAnimationFrameLoop');
var TransformSystem = require('./TransformSystem');
var OpacitySystem = require('./OpacitySystem');
var SizeSystem = require('./SizeSystem');
var Commands = require('./Commands');

Expand Down Expand Up @@ -158,6 +159,7 @@ FamousEngine.prototype._update = function _update () {

SizeSystem.update();
TransformSystem.update();
OpacitySystem.update();

while (nextQueue.length) queue.unshift(nextQueue.pop());

Expand Down
Loading