|
| 1 | +"use strict"; |
| 2 | +/** |
| 3 | + * ViroEventsManager.js |
| 4 | + * JavaScript integration for ViroEventsTurboModule |
| 5 | + * |
| 6 | + * This module provides the JavaScript interface for handling |
| 7 | + * Viro JSI events through the TurboModule system |
| 8 | + */ |
| 9 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 10 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 11 | +}; |
| 12 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 13 | +const react_native_1 = require("react-native"); |
| 14 | +const ViroEventsTurboModule_1 = __importDefault(require("./specs/ViroEventsTurboModule")); |
| 15 | +class ViroEventsManager { |
| 16 | + constructor() { |
| 17 | + this.eventEmitter = new react_native_1.NativeEventEmitter(ViroEventsTurboModule_1.default); |
| 18 | + this.listeners = new Map(); |
| 19 | + this.callbackRegistry = new Map(); |
| 20 | + // Set up event listeners |
| 21 | + this.setupEventListeners(); |
| 22 | + } |
| 23 | + setupEventListeners() { |
| 24 | + // Listen for JSI callbacks |
| 25 | + this.eventEmitter.addListener("ViroJSICallback", (event) => { |
| 26 | + this.handleJSICallback(event); |
| 27 | + }); |
| 28 | + // Listen for node events |
| 29 | + this.eventEmitter.addListener("ViroNodeEvent", (event) => { |
| 30 | + this.handleNodeEvent(event); |
| 31 | + }); |
| 32 | + // Listen for scene events |
| 33 | + this.eventEmitter.addListener("ViroSceneEvent", (event) => { |
| 34 | + this.handleSceneEvent(event); |
| 35 | + }); |
| 36 | + } |
| 37 | + /** |
| 38 | + * Register a callback for JSI events |
| 39 | + * @param {string} callbackId - The callback ID from JSI |
| 40 | + * @param {function} callback - The JavaScript callback function |
| 41 | + */ |
| 42 | + registerJSICallback(callbackId, callback) { |
| 43 | + if (typeof callback === "function") { |
| 44 | + this.callbackRegistry.set(callbackId, callback); |
| 45 | + console.log(`[ViroEventsManager] Registered JSI callback: ${callbackId}`); |
| 46 | + } |
| 47 | + else { |
| 48 | + console.warn(`[ViroEventsManager] Invalid callback for ID: ${callbackId}`); |
| 49 | + } |
| 50 | + } |
| 51 | + /** |
| 52 | + * Unregister a JSI callback |
| 53 | + * @param {string} callbackId - The callback ID to remove |
| 54 | + */ |
| 55 | + unregisterJSICallback(callbackId) { |
| 56 | + if (this.callbackRegistry.has(callbackId)) { |
| 57 | + this.callbackRegistry.delete(callbackId); |
| 58 | + console.log(`[ViroEventsManager] Unregistered JSI callback: ${callbackId}`); |
| 59 | + } |
| 60 | + } |
| 61 | + /** |
| 62 | + * Register a listener for node events |
| 63 | + * @param {string} nodeId - The node ID |
| 64 | + * @param {string} eventName - The event name |
| 65 | + * @param {function} callback - The callback function |
| 66 | + */ |
| 67 | + registerNodeEventListener(nodeId, eventName, callback) { |
| 68 | + const key = `${nodeId}.${eventName}`; |
| 69 | + this.listeners.set(key, callback); |
| 70 | + console.log(`[ViroEventsManager] Registered node event listener: ${key}`); |
| 71 | + } |
| 72 | + /** |
| 73 | + * Register a listener for scene events |
| 74 | + * @param {string} sceneId - The scene ID |
| 75 | + * @param {string} eventName - The event name |
| 76 | + * @param {function} callback - The callback function |
| 77 | + */ |
| 78 | + registerSceneEventListener(sceneId, eventName, callback) { |
| 79 | + const key = `scene.${sceneId}.${eventName}`; |
| 80 | + this.listeners.set(key, callback); |
| 81 | + console.log(`[ViroEventsManager] Registered scene event listener: ${key}`); |
| 82 | + } |
| 83 | + /** |
| 84 | + * Handle JSI callback events |
| 85 | + * @private |
| 86 | + */ |
| 87 | + handleJSICallback(event) { |
| 88 | + const { callbackId, eventData, timestamp } = event; |
| 89 | + console.log(`[ViroEventsManager] Received JSI callback: ${callbackId}`, eventData); |
| 90 | + const callback = this.callbackRegistry.get(callbackId); |
| 91 | + if (callback) { |
| 92 | + try { |
| 93 | + callback(eventData); |
| 94 | + } |
| 95 | + catch (error) { |
| 96 | + console.error(`[ViroEventsManager] Error executing JSI callback ${callbackId}:`, error); |
| 97 | + } |
| 98 | + } |
| 99 | + else { |
| 100 | + console.warn(`[ViroEventsManager] No callback registered for ID: ${callbackId}`); |
| 101 | + } |
| 102 | + } |
| 103 | + /** |
| 104 | + * Handle node events |
| 105 | + * @private |
| 106 | + */ |
| 107 | + handleNodeEvent(event) { |
| 108 | + const { nodeId, eventName, eventData, timestamp } = event; |
| 109 | + const key = `${nodeId}.${eventName}`; |
| 110 | + console.log(`[ViroEventsManager] Received node event: ${key}`, eventData); |
| 111 | + const callback = this.listeners.get(key); |
| 112 | + if (callback) { |
| 113 | + try { |
| 114 | + callback(eventData); |
| 115 | + } |
| 116 | + catch (error) { |
| 117 | + console.error(`[ViroEventsManager] Error executing node event callback ${key}:`, error); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + /** |
| 122 | + * Handle scene events |
| 123 | + * @private |
| 124 | + */ |
| 125 | + handleSceneEvent(event) { |
| 126 | + const { sceneId, eventName, eventData, timestamp } = event; |
| 127 | + const key = `scene.${sceneId}.${eventName}`; |
| 128 | + console.log(`[ViroEventsManager] Received scene event: ${key}`, eventData); |
| 129 | + const callback = this.listeners.get(key); |
| 130 | + if (callback) { |
| 131 | + try { |
| 132 | + callback(eventData); |
| 133 | + } |
| 134 | + catch (error) { |
| 135 | + console.error(`[ViroEventsManager] Error executing scene event callback ${key}:`, error); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + /** |
| 140 | + * Check if the event system is ready |
| 141 | + * @returns {boolean} |
| 142 | + */ |
| 143 | + isEventSystemReady() { |
| 144 | + try { |
| 145 | + return ViroEventsTurboModule_1.default.isEventSystemReady(); |
| 146 | + } |
| 147 | + catch (error) { |
| 148 | + console.error("[ViroEventsManager] Error checking event system status:", error); |
| 149 | + return false; |
| 150 | + } |
| 151 | + } |
| 152 | + /** |
| 153 | + * Get the number of active listeners |
| 154 | + * @returns {number} |
| 155 | + */ |
| 156 | + getActiveListenerCount() { |
| 157 | + try { |
| 158 | + return ViroEventsTurboModule_1.default.getActiveListenerCount(); |
| 159 | + } |
| 160 | + catch (error) { |
| 161 | + console.error("[ViroEventsManager] Error getting listener count:", error); |
| 162 | + return 0; |
| 163 | + } |
| 164 | + } |
| 165 | + /** |
| 166 | + * Manually emit a JSI callback (for testing) |
| 167 | + * @param {string} callbackId - The callback ID |
| 168 | + * @param {object} eventData - The event data |
| 169 | + */ |
| 170 | + emitJSICallback(callbackId, eventData = {}) { |
| 171 | + try { |
| 172 | + ViroEventsTurboModule_1.default.emitJSICallback(callbackId, eventData); |
| 173 | + } |
| 174 | + catch (error) { |
| 175 | + console.error("[ViroEventsManager] Error emitting JSI callback:", error); |
| 176 | + } |
| 177 | + } |
| 178 | + /** |
| 179 | + * Manually emit a node event (for testing) |
| 180 | + * @param {string} nodeId - The node ID |
| 181 | + * @param {string} eventName - The event name |
| 182 | + * @param {object} eventData - The event data |
| 183 | + */ |
| 184 | + emitNodeEvent(nodeId, eventName, eventData = {}) { |
| 185 | + try { |
| 186 | + ViroEventsTurboModule_1.default.emitNodeEvent(nodeId, eventName, eventData); |
| 187 | + } |
| 188 | + catch (error) { |
| 189 | + console.error("[ViroEventsManager] Error emitting node event:", error); |
| 190 | + } |
| 191 | + } |
| 192 | + /** |
| 193 | + * Manually emit a scene event (for testing) |
| 194 | + * @param {string} sceneId - The scene ID |
| 195 | + * @param {string} eventName - The event name |
| 196 | + * @param {object} eventData - The event data |
| 197 | + */ |
| 198 | + emitSceneEvent(sceneId, eventName, eventData = {}) { |
| 199 | + try { |
| 200 | + ViroEventsTurboModule_1.default.emitSceneEvent(sceneId, eventName, eventData); |
| 201 | + } |
| 202 | + catch (error) { |
| 203 | + console.error("[ViroEventsManager] Error emitting scene event:", error); |
| 204 | + } |
| 205 | + } |
| 206 | + /** |
| 207 | + * Clean up all listeners and callbacks |
| 208 | + */ |
| 209 | + cleanup() { |
| 210 | + console.log("[ViroEventsManager] Cleaning up event manager"); |
| 211 | + // Remove all event listeners |
| 212 | + this.eventEmitter.removeAllListeners("ViroJSICallback"); |
| 213 | + this.eventEmitter.removeAllListeners("ViroNodeEvent"); |
| 214 | + this.eventEmitter.removeAllListeners("ViroSceneEvent"); |
| 215 | + // Clear registries |
| 216 | + this.listeners.clear(); |
| 217 | + this.callbackRegistry.clear(); |
| 218 | + console.log("[ViroEventsManager] Event manager cleanup completed"); |
| 219 | + } |
| 220 | +} |
| 221 | +// Export singleton instance |
| 222 | +exports.default = new ViroEventsManager(); |
0 commit comments