Skip to content

Commit f355e71

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 cca09e3 commit f355e71

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

core/Component.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
if (!node) throw new Error(
38+
'Component must be instantiated on a Node'
39+
);
40+
41+
/** @protected */
42+
this._node = node;
43+
44+
/** @protected */
45+
this._id = this._node.addComponent(this);
46+
47+
/** @protected */
48+
this._requestingUpdate = false;
49+
}
50+
51+
/**
52+
* Requests a new update from the Node. This results into the component's
53+
* `onUpdate` method being called on the next
54+
*
55+
* @method _requestUpdate
56+
* @protected
57+
*
58+
* @return {undefined} undefined
59+
*/
60+
Component.prototype._requestUpdate = function _requestUpdate () {
61+
if (!this._requestingUpdate && this.onUpdate) {
62+
this._node.requestUpdate(this._id);
63+
this._requestingUpdate = true;
64+
}
65+
};
66+
67+
/**
68+
* A method to be invoked during the node's update phase if an update has been
69+
* requested previously.
70+
*
71+
* @method onUpdate
72+
* @private
73+
*
74+
* @return {undefined} undefined
75+
*/
76+
Component.prototype.onUpdate = function onUpdate () {
77+
this._requestingUpdate = false;
78+
};
79+
80+
module.exports = Component;

core/FamousEngine.js

Lines changed: 4 additions & 4 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

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)