|
| 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; |
0 commit comments