UNPKG

stream-chat-react-native-core

Version:

The official React Native and Expo components for Stream Chat, a service for building chat applications

171 lines 5.66 kB
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.AudioRecorderManager = void 0; var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _reactNative = require("react-native"); var _streamChat = require("stream-chat"); var _normalizeAudioLevel = require("../components/MessageInput/utils/normalizeAudioLevel"); var _native = require("../native"); var INITIAL_STATE = { isStarting: false, micLocked: false, waveformData: [], recording: undefined, duration: 0, status: 'idle' }; var AudioRecorderManager = exports.AudioRecorderManager = function () { function AudioRecorderManager() { (0, _classCallCheck2.default)(this, AudioRecorderManager); this.pendingStopRequestIds = new Set(); this.startRequestId = 0; this.onRecordingStatusUpdate = status => { if (status.isDoneRecording === true) { return; } this.state.partialNext({ duration: status?.currentPosition || status.durationMillis }); var lowerBound = _reactNative.Platform.OS === 'ios' || status.currentMetering ? -60 : -120; var normalizedAudioLevel = (0, _normalizeAudioLevel.normalizeAudioLevel)(status.currentMetering || status.metering, lowerBound); this.state.partialNext({ waveformData: [...this.state.getLatestValue().waveformData, normalizedAudioLevel] }); }; this.state = new _streamChat.StateStore(INITIAL_STATE); } return (0, _createClass2.default)(AudioRecorderManager, [{ key: "reset", value: function reset() { this.state.next(INITIAL_STATE); } }, { key: "startRecording", value: function () { var _startRecording = (0, _asyncToGenerator2.default)(function* () { if (!_native.NativeHandlers.Audio) { return; } var _this$state$getLatest = this.state.getLatestValue(), isStarting = _this$state$getLatest.isStarting, status = _this$state$getLatest.status; if (isStarting || status === 'recording') { return true; } var requestId = ++this.startRequestId; this.state.partialNext({ duration: 0, isStarting: true, micLocked: false, recording: undefined, status: 'idle', waveformData: [] }); var recordingInfo; try { recordingInfo = yield _native.NativeHandlers.Audio.startRecording({ isMeteringEnabled: true }, this.onRecordingStatusUpdate); } catch { this.pendingStopRequestIds.delete(requestId); if (requestId === this.startRequestId) { this.reset(); } return false; } var _recordingInfo = recordingInfo, accessGranted = _recordingInfo.accessGranted, recording = _recordingInfo.recording; if (this.pendingStopRequestIds.has(requestId)) { if (accessGranted) { try { yield _native.NativeHandlers.Audio.stopRecording(); } catch {} } this.pendingStopRequestIds.delete(requestId); if (requestId === this.startRequestId) { this.reset(); } return accessGranted; } if (requestId !== this.startRequestId) { return accessGranted; } if (accessGranted && recording) { if (recording && typeof recording !== 'string' && recording.setProgressUpdateInterval) { recording.setProgressUpdateInterval(_reactNative.Platform.OS === 'android' ? 100 : 60); } this.state.partialNext({ isStarting: false, recording, status: 'recording' }); } else { this.reset(); } return accessGranted; }); function startRecording() { return _startRecording.apply(this, arguments); } return startRecording; }() }, { key: "stopRecording", value: function () { var _stopRecording = (0, _asyncToGenerator2.default)(function* (withDelete = false) { if (!_native.NativeHandlers.Audio) { return; } var _this$state$getLatest2 = this.state.getLatestValue(), isStarting = _this$state$getLatest2.isStarting, status = _this$state$getLatest2.status; if (isStarting) { this.pendingStopRequestIds.add(this.startRequestId); this.reset(); return; } if (status !== 'recording') { if (withDelete) { this.reset(); } return; } try { yield _native.NativeHandlers.Audio.stopRecording(); } catch {} if (withDelete) { this.reset(); } else { this.state.partialNext({ isStarting: false, status: 'stopped' }); } }); function stopRecording() { return _stopRecording.apply(this, arguments); } return stopRecording; }() }, { key: "micLocked", set: function (value) { this.state.partialNext({ micLocked: value }); } }, { key: "status", set: function (value) { this.state.partialNext({ status: value }); } }]); }(); //# sourceMappingURL=audio-recorder-manager.js.map