UNPKG

@needle-tools/engine

Version:

Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development with great integrations into editors like Unity or Blender - and can be deployed onto any device! It is flexible, extensible and networking and XR are built-in.

41 lines (32 loc) 1.54 kB
import { type IComponent } from "./engine_types.js"; import { getParam } from "./engine_utils.js"; type ComponentLifecycleEvent = "component-added" | "removing-component"; const debug = getParam("debugcomponentevents"); export class ComponentLifecycleEvents { private static eventListeners = new Map<string, ((data: IComponent) => void)[]>(); static addComponentLifecylceEventListener(evt: ComponentLifecycleEvent | (string & {}), cb: (data: IComponent) => void) { if (this.eventListeners.has(evt)) { this.eventListeners.set(evt, []); } let arr = this.eventListeners.get(evt); if (!arr) arr = []; arr.push(cb); this.eventListeners.set(evt, arr); if(debug) console.log("Added event listener for " + evt, this.eventListeners) } static removeComponentLifecylceEventListener(evt: ComponentLifecycleEvent | (string & {}), cb: (data: IComponent) => void) { const listeners = this.eventListeners.get(evt); if (!listeners) return; const index = listeners.indexOf(cb); if (index < 0) return; listeners.splice(index, 1); } static dispatchComponentLifecycleEvent(evt: ComponentLifecycleEvent, data: IComponent) { const listeners = this.eventListeners.get(evt); if(debug) console.log("Dispatching event " + evt, listeners) if (!listeners) return; for (const listener of listeners) { listener(data); } } }