UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

131 lines (130 loc) 3.25 kB
"use strict"; export class NodeLifeCycleController { constructor(node) { this.node = node; this._creationCompleted = false; } dispose() { this._onChildAddCallbacks = void 0; this._onChildRemoveCallbacks = void 0; this._onAfterCreatedCallbacks = void 0; this._onAfterAddedCallbacks = void 0; this._onBeforeDeletedCallbacks = void 0; this._onAfterDeletedCallbacks = void 0; } setCreationCompleted() { if (!this._creationCompleted) { this._creationCompleted = true; } } creationCompleted() { return this.node.scene().loadingController.loaded() && this._creationCompleted; } // // // ON CREATION COMPLETED // // // add_on_creation_completed_hook(callback: Callback) { // this._on_creation_completed_hooks = this._on_creation_completed_hooks || []; // this._on_creation_completed_hooks.push(callback); // } // private run_on_creation_completed_hooks() { // if (this._on_creation_completed_hooks) { // console.log('run_on_creation_completed_hooks', this.node.name); // } // this.execute_hooks(this._on_creation_completed_hooks); // } // // // ON CHILD ADD // // onChildAdd(callback) { this._onChildAddCallbacks = this._onChildAddCallbacks || []; this._onChildAddCallbacks.push(callback); } runOnChildAddCallbacks(node) { this._runCallbacksWithChildNode(this._onChildAddCallbacks, node); } // // // ON CHILD REMOVE // // onChildRemove(callback) { this._onChildRemoveCallbacks = this._onChildRemoveCallbacks || []; this._onChildRemoveCallbacks.push(callback); } runOnChildRemoveCallbacks(node) { this._runCallbacksWithChildNode(this._onChildRemoveCallbacks, node); } // // // ON CREATE // // onAfterCreated(callback) { this._onAfterCreatedCallbacks = this._onAfterCreatedCallbacks || []; this._onAfterCreatedCallbacks.push(callback); } runOnAfterCreatedCallbacks() { this._runCallbacks(this._onAfterCreatedCallbacks); } // // // ON ADD // // onAfterAdded(callback) { this._onAfterAddedCallbacks = this._onAfterAddedCallbacks || []; this._onAfterAddedCallbacks.push(callback); } runOnAfterAddedCallbacks() { this._runCallbacks(this._onAfterAddedCallbacks); } // // // ON DELETE // // onBeforeDeleted(callback) { this._onBeforeDeletedCallbacks = this._onBeforeDeletedCallbacks || []; this._onBeforeDeletedCallbacks.push(callback); } runOnBeforeDeleteCallbacks() { this._runCallbacks(this._onBeforeDeletedCallbacks); } // TODO: onAfterDeleted is not very different than methods in .dispose // so this should probably be removed/refactored onAfterDeleted(callback) { this._onAfterDeletedCallbacks = this._onAfterDeletedCallbacks || []; this._onAfterDeletedCallbacks.push(callback); } runOnDeleteCallbacks() { this._runCallbacks(this._onAfterDeletedCallbacks); } // // // UTILS // // _runCallbacks(hooks) { if (!hooks) { return; } let hook; for (hook of hooks) { hook(); } } _runCallbacksWithChildNode(hooks, childNode) { if (!hooks) { return; } let hook; for (hook of hooks) { hook(childNode); } } }