| 
 | 1 | +/*  | 
 | 2 | +Copyright 2021 The Matrix.org Foundation C.I.C.  | 
 | 3 | +
  | 
 | 4 | +Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 5 | +you may not use this file except in compliance with the License.  | 
 | 6 | +You may obtain a copy of the License at  | 
 | 7 | +
  | 
 | 8 | + http://www.apache.org/licenses/LICENSE-2.0  | 
 | 9 | +
  | 
 | 10 | +Unless required by applicable law or agreed to in writing, software  | 
 | 11 | +distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 13 | +See the License for the specific language governing permissions and  | 
 | 14 | +limitations under the License.  | 
 | 15 | +*/  | 
 | 16 | + | 
 | 17 | +import {AsyncStoreWithClient} from "./AsyncStoreWithClient";  | 
 | 18 | +import defaultDispatcher from "../dispatcher/dispatcher";  | 
 | 19 | +import {ActionPayload} from "../dispatcher/payloads";  | 
 | 20 | +import {VoiceRecording} from "../voice/VoiceRecording";  | 
 | 21 | + | 
 | 22 | +interface IState {  | 
 | 23 | + recording?: VoiceRecording;  | 
 | 24 | +}  | 
 | 25 | + | 
 | 26 | +export class VoiceRecordingStore extends AsyncStoreWithClient<IState> {  | 
 | 27 | + private static internalInstance: VoiceRecordingStore;  | 
 | 28 | + | 
 | 29 | + public constructor() {  | 
 | 30 | + super(defaultDispatcher, {});  | 
 | 31 | + }  | 
 | 32 | + | 
 | 33 | + /**  | 
 | 34 | + * Gets the active recording instance, if any.  | 
 | 35 | + */  | 
 | 36 | + public get activeRecording(): VoiceRecording | null {  | 
 | 37 | + return this.state.recording;  | 
 | 38 | + }  | 
 | 39 | + | 
 | 40 | + public static get instance(): VoiceRecordingStore {  | 
 | 41 | + if (!VoiceRecordingStore.internalInstance) {  | 
 | 42 | + VoiceRecordingStore.internalInstance = new VoiceRecordingStore();  | 
 | 43 | + }  | 
 | 44 | + return VoiceRecordingStore.internalInstance;  | 
 | 45 | + }  | 
 | 46 | + | 
 | 47 | + protected async onAction(payload: ActionPayload): Promise<void> {  | 
 | 48 | + // Nothing to do, but we're required to override the function  | 
 | 49 | + return;  | 
 | 50 | + }  | 
 | 51 | + | 
 | 52 | + /**  | 
 | 53 | + * Starts a new recording if one isn't already in progress. Note that this simply  | 
 | 54 | + * creates a recording instance - whether or not recording is actively in progress  | 
 | 55 | + * can be seen via the VoiceRecording class.  | 
 | 56 | + * @returns {VoiceRecording} The recording.  | 
 | 57 | + */  | 
 | 58 | + public startRecording(): VoiceRecording {  | 
 | 59 | + if (!this.matrixClient) throw new Error("Cannot start a recording without a MatrixClient");  | 
 | 60 | + if (this.state.recording) throw new Error("A recording is already in progress");  | 
 | 61 | + | 
 | 62 | + const recording = new VoiceRecording(this.matrixClient);  | 
 | 63 | + | 
 | 64 | + // noinspection JSIgnoredPromiseFromCall - we can safely run this async  | 
 | 65 | + this.updateState({recording});  | 
 | 66 | + | 
 | 67 | + return recording;  | 
 | 68 | + }  | 
 | 69 | + | 
 | 70 | + /**  | 
 | 71 | + * Disposes of the current recording, no matter the state of it.  | 
 | 72 | + * @returns {Promise<void>} Resolves when complete.  | 
 | 73 | + */  | 
 | 74 | + public disposeRecording(): Promise<void> {  | 
 | 75 | + if (this.state.recording) {  | 
 | 76 | + // Stop for good measure, but completely async because we're not concerned with this  | 
 | 77 | + // passing or failing.  | 
 | 78 | + this.state.recording.stop().catch(e => console.error("Error stopping recording", e));  | 
 | 79 | + }  | 
 | 80 | + return this.updateState({recording: null});  | 
 | 81 | + }  | 
 | 82 | +}  | 
0 commit comments