@osobh/reactar
Version:
AR Library for React Native and Expo
183 lines • 6.98 kB
JavaScript
"use strict";
// src/core/ARSession.ts
Object.defineProperty(exports, "__esModule", { value: true });
exports.ARSession = exports.ARSessionState = void 0;
const expo_camera_1 = require("expo-camera");
const events_1 = require("events");
const CloudAnchorManager_1 = require("./CloudAnchorManager");
const UserSessionManager_1 = require("./UserSessionManager");
var ARSessionState;
(function (ARSessionState) {
ARSessionState["UNINITIALIZED"] = "UNINITIALIZED";
ARSessionState["INITIALIZED"] = "INITIALIZED";
ARSessionState["RUNNING"] = "RUNNING";
ARSessionState["PAUSED"] = "PAUSED";
ARSessionState["STOPPED"] = "STOPPED";
})(ARSessionState || (exports.ARSessionState = ARSessionState = {}));
class ARSession extends events_1.EventEmitter {
constructor(options = {}) {
super();
this.state = ARSessionState.UNINITIALIZED;
this.cloudAnchorManager = null;
this.userSessionManager = null;
this.options = options;
}
/**
* Initializes the AR session:
* - Requests camera permissions.
* - Prepares configuration based on options.
*/
async initialize() {
var _a;
try {
const { status } = await expo_camera_1.Camera.requestCameraPermissionsAsync();
if (status !== 'granted') {
throw new Error('Camera permission not granted');
}
if (this.options.enableCloudAnchors) {
this.cloudAnchorManager = new CloudAnchorManager_1.CloudAnchorManager(this.options.cloudAnchorOptions);
this.cloudAnchorManager.on('error', (error) => this.emit('error', error));
this.cloudAnchorManager.on('anchorHosted', (anchor) => this.emit('cloudAnchorHosted', anchor));
this.cloudAnchorManager.on('anchorResolved', (anchor) => this.emit('cloudAnchorResolved', anchor));
this.cloudAnchorManager.on('anchorUpdated', (anchor) => this.emit('cloudAnchorUpdated', anchor));
this.cloudAnchorManager.on('anchorRemoved', (cloudId) => this.emit('cloudAnchorRemoved', cloudId));
}
if (this.options.enableMultiUser) {
this.userSessionManager = new UserSessionManager_1.UserSessionManager(this.options.userSessionOptions);
this.userSessionManager.on('error', (error) => this.emit('error', error));
this.userSessionManager.on('userJoined', (user) => this.emit('userJoined', user));
this.userSessionManager.on('userLeft', (user) => this.emit('userLeft', user));
this.userSessionManager.on('userUpdated', (user) => this.emit('userUpdated', user));
await this.userSessionManager.initializeSession(((_a = this.cloudAnchorManager) === null || _a === void 0 ? void 0 : _a.getDeviceId()) || 'unknown');
}
this.state = ARSessionState.INITIALIZED;
this.emit('initialized');
console.log('ARSession initialized with options:', this.options);
}
catch (error) {
this.emit('error', error);
console.error('Error initializing ARSession:', error);
throw error;
}
}
/**
* Starts the AR session if it is properly initialized.
*/
start() {
if (this.state !== ARSessionState.INITIALIZED && this.state !== ARSessionState.PAUSED) {
console.warn('ARSession must be initialized or paused to start.');
return;
}
this.state = ARSessionState.RUNNING;
// Start camera feed, rendering, and sensor subscriptions here.
this.emit('started');
console.log('ARSession started.');
}
/**
* Pauses the AR session, suspending rendering and sensor updates.
*/
pause() {
if (this.state !== ARSessionState.RUNNING) {
console.warn('ARSession is not running. Cannot pause.');
return;
}
this.state = ARSessionState.PAUSED;
// Pause camera feed, rendering loop, and sensor updates.
this.emit('paused');
console.log('ARSession paused.');
}
/**
* Resumes the AR session if it is paused.
*/
resume() {
if (this.state !== ARSessionState.PAUSED) {
console.warn('ARSession is not paused. Cannot resume.');
return;
}
this.state = ARSessionState.RUNNING;
// Resume camera feed, rendering loop, and sensor updates.
this.emit('resumed');
console.log('ARSession resumed.');
}
/**
* Hosts a local anchor to the cloud
*/
async hostCloudAnchor(anchor) {
if (!this.cloudAnchorManager) {
throw new Error('Cloud anchors are not enabled');
}
return this.cloudAnchorManager.hostAnchor(anchor);
}
/**
* Resolves a cloud anchor by its ID
*/
async resolveCloudAnchor(cloudId) {
if (!this.cloudAnchorManager) {
throw new Error('Cloud anchors are not enabled');
}
return this.cloudAnchorManager.resolveAnchor(cloudId);
}
/**
* Updates a cloud anchor's position
*/
async updateCloudAnchor(cloudId, newPosition) {
if (!this.cloudAnchorManager) {
throw new Error('Cloud anchors are not enabled');
}
return this.cloudAnchorManager.updateAnchor(cloudId, newPosition);
}
/**
* Removes a cloud anchor
*/
async removeCloudAnchor(cloudId) {
if (!this.cloudAnchorManager) {
throw new Error('Cloud anchors are not enabled');
}
return this.cloudAnchorManager.removeAnchor(cloudId);
}
/**
* Gets all hosted cloud anchors
*/
getHostedCloudAnchors() {
if (!this.cloudAnchorManager) {
return [];
}
return this.cloudAnchorManager.getHostedAnchors();
}
/**
* Gets all resolved cloud anchors
*/
getResolvedCloudAnchors() {
if (!this.cloudAnchorManager) {
return [];
}
return this.cloudAnchorManager.getResolvedAnchors();
}
/**
* Stops the AR session and cleans up resources.
*/
stop() {
if (this.state === ARSessionState.STOPPED || this.state === ARSessionState.UNINITIALIZED) {
console.warn('ARSession is not running.');
return;
}
// Clean up managers
if (this.userSessionManager) {
this.userSessionManager.endSession();
this.userSessionManager = null;
}
this.cloudAnchorManager = null;
this.state = ARSessionState.STOPPED;
// Clean up resources: stop camera, remove sensor listeners, etc.
this.emit('stopped');
console.log('ARSession stopped.');
}
/**
* Returns the current state of the AR session.
*/
getState() {
return this.state;
}
}
exports.ARSession = ARSession;
//# sourceMappingURL=ARSession.js.map