UNPKG

@sendbird/calls-react-native

Version:

Sendbird Calls SDK for React Native: Empower React Native apps with seamless audio, video, and group calling. Build interactive communication easily.

306 lines (304 loc) 14.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Room = void 0; var _reactNative = require("react-native"); var _types = require("../types"); var _logger = require("../utils/logger"); var _NativeBinder = require("./NativeBinder"); var _Participant = require("./Participant"); var _SendbirdError = require("./SendbirdError"); var _Room; function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } class Room { /** @internal **/ static poolRelease() { Room.pool = {}; } /** @internal **/ static get(binder, props) { if (!Room.pool[props.roomId]) Room.pool[props.roomId] = new Room(binder, props); const room = Room.pool[props.roomId]; return room._updateInternal(props); } constructor(binder, props) { _defineProperty(this, "_binder", void 0); _defineProperty(this, "_props", void 0); _defineProperty(this, "_localParticipant", void 0); _defineProperty(this, "_participants", void 0); _defineProperty(this, "_remoteParticipants", void 0); _defineProperty(this, "_internalEvents", { pool: [], emit: (event, ...args) => { // @ts-ignore this._internalEvents.pool.forEach(listener => { var _listener$event; return (_listener$event = listener[event]) === null || _listener$event === void 0 ? void 0 : _listener$event.call(listener, ...args); }); }, add: listener => { this._internalEvents.pool.push(listener); return () => { const index = this._internalEvents.pool.indexOf(listener); if (index > -1) delete this._internalEvents.pool[index]; }; } }); /** * Add GroupCall Room listener. * supports multiple listeners. * * @since 1.0.0 */ _defineProperty(this, "addListener", listener => { _logger.Logger.info('[GroupCall]', 'addListener', this.roomId); const unsubscribes = []; const disposeInternal = this._internalEvents.add(listener); const disposeNative = this._binder.addListener(_NativeBinder.CallsEvent.GROUP_CALL, ({ type, data, additionalData }) => { if (data.roomId !== this.roomId) return; _logger.Logger.info('[GroupCall]', 'receive events ', type, data.roomId, additionalData); this._updateInternal(data); switch (type) { case _NativeBinder.GroupCallEventType.ON_DELETED: { var _listener$onDeleted; (_listener$onDeleted = listener.onDeleted) === null || _listener$onDeleted === void 0 || _listener$onDeleted.call(listener); break; } case _NativeBinder.GroupCallEventType.ON_ERROR: { var _listener$onError; const error = new _SendbirdError.SendbirdError(additionalData === null || additionalData === void 0 ? void 0 : additionalData.errorMessage, additionalData === null || additionalData === void 0 ? void 0 : additionalData.errorCode); const participant = _Participant.Participant.get(additionalData === null || additionalData === void 0 ? void 0 : additionalData.participant); (_listener$onError = listener.onError) === null || _listener$onError === void 0 || _listener$onError.call(listener, error, participant); break; } case _NativeBinder.GroupCallEventType.ON_LOCAL_PARTICIPANT_DISCONNECTED: { var _listener$onLocalPart; (_listener$onLocalPart = listener.onLocalParticipantDisconnected) === null || _listener$onLocalPart === void 0 || _listener$onLocalPart.call(listener, _Participant.Participant.get(additionalData === null || additionalData === void 0 ? void 0 : additionalData.participant)); break; } case _NativeBinder.GroupCallEventType.ON_LOCAL_PARTICIPANT_RECONNECTED: { var _listener$onLocalPart2; (_listener$onLocalPart2 = listener.onLocalParticipantReconnected) === null || _listener$onLocalPart2 === void 0 || _listener$onLocalPart2.call(listener, _Participant.Participant.get(additionalData === null || additionalData === void 0 ? void 0 : additionalData.participant)); break; } case _NativeBinder.GroupCallEventType.ON_REMOTE_PARTICIPANT_ENTERED: { var _listener$onRemotePar; (_listener$onRemotePar = listener.onRemoteParticipantEntered) === null || _listener$onRemotePar === void 0 || _listener$onRemotePar.call(listener, _Participant.Participant.get(additionalData === null || additionalData === void 0 ? void 0 : additionalData.participant)); break; } case _NativeBinder.GroupCallEventType.ON_REMOTE_PARTICIPANT_EXITED: { var _listener$onRemotePar2; (_listener$onRemotePar2 = listener.onRemoteParticipantExited) === null || _listener$onRemotePar2 === void 0 || _listener$onRemotePar2.call(listener, _Participant.Participant.get(additionalData === null || additionalData === void 0 ? void 0 : additionalData.participant)); break; } case _NativeBinder.GroupCallEventType.ON_REMOTE_PARTICIPANT_STREAM_STARTED: { var _listener$onRemotePar3; (_listener$onRemotePar3 = listener.onRemoteParticipantStreamStarted) === null || _listener$onRemotePar3 === void 0 || _listener$onRemotePar3.call(listener, _Participant.Participant.get(additionalData === null || additionalData === void 0 ? void 0 : additionalData.participant)); break; } case _NativeBinder.GroupCallEventType.ON_AUDIO_DEVICE_CHANGED: { if (_reactNative.Platform.OS === 'android') { var _listener$onAudioDevi; (_listener$onAudioDevi = listener.onAudioDeviceChanged) === null || _listener$onAudioDevi === void 0 || _listener$onAudioDevi.call(listener, { platform: 'android', data: { currentAudioDevice: (additionalData === null || additionalData === void 0 ? void 0 : additionalData.currentAudioDevice) ?? null, availableAudioDevices: (additionalData === null || additionalData === void 0 ? void 0 : additionalData.availableAudioDevices) ?? [] } }); } if (_reactNative.Platform.OS === 'ios') { var _listener$onAudioDevi2; (_listener$onAudioDevi2 = listener.onAudioDeviceChanged) === null || _listener$onAudioDevi2 === void 0 || _listener$onAudioDevi2.call(listener, { platform: 'ios', data: { reason: (additionalData === null || additionalData === void 0 ? void 0 : additionalData.reason) ?? _types.RouteChangeReason.unknown, currentRoute: (additionalData === null || additionalData === void 0 ? void 0 : additionalData.currentRoute) ?? { inputs: [], outputs: [] }, previousRoute: (additionalData === null || additionalData === void 0 ? void 0 : additionalData.previousRoute) ?? { inputs: [], outputs: [] } } }); } break; } case _NativeBinder.GroupCallEventType.ON_REMOTE_VIDEO_SETTINGS_CHANGED: { var _listener$onRemoteVid; (_listener$onRemoteVid = listener.onRemoteVideoSettingsChanged) === null || _listener$onRemoteVid === void 0 || _listener$onRemoteVid.call(listener, _Participant.Participant.get(additionalData === null || additionalData === void 0 ? void 0 : additionalData.participant)); break; } case _NativeBinder.GroupCallEventType.ON_REMOTE_AUDIO_SETTINGS_CHANGED: { var _listener$onRemoteAud; (_listener$onRemoteAud = listener.onRemoteAudioSettingsChanged) === null || _listener$onRemoteAud === void 0 || _listener$onRemoteAud.call(listener, _Participant.Participant.get(additionalData === null || additionalData === void 0 ? void 0 : additionalData.participant)); break; } case _NativeBinder.GroupCallEventType.ON_CUSTOM_ITEMS_UPDATED: { var _listener$onCustomIte; (_listener$onCustomIte = listener.onCustomItemsUpdated) === null || _listener$onCustomIte === void 0 || _listener$onCustomIte.call(listener, (additionalData === null || additionalData === void 0 ? void 0 : additionalData.updatedKeys) ?? []); break; } case _NativeBinder.GroupCallEventType.ON_CUSTOM_ITEMS_DELETED: { var _listener$onCustomIte2; (_listener$onCustomIte2 = listener.onCustomItemsDeleted) === null || _listener$onCustomIte2 === void 0 || _listener$onCustomIte2.call(listener, (additionalData === null || additionalData === void 0 ? void 0 : additionalData.deletedKeys) ?? []); break; } } }); unsubscribes.push(disposeNative, disposeInternal); return () => unsubscribes.forEach(fn => fn()); }); /** * Enter the room * Will trigger {@link RoomListener.onRemoteParticipantEntered} method of remote participants after successfully entering the room. * If a remote participant entered the room, the local user will be notified via the same method. * * @since 1.0.0 */ _defineProperty(this, "enter", (options = { audioEnabled: true, videoEnabled: true }) => { return this._binder.nativeModule.enter(this.roomId, options); }); /** * Selects audio device * Changes current audio device asynchronously. * Will trigger {@link RoomListener.onAudioDeviceChanged} method of the local participant after successfully changing the audio device. * * @platform Android * @since 1.0.0 */ _defineProperty(this, "android_selectAudioDevice", async device => { if (_reactNative.Platform.OS !== 'android') return; await this._binder.nativeModule.selectAudioDevice(_types.ControllableModuleType.GROUP_CALL, this.roomId, device); }); /** * Updates custom items for this room. * Custom items are key-value pairs that can be stored with the room. * * @param customItems - Key-value pairs to update * @returns Promise resolving to the update result with updated items and affected keys * @since 1.1.9 */ _defineProperty(this, "updateCustomItems", async customItems => { const result = await this._binder.nativeModule.groupCallUpdateCustomItems(this.roomId, customItems); if (result && result.updatedItems) { this._updateCustomItems(result.updatedItems); } return result; }); /** * Deletes custom items from this room. * * @param customItemKeys - Array of keys to delete * @returns Promise resolving to the update result with updated items and affected keys * @since 1.1.9 */ _defineProperty(this, "deleteCustomItems", async customItemKeys => { const result = await this._binder.nativeModule.groupCallDeleteCustomItems(this.roomId, customItemKeys); if (result && result.updatedItems) { this._updateCustomItems(result.updatedItems); } return result; }); /** * Deletes all custom items from this room. * * @returns Promise resolving to the update result with updated items and affected keys * @since 1.1.9 */ _defineProperty(this, "deleteAllCustomItems", async () => { const result = await this._binder.nativeModule.groupCallDeleteAllCustomItems(this.roomId); if (result && result.updatedItems) { this._updateCustomItems(result.updatedItems); } return result; }); this._binder = binder; this._props = props; this._localParticipant = null; this._participants = []; this._remoteParticipants = []; } _updateInternal(props) { this._localParticipant = _Participant.LocalParticipant.get(props.localParticipant, this._binder, this._internalEvents, this.roomId); this._participants = props.participants.map(participant => _Participant.Participant.get(participant)); this._remoteParticipants = props.remoteParticipants.map(remoteParticipant => _Participant.Participant.get(remoteParticipant)); this._props = props; return this; } _updateCustomItems(customItems) { this._props.customItems = customItems; } get roomId() { return this._props.roomId; } get state() { return this._props.state; } get type() { return this._props.type; } get customItems() { return this._props.customItems; } get participants() { return this._participants; } get localParticipant() { return this._localParticipant; } get remoteParticipants() { return this._remoteParticipants; } get android_availableAudioDevices() { return this._props.android_availableAudioDevices; } get android_currentAudioDevice() { return this._props.android_currentAudioDevice; } get createdAt() { return this._props.createdAt; } get createdBy() { return this._props.createdBy; } /** * Exit from the room * Will trigger {@link RoomListener.onRemoteParticipantExited} method of remote participants after successfully exiting the room. * If a remote participant exited the room, the local user will be notified via the same method. * * @since 1.0.0 */ exit() { this._binder.nativeModule.exit(this.roomId); } } exports.Room = Room; _Room = Room; /** @internal **/ _defineProperty(Room, "pool", {}); //# sourceMappingURL=Room.js.map