Reference Source

src/viewer/scene/Component.js

  1. import {core} from "./core.js";
  2. import {utils} from './utils.js';
  3. import {Map} from "./utils/Map.js";
  4.  
  5. /**
  6. * @desc Base class for all xeokit components.
  7. *
  8. * ## Component IDs
  9. *
  10. * Every Component has an ID that's unique within the parent {@link Scene}. xeokit generates
  11. * the IDs automatically by default, however you can also specify them yourself. In the example below, we're creating a
  12. * scene comprised of {@link Scene}, {@link Material}, {@link ReadableGeometry} and
  13. * {@link Mesh} components, while letting xeokit generate its own ID for
  14. * the {@link ReadableGeometry}:
  15. *
  16. *````JavaScript
  17. * import {Viewer, Mesh, buildTorusGeometry, ReadableGeometry, PhongMaterial, Texture, Fresnel} from "xeokit-sdk.es.js";
  18. *
  19. * const viewer = new Viewer({
  20. * canvasId: "myCanvas"
  21. * });
  22. *
  23. * viewer.scene.camera.eye = [0, 0, 5];
  24. * viewer.scene.camera.look = [0, 0, 0];
  25. * viewer.scene.camera.up = [0, 1, 0];
  26. *
  27. * new Mesh(viewer.scene, {
  28. * geometry: new ReadableGeometry(viewer.scene, buildTorusGeometry({
  29. * center: [0, 0, 0],
  30. * radius: 1.5,
  31. * tube: 0.5,
  32. * radialSegments: 32,
  33. * tubeSegments: 24,
  34. * arc: Math.PI * 2.0
  35. * }),
  36. * material: new PhongMaterial(viewer.scene, {
  37. * id: "myMaterial",
  38. * ambient: [0.9, 0.3, 0.9],
  39. * shininess: 30,
  40. * diffuseMap: new Texture(viewer.scene, {
  41. * src: "textures/diffuse/uvGrid2.jpg"
  42. * }),
  43. * specularFresnel: new Fresnel(viewer.scene, {
  44. * leftColor: [1.0, 1.0, 1.0],
  45. * rightColor: [0.0, 0.0, 0.0],
  46. * power: 4
  47. * })
  48. * })
  49. * });
  50. *````
  51. *
  52. * We can then find those components like this:
  53. *
  54. * ````javascript
  55. * // Find the Material
  56. * var material = viewer.scene.components["myMaterial"];
  57. *
  58. * // Find all PhongMaterials in the Scene
  59. * var phongMaterials = viewer.scene.types["PhongMaterial"];
  60. *
  61. * // Find our Material within the PhongMaterials
  62. * var materialAgain = phongMaterials["myMaterial"];
  63. * ````
  64. *
  65. * ## Restriction on IDs
  66. *
  67. * Auto-generated IDs are of the form ````"__0"````, ````"__1"````, ````"__2"```` ... and so on.
  68. *
  69. * Scene maintains a map of these IDs, along with a counter that it increments each time it generates a new ID.
  70. *
  71. * If Scene has created the IDs listed above, and we then destroy the ````Component```` with ID ````"__1"````,
  72. * Scene will mark that ID as available, and will reuse it for the next default ID.
  73. *
  74. * Therefore, two restrictions your on IDs:
  75. *
  76. * * don't use IDs that begin with two underscores, and
  77. * * don't reuse auto-generated IDs of destroyed Components.
  78. *
  79. * ## Logging
  80. *
  81. * Components have methods to log ID-prefixed messages to the JavaScript console:
  82. *
  83. * ````javascript
  84. * material.log("Everything is fine, situation normal.");
  85. * material.warn("Wait, whats that red light?");
  86. * material.error("Aw, snap!");
  87. * ````
  88. *
  89. * The logged messages will look like this in the console:
  90. *
  91. * ````text
  92. * [LOG] myMaterial: Everything is fine, situation normal.
  93. * [WARN] myMaterial: Wait, whats that red light..
  94. * [ERROR] myMaterial: Aw, snap!
  95. * ````
  96. *
  97. * ## Destruction
  98. *
  99. * Get notification of destruction of Components:
  100. *
  101. * ````javascript
  102. * material.once("destroyed", function() {
  103. * this.log("Component was destroyed: " + this.id);
  104. * });
  105. * ````
  106. *
  107. * Or get notification of destruction of any Component within its {@link Scene}:
  108. *
  109. * ````javascript
  110. * scene.on("componentDestroyed", function(component) {
  111. * this.log("Component was destroyed: " + component.id);
  112. * });
  113. * ````
  114. *
  115. * Then destroy a component like this:
  116. *
  117. * ````javascript
  118. * material.destroy();
  119. * ````
  120. */
  121. class Component {
  122.  
  123. /**
  124. @private
  125. */
  126. get type() {
  127. return "Component";
  128. }
  129.  
  130. /**
  131. * @private
  132. */
  133. get isComponent() {
  134. return true;
  135. }
  136.  
  137. constructor(owner = null, cfg = {}) {
  138.  
  139. /**
  140. * The parent {@link Scene} that contains this Component.
  141. *
  142. * @property scene
  143. * @type {Scene}
  144. * @final
  145. */
  146. this.scene = null;
  147.  
  148. if (this.type === "Scene") {
  149. this.scene = this;
  150. /**
  151. * The viewer that contains this Scene.
  152. * @property viewer
  153. * @type {Viewer}
  154. */
  155. this.viewer = cfg.viewer;
  156. } else {
  157. if (owner.type === "Scene") {
  158. this.scene = owner;
  159. } else if (owner instanceof Component) {
  160. this.scene = owner.scene;
  161. } else {
  162. throw "Invalid param: owner must be a Component"
  163. }
  164. this._owner = owner;
  165. }
  166.  
  167. this._dontClear = !!cfg.dontClear; // Prevent Scene#clear from destroying this component
  168.  
  169. this._renderer = this.scene._renderer;
  170.  
  171. /**
  172. Arbitrary, user-defined metadata on this component.
  173.  
  174. @property metadata
  175. @type Object
  176. */
  177. this.meta = cfg.meta || {};
  178.  
  179.  
  180. /**
  181. * ID of this Component, unique within the {@link Scene}.
  182. *
  183. * Components are mapped by this ID in {@link Scene#components}.
  184. *
  185. * @property id
  186. * @type {String|Number}
  187. */
  188. this.id = cfg.id; // Auto-generated by Scene by default
  189.  
  190. /**
  191. True as soon as this Component has been destroyed
  192.  
  193. @property destroyed
  194. @type {Boolean}
  195. */
  196. this.destroyed = false;
  197.  
  198. this._attached = {}; // Attached components with names.
  199. this._attachments = null; // Attached components keyed to IDs - lazy-instantiated
  200. this._subIdMap = null; // Subscription subId pool
  201. this._subIdEvents = null; // Subscription subIds mapped to event names
  202. this._eventSubs = null; // Event names mapped to subscribers
  203. this._eventSubsNum = null;
  204. this._events = null; // Maps names to events
  205. this._eventCallDepth = 0; // Helps us catch stack overflows from recursive events
  206. this._ownedComponents = null; // // Components created with #create - lazy-instantiated
  207.  
  208. if (this !== this.scene) { // Don't add scene to itself
  209. this.scene._addComponent(this); // Assigns this component an automatic ID if not yet assigned
  210. }
  211.  
  212. this._updateScheduled = false; // True when #_update will be called on next tick
  213.  
  214. if (owner) {
  215. owner._own(this);
  216. }
  217. }
  218.  
  219. // /**
  220. // * Unique ID for this Component within its {@link Scene}.
  221. // *
  222. // * @property
  223. // * @type {String}
  224. // */
  225. // get id() {
  226. // return this._id;
  227. // }
  228.  
  229. /**
  230. Indicates that we need to redraw the scene.
  231.  
  232. This is called by certain subclasses after they have made some sort of state update that requires the
  233. renderer to perform a redraw.
  234.  
  235. For example: a {@link Mesh} calls this on itself whenever you update its
  236. {@link Mesh#layer} property, which manually controls its render order in
  237. relation to other Meshes.
  238.  
  239. If this component has a ````castsShadow```` property that's set ````true````, then this will also indicate
  240. that the renderer needs to redraw shadow map associated with this component. Components like
  241. {@link DirLight} have that property set when they produce light that creates shadows, while
  242. components like {@link Mesh"}}layer{{/crossLink}} have that property set when they cast shadows.
  243.  
  244. @protected
  245. */
  246. glRedraw() {
  247. if (!this._renderer) { // Called from a constructor
  248. return;
  249. }
  250. this._renderer.imageDirty();
  251. if (this.castsShadow) { // Light source or object
  252. this._renderer.shadowsDirty();
  253. }
  254. }
  255.  
  256. /**
  257. Indicates that we need to re-sort the renderer's state-ordered drawables list.
  258.  
  259. For efficiency, the renderer keeps its list of drawables ordered so that runs of the same state updates can be
  260. combined. This method is called by certain subclasses after they have made some sort of state update that would
  261. require re-ordering of the drawables list.
  262.  
  263. For example: a {@link DirLight} calls this on itself whenever you update {@link DirLight#dir}.
  264.  
  265. @protected
  266. */
  267. glResort() {
  268. if (!this._renderer) { // Called from a constructor
  269. return;
  270. }
  271. this._renderer.needStateSort();
  272. }
  273.  
  274. /**
  275. * The {@link Component} that owns the lifecycle of this Component, if any.
  276. *
  277. * When that component is destroyed, this component will be automatically destroyed also.
  278. *
  279. * Will be null if this Component has no owner.
  280. *
  281. * @property owner
  282. * @type {Component}
  283. */
  284. get owner() {
  285. return this._owner;
  286. }
  287.  
  288. /**
  289. * Tests if this component is of the given type, or is a subclass of the given type.
  290. * @type {Boolean}
  291. */
  292. isType(type) {
  293. return this.type === type;
  294. }
  295.  
  296. /**
  297. * Fires an event on this component.
  298. *
  299. * Notifies existing subscribers to the event, optionally retains the event to give to
  300. * any subsequent notifications on the event as they are made.
  301. *
  302. * @param {String} event The event type name
  303. * @param {Object} value The event parameters
  304. * @param {Boolean} [forget=false] When true, does not retain for subsequent subscribers
  305. */
  306. fire(event, value, forget) {
  307. if (!this._events) {
  308. this._events = {};
  309. }
  310. if (!this._eventSubs) {
  311. this._eventSubs = {};
  312. this._eventSubsNum = {};
  313. }
  314. if (forget !== true) {
  315. this._events[event] = value || true; // Save notification
  316. }
  317. const subs = this._eventSubs[event];
  318. let sub;
  319. if (subs) { // Notify subscriptions
  320. for (const subId in subs) {
  321. if (subs.hasOwnProperty(subId)) {
  322. sub = subs[subId];
  323. this._eventCallDepth++;
  324. if (this._eventCallDepth < 300) {
  325. sub.callback.call(sub.scope, value);
  326. } else {
  327. this.error("fire: potential stack overflow from recursive event '" + event + "' - dropping this event");
  328. }
  329. this._eventCallDepth--;
  330. }
  331. }
  332. }
  333. }
  334.  
  335. /**
  336. * Subscribes to an event on this component.
  337. *
  338. * The callback is be called with this component as scope.
  339. *
  340. * @param {String} event The event
  341. * @param {Function} callback Called fired on the event
  342. * @param {Object} [scope=this] Scope for the callback
  343. * @return {String} Handle to the subscription, which may be used to unsubscribe with {@link #off}.
  344. */
  345. on(event, callback, scope) {
  346. if (!this._events) {
  347. this._events = {};
  348. }
  349. if (!this._subIdMap) {
  350. this._subIdMap = new Map(); // Subscription subId pool
  351. }
  352. if (!this._subIdEvents) {
  353. this._subIdEvents = {};
  354. }
  355. if (!this._eventSubs) {
  356. this._eventSubs = {};
  357. }
  358. if (!this._eventSubsNum) {
  359. this._eventSubsNum = {};
  360. }
  361. let subs = this._eventSubs[event];
  362. if (!subs) {
  363. subs = {};
  364. this._eventSubs[event] = subs;
  365. this._eventSubsNum[event] = 1;
  366. } else {
  367. this._eventSubsNum[event]++;
  368. }
  369. const subId = this._subIdMap.addItem(); // Create unique subId
  370. subs[subId] = {
  371. callback: callback,
  372. scope: scope || this
  373. };
  374. this._subIdEvents[subId] = event;
  375. const value = this._events[event];
  376. if (value !== undefined) { // A publication exists, notify callback immediately
  377. callback.call(scope || this, value);
  378. }
  379. return subId;
  380. }
  381.  
  382. /**
  383. * Cancels an event subscription that was previously made with {@link Component#on} or {@link Component#once}.
  384. *
  385. * @param {String} subId Subscription ID
  386. */
  387. off(subId) {
  388. if (subId === undefined || subId === null) {
  389. return;
  390. }
  391. if (!this._subIdEvents) {
  392. return;
  393. }
  394. const event = this._subIdEvents[subId];
  395. if (event) {
  396. delete this._subIdEvents[subId];
  397. const subs = this._eventSubs[event];
  398. if (subs) {
  399. delete subs[subId];
  400. this._eventSubsNum[event]--;
  401. }
  402. this._subIdMap.removeItem(subId); // Release subId
  403. }
  404. }
  405.  
  406. /**
  407. * Subscribes to the next occurrence of the given event, then un-subscribes as soon as the event is subIdd.
  408. *
  409. * This is equivalent to calling {@link Component#on}, and then calling {@link Component#off} inside the callback function.
  410. *
  411. * @param {String} event Data event to listen to
  412. * @param {Function} callback Called when fresh data is available at the event
  413. * @param {Object} [scope=this] Scope for the callback
  414. */
  415. once(event, callback, scope) {
  416. const self = this;
  417. const subId = this.on(event,
  418. function (value) {
  419. self.off(subId);
  420. callback.call(scope || this, value);
  421. },
  422. scope);
  423. }
  424.  
  425. /**
  426. * Returns true if there are any subscribers to the given event on this component.
  427. *
  428. * @param {String} event The event
  429. * @return {Boolean} True if there are any subscribers to the given event on this component.
  430. */
  431. hasSubs(event) {
  432. return (this._eventSubsNum && (this._eventSubsNum[event] > 0));
  433. }
  434.  
  435. /**
  436. * Logs a console debugging message for this component.
  437. *
  438. * The console message will have this format: *````[LOG] [<component type> <component id>: <message>````*
  439. *
  440. * Also fires the message as a "log" event on the parent {@link Scene}.
  441. *
  442. * @param {String} message The message to log
  443. */
  444. log(message) {
  445. message = "[LOG]" + this._message(message);
  446. window.console.log(message);
  447. this.scene.fire("log", message);
  448. }
  449.  
  450. _message(message) {
  451. return " [" + this.type + " " + utils.inQuotes(this.id) + "]: " + message;
  452. }
  453.  
  454. /**
  455. * Logs a warning for this component to the JavaScript console.
  456. *
  457. * The console message will have this format: *````[WARN] [<component type> =<component id>: <message>````*
  458. *
  459. * Also fires the message as a "warn" event on the parent {@link Scene}.
  460. *
  461. * @param {String} message The message to log
  462. */
  463. warn(message) {
  464. message = "[WARN]" + this._message(message);
  465. window.console.warn(message);
  466. this.scene.fire("warn", message);
  467. }
  468.  
  469. /**
  470. * Logs an error for this component to the JavaScript console.
  471. *
  472. * The console message will have this format: *````[ERROR] [<component type> =<component id>: <message>````*
  473. *
  474. * Also fires the message as an "error" event on the parent {@link Scene}.
  475. *
  476. * @param {String} message The message to log
  477. */
  478. error(message) {
  479. message = "[ERROR]" + this._message(message);
  480. window.console.error(message);
  481. this.scene.fire("error", message);
  482. }
  483.  
  484. /**
  485. * Adds a child component to this.
  486. *
  487. * When component not given, attaches the scene's default instance for the given name (if any).
  488. * Publishes the new child component on this component, keyed to the given name.
  489. *
  490. * @param {*} params
  491. * @param {String} params.name component name
  492. * @param {Component} [params.component] The component
  493. * @param {String} [params.type] Optional expected type of base type of the child; when supplied, will
  494. * cause an exception if the given child is not the same type or a subtype of this.
  495. * @param {Boolean} [params.sceneDefault=false]
  496. * @param {Boolean} [params.sceneSingleton=false]
  497. * @param {Function} [params.onAttached] Optional callback called when component attached
  498. * @param {Function} [params.onAttached.callback] Callback function
  499. * @param {Function} [params.onAttached.scope] Optional scope for callback
  500. * @param {Function} [params.onDetached] Optional callback called when component is detached
  501. * @param {Function} [params.onDetached.callback] Callback function
  502. * @param {Function} [params.onDetached.scope] Optional scope for callback
  503. * @param {{String:Function}} [params.on] Callbacks to subscribe to properties on component
  504. * @param {Boolean} [params.recompiles=true] When true, fires "dirty" events on this component
  505. * @private
  506. */
  507. _attach(params) {
  508.  
  509. const name = params.name;
  510.  
  511. if (!name) {
  512. this.error("Component 'name' expected");
  513. return;
  514. }
  515.  
  516. let component = params.component;
  517. const sceneDefault = params.sceneDefault;
  518. const sceneSingleton = params.sceneSingleton;
  519. const type = params.type;
  520. const on = params.on;
  521. const recompiles = params.recompiles !== false;
  522.  
  523. // True when child given as config object, where parent manages its instantiation and destruction
  524. let managingLifecycle = false;
  525.  
  526. if (component) {
  527.  
  528. if (utils.isNumeric(component) || utils.isString(component)) {
  529.  
  530. // Component ID given
  531. // Both numeric and string IDs are supported
  532.  
  533. const id = component;
  534.  
  535. component = this.scene.components[id];
  536.  
  537. if (!component) {
  538.  
  539. // Quote string IDs in errors
  540.  
  541. this.error("Component not found: " + utils.inQuotes(id));
  542. return;
  543. }
  544. }
  545. }
  546.  
  547. if (!component) {
  548.  
  549. if (sceneSingleton === true) {
  550.  
  551. // Using the first instance of the component type we find
  552.  
  553. const instances = this.scene.types[type];
  554. for (const id2 in instances) {
  555. if (instances.hasOwnProperty) {
  556. component = instances[id2];
  557. break;
  558. }
  559. }
  560.  
  561. if (!component) {
  562. this.error("Scene has no default component for '" + name + "'");
  563. return null;
  564. }
  565.  
  566. } else if (sceneDefault === true) {
  567.  
  568. // Using a default scene component
  569.  
  570. component = this.scene[name];
  571.  
  572. if (!component) {
  573. this.error("Scene has no default component for '" + name + "'");
  574. return null;
  575. }
  576. }
  577. }
  578.  
  579. if (component) {
  580.  
  581. if (component.scene.id !== this.scene.id) {
  582. this.error("Not in same scene: " + component.type + " " + utils.inQuotes(component.id));
  583. return;
  584. }
  585.  
  586. if (type) {
  587.  
  588. if (!component.isType(type)) {
  589. this.error("Expected a " + type + " type or subtype: " + component.type + " " + utils.inQuotes(component.id));
  590. return;
  591. }
  592. }
  593. }
  594.  
  595. if (!this._attachments) {
  596. this._attachments = {};
  597. }
  598.  
  599. const oldComponent = this._attached[name];
  600. let subs;
  601. let i;
  602. let len;
  603.  
  604. if (oldComponent) {
  605.  
  606. if (component && oldComponent.id === component.id) {
  607.  
  608. // Reject attempt to reattach same component
  609. return;
  610. }
  611.  
  612. const oldAttachment = this._attachments[oldComponent.id];
  613.  
  614. // Unsubscribe from events on old component
  615.  
  616. subs = oldAttachment.subs;
  617.  
  618. for (i = 0, len = subs.length; i < len; i++) {
  619. oldComponent.off(subs[i]);
  620. }
  621.  
  622. delete this._attached[name];
  623. delete this._attachments[oldComponent.id];
  624.  
  625. const onDetached = oldAttachment.params.onDetached;
  626. if (onDetached) {
  627. if (utils.isFunction(onDetached)) {
  628. onDetached(oldComponent);
  629. } else {
  630. onDetached.scope ? onDetached.callback.call(onDetached.scope, oldComponent) : onDetached.callback(oldComponent);
  631. }
  632. }
  633.  
  634. if (oldAttachment.managingLifecycle) {
  635.  
  636. // Note that we just unsubscribed from all events fired by the child
  637. // component, so destroying it won't fire events back at us now.
  638.  
  639. oldComponent.destroy();
  640. }
  641. }
  642.  
  643. if (component) {
  644.  
  645. // Set and publish the new component on this component
  646.  
  647. const attachment = {
  648. params: params,
  649. component: component,
  650. subs: [],
  651. managingLifecycle: managingLifecycle
  652. };
  653.  
  654. attachment.subs.push(
  655. component.once("destroyed",
  656. function () {
  657. attachment.params.component = null;
  658. this._attach(attachment.params);
  659. },
  660. this));
  661.  
  662. if (recompiles) {
  663. attachment.subs.push(
  664. component.on("dirty",
  665. function () {
  666. this.fire("dirty", this);
  667. },
  668. this));
  669. }
  670.  
  671. this._attached[name] = component;
  672. this._attachments[component.id] = attachment;
  673.  
  674. // Bind destruct listener to new component to remove it
  675. // from this component when destroyed
  676.  
  677. const onAttached = params.onAttached;
  678. if (onAttached) {
  679. if (utils.isFunction(onAttached)) {
  680. onAttached(component);
  681. } else {
  682. onAttached.scope ? onAttached.callback.call(onAttached.scope, component) : onAttached.callback(component);
  683. }
  684. }
  685.  
  686. if (on) {
  687.  
  688. let event;
  689. let subIdr;
  690. let callback;
  691. let scope;
  692.  
  693. for (event in on) {
  694. if (on.hasOwnProperty(event)) {
  695.  
  696. subIdr = on[event];
  697.  
  698. if (utils.isFunction(subIdr)) {
  699. callback = subIdr;
  700. scope = null;
  701. } else {
  702. callback = subIdr.callback;
  703. scope = subIdr.scope;
  704. }
  705.  
  706. if (!callback) {
  707. continue;
  708. }
  709.  
  710. attachment.subs.push(component.on(event, callback, scope));
  711. }
  712. }
  713. }
  714. }
  715.  
  716. if (recompiles) {
  717. this.fire("dirty", this); // FIXME: May trigger spurous mesh recompilations unless able to limit with param?
  718. }
  719.  
  720. this.fire(name, component); // Component can be null
  721.  
  722. return component;
  723. }
  724.  
  725. _checkComponent(expectedType, component) {
  726. if (!component.isComponent) {
  727. if (utils.isID(component)) {
  728. const id = component;
  729. component = this.scene.components[id];
  730. if (!component) {
  731. this.error("Component not found: " + id);
  732. return;
  733. }
  734. } else {
  735. this.error("Expected a Component or ID");
  736. return;
  737. }
  738. }
  739. if (expectedType !== component.type) {
  740. this.error("Expected a " + expectedType + " Component");
  741. return;
  742. }
  743. if (component.scene.id !== this.scene.id) {
  744. this.error("Not in same scene: " + component.type);
  745. return;
  746. }
  747. return component;
  748. }
  749.  
  750. _checkComponent2(expectedTypes, component) {
  751. if (!component.isComponent) {
  752. if (utils.isID(component)) {
  753. const id = component;
  754. component = this.scene.components[id];
  755. if (!component) {
  756. this.error("Component not found: " + id);
  757. return;
  758. }
  759. } else {
  760. this.error("Expected a Component or ID");
  761. return;
  762. }
  763. }
  764. if (component.scene.id !== this.scene.id) {
  765. this.error("Not in same scene: " + component.type);
  766. return;
  767. }
  768. for (var i = 0, len = expectedTypes.length; i < len; i++) {
  769. if (expectedTypes[i] === component.type) {
  770. return component;
  771. }
  772. }
  773. this.error("Expected component types: " + expectedTypes);
  774. return null;
  775. }
  776.  
  777. _own(component) {
  778. if (!this._ownedComponents) {
  779. this._ownedComponents = {};
  780. }
  781. if (!this._ownedComponents[component.id]) {
  782. this._ownedComponents[component.id] = component;
  783. }
  784. component.once("destroyed", () => {
  785. delete this._ownedComponents[component.id];
  786. }, this);
  787. }
  788.  
  789. /**
  790. * Protected method, called by sub-classes to queue a call to _update().
  791. * @protected
  792. * @param {Number} [priority=1]
  793. */
  794. _needUpdate(priority) {
  795. if (!this._updateScheduled) {
  796. this._updateScheduled = true;
  797. if (priority === 0) {
  798. this._doUpdate();
  799. } else {
  800. core.scheduleTask(this._doUpdate, this);
  801. }
  802. }
  803. }
  804.  
  805. /**
  806. * @private
  807. */
  808. _doUpdate() {
  809. if (this._updateScheduled) {
  810. this._updateScheduled = false;
  811. if (this._update) {
  812. this._update();
  813. }
  814. }
  815. }
  816.  
  817. /**
  818. * Schedule a task to perform on the next browser interval
  819. * @param task
  820. */
  821. scheduleTask(task) {
  822. core.scheduleTask(task, null);
  823. }
  824.  
  825. /**
  826. * Protected virtual template method, optionally implemented
  827. * by sub-classes to perform a scheduled task.
  828. *
  829. * @protected
  830. */
  831. _update() {
  832. }
  833.  
  834. /**
  835. * Destroys all {@link Component}s that are owned by this. These are Components that were instantiated with
  836. * this Component as their first constructor argument.
  837. */
  838. clear() {
  839. if (this._ownedComponents) {
  840. for (var id in this._ownedComponents) {
  841. if (this._ownedComponents.hasOwnProperty(id)) {
  842. const component = this._ownedComponents[id];
  843. component.destroy();
  844. delete this._ownedComponents[id];
  845. }
  846. }
  847. }
  848. }
  849.  
  850. /**
  851. * Destroys this component.
  852. */
  853. destroy() {
  854.  
  855. if (this.destroyed) {
  856. return;
  857. }
  858.  
  859. /**
  860. * Fired when this Component is destroyed.
  861. * @event destroyed
  862. */
  863. this.fire("destroyed", this.destroyed = true); // Must fire before we blow away subscription maps, below
  864.  
  865. // Unsubscribe from child components and destroy then
  866.  
  867. let id;
  868. let attachment;
  869. let component;
  870. let subs;
  871. let i;
  872. let len;
  873.  
  874. if (this._attachments) {
  875. for (id in this._attachments) {
  876. if (this._attachments.hasOwnProperty(id)) {
  877. attachment = this._attachments[id];
  878. component = attachment.component;
  879. subs = attachment.subs;
  880. for (i = 0, len = subs.length; i < len; i++) {
  881. component.off(subs[i]);
  882. }
  883. if (attachment.managingLifecycle) {
  884. component.destroy();
  885. }
  886. }
  887. }
  888. }
  889.  
  890. if (this._ownedComponents) {
  891. for (id in this._ownedComponents) {
  892. if (this._ownedComponents.hasOwnProperty(id)) {
  893. component = this._ownedComponents[id];
  894. component.destroy();
  895. delete this._ownedComponents[id];
  896. }
  897. }
  898. }
  899.  
  900. this.scene._removeComponent(this);
  901.  
  902. // Memory leak avoidance
  903. this._attached = {};
  904. this._attachments = null;
  905. this._subIdMap = null;
  906. this._subIdEvents = null;
  907. this._eventSubs = null;
  908. this._events = null;
  909. this._eventCallDepth = 0;
  910. this._ownedComponents = null;
  911. this._updateScheduled = false;
  912. }
  913. }
  914.  
  915. export {Component};