UNPKG

@osobh/reactar

Version:

AR Library for React Native and Expo

105 lines 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserSessionManager = void 0; // src/core/UserSessionManager.ts const events_1 = require("events"); class UserSessionManager extends events_1.EventEmitter { constructor(options = {}) { super(); this.currentUser = null; this.heartbeatInterval = null; this.options = { serverUrl: options.serverUrl || 'https://ar-session-service.example.com', heartbeatInterval: options.heartbeatInterval || 1000, inactivityTimeout: options.inactivityTimeout || 5000, }; this.users = new Map(); } /** * Initializes a user session */ async initializeSession(deviceId) { try { const userId = `user-${Math.random().toString(36).substr(2, 9)}`; this.currentUser = { userId, deviceId, position: { x: 0, y: 0, z: 0 }, rotation: { x: 0, y: 0, z: 0 }, lastUpdate: Date.now(), isActive: true, }; this.users.set(userId, this.currentUser); this.emit('userJoined', this.currentUser); this.startHeartbeat(); return this.currentUser; } catch (error) { this.emit('error', error); throw error; } } /** * Updates the current user's position and rotation */ updateUserPosition(position, rotation) { if (!this.currentUser) { throw new Error('No active user session'); } this.currentUser.position = position; this.currentUser.rotation = rotation; this.currentUser.lastUpdate = Date.now(); this.users.set(this.currentUser.userId, this.currentUser); this.emit('userUpdated', this.currentUser); } /** * Handles incoming user updates from other devices */ handleRemoteUserUpdate(user) { var _a; if (user.userId === ((_a = this.currentUser) === null || _a === void 0 ? void 0 : _a.userId)) return; const existingUser = this.users.get(user.userId); if (existingUser) { Object.assign(existingUser, user); } else { this.users.set(user.userId, user); this.emit('userJoined', user); } this.emit('userUpdated', user); } /** * Gets all active users in the session */ getActiveUsers() { const now = Date.now(); return Array.from(this.users.values()).filter(user => user.isActive && now - user.lastUpdate < this.options.inactivityTimeout); } startHeartbeat() { if (this.heartbeatInterval) return; this.heartbeatInterval = setInterval(() => { if (this.currentUser) { this.currentUser.lastUpdate = Date.now(); this.emit('heartbeat', this.currentUser); } }, this.options.heartbeatInterval); } /** * Ends the current user's session */ endSession() { if (!this.currentUser) return; this.currentUser.isActive = false; this.emit('userLeft', this.currentUser); if (this.heartbeatInterval) { clearInterval(this.heartbeatInterval); this.heartbeatInterval = null; } this.currentUser = null; } } exports.UserSessionManager = UserSessionManager; //# sourceMappingURL=UserSessionManager.js.map