Skip to content

Commit 18486c6

Browse files
feat: Add core/Component.js
Having a generic component base allows us to reduce redundancy in renderables and component. E.g. currently Mesh and DOMElement (and potentially all future renderables) have a _requestUpdate method. Abstracting this out into a core component encapsulates functionality commonly used by most components.
1 parent f3986d5 commit 18486c6

File tree

4 files changed

+88
-6
lines changed

4 files changed

+88
-6
lines changed

core/Component.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2015 Famous Industries Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
'use strict';
26+
27+
/**
28+
* An abstract component meant to be subclassed.
29+
*
30+
* @class Component
31+
* @constructor
32+
* @abstract
33+
*
34+
* @param {Node} node Node to which the component should be added.
35+
*/
36+
function Component(node) {
37+
this._node = node;
38+
this._id = this._node.addComponent(this);
39+
this._requestingUpdate = false;
40+
}
41+
42+
/**
43+
* Requests a new update from the Node. This results into the component's
44+
* `onUpdate` method being called on the next
45+
*
46+
* @method _requestUpdate
47+
* @protected
48+
*
49+
* @return {undefined} undefined
50+
*/
51+
Component.prototype._requestUpdate = function _requestUpdate () {
52+
if (!this._requestingUpdate && this.onUpdate) {
53+
this._node.requestUpdate(this._id);
54+
this._requestingUpdate = true;
55+
}
56+
};
57+
58+
/**
59+
* A method that will be invoked by the Node after the component has been
60+
* updated. Resets the `_requestUpdate` flag.
61+
*
62+
* @method onUpdated
63+
* @private
64+
*
65+
* @return {undefined} undefined
66+
*/
67+
Component.prototype.onUpdated = function onUpdated () {
68+
this._requestingUpdate = false;
69+
};
70+
71+
module.exports = Component;

core/FamousEngine.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/**
22
* The MIT License (MIT)
3-
*
3+
*
44
* Copyright (c) 2015 Famous Industries Inc.
5-
*
5+
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
88
* in the Software without restriction, including without limitation the rights
99
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
* copies of the Software, and to permit persons to whom the Software is
1111
* furnished to do so, subject to the following conditions:
12-
*
12+
*
1313
* The above copyright notice and this permission notice shall be included in
1414
* all copies or substantial portions of the Software.
15-
*
15+
*
1616
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -143,7 +143,13 @@ FamousEngine.prototype._update = function _update () {
143143

144144
while (queue.length) {
145145
item = queue.shift();
146-
if (item && item.onUpdate) item.onUpdate(time);
146+
if (item && item.onUpdate) {
147+
item.onUpdate(time);
148+
149+
if (item.onUpdated) {
150+
item.onUpdated(time);
151+
}
152+
}
147153
}
148154

149155
this._inUpdate = false;

core/Node.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,11 @@ Node.prototype.update = function update (time){
13961396

13971397
while (queue.length) {
13981398
item = this._components[queue.shift()];
1399-
if (item && item.onUpdate) item.onUpdate(time);
1399+
if (item && item.onUpdate) {
1400+
item.onUpdate(time);
1401+
1402+
if (item.onUpdated) item.onUpdated(time);
1403+
}
14001404
}
14011405

14021406
var mySize = this.getSize();

core/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
module.exports = {
2828
Clock: require('./Clock'),
29+
Component: require('./Component'),
2930
Event: require('./Event'),
3031
Scene: require('./Scene'),
3132
FamousEngine: require('./FamousEngine'),

0 commit comments

Comments
 (0)