// import { action, computed, makeObservable, observable } from 'mobx'; // // import { ENDPOINT } from '../shared/endpointConstants'; // import AppState from '../shared/stores/appState'; // import SessionStore, { FDSource } from './sessionStore'; // // import { downloadFromUrl } from '../../shared/util'; // // import { Timerange } from '../shared/components/MultiTimeVis'; // // const PROGRESS_INTERVAL: number = 100; // // export default class PlaybackStore { // @observable playing: boolean = false; // @observable progressMs: number = 0; // // _audio: HTMLAudioElement; // appState: AppState; // sessionStore: SessionStore; // _playbackStart: number; // _playing: Promise; // _scrolling: number; // constructor(appState: AppState, sessionStore: SessionStore) { // makeObservable(this); // this.appState = appState; // this.sessionStore = sessionStore; // } // // @action.bound getSignedUrl(bt?: number, tt?: number): Promise { // if (!this.sessionId) { // return Promise.resolve(''); // } // const requestArgs: any = { session: this.sessionId }; // if (bt) { // requestArgs.bt = bt; // } // if (tt) { // requestArgs.tt = tt; // } // return this.appState.apiRequest(ENDPOINT.SIGN_AUDIO_URL, requestArgs) // .then((result: { url: string }) => { // console.log({ url: result.url }); // return result.url; // }); // } // // @action.bound playAnnotation(annotation: any): Promise { // const bt: number = annotation.bt || 0; // const tt: number = annotation.tt || 0; // return this.playSession(bt, tt); // } // // @action.bound startPlayback(fromRatio?: number, zoomedOut?: boolean): Promise { // if (!this.sessionStore.tt) { // return; // } // console.log({ fromRatio }); // const bt: number = this.sessionStore.getBtFromRatio(fromRatio || 0); // if (bt) { // this.progressMs = (bt - this.sessionStore.bt) / 1000; // } // return this.playSession(bt, this.sessionStore.tt); // } // // @action playSession(bt?: number, tt?: number): Promise { // const ready: Promise = this.playing ? this.stopPlayback() : Promise.resolve(); // const playbackStart: number = performance.now(); // this._playbackStart = playbackStart; // return ready.then(() => this.getSignedUrl(bt, tt)) // .then((url: string) => { // if (!url || this._playbackStart !== playbackStart) { // return null; // } // this.playing = true; // if (this._audio && this._audio.pause) { // this._audio.pause(); // } // this._audio = new Audio(url); // this._audio.onplaying = () => this.handlePlaybackStarted(bt); // this._audio.onended = this.handlePlaybackEnded; // this._audio.onpause = this.handlePlaybackEnded; // this._audio.onerror = this.handlePlaybackError; // this._playing = this._audio.play().catch(this.handlePlaybackError); // }); // } // // @action.bound handlePlaybackStarted(bt?: number): void { // this.scrollIndicator(bt); // } // // @action.bound handlePlaybackError(e: any): void { // console.log('playback error: ', e); // this.stopPlayback(); // } // // @action.bound scrollIndicator(bt?: number): void { // if (bt) { // this.progressMs = (bt - this.sessionStore.bt) / 1000; // } // let now: number = performance.now(); // const scroll: VoidFunction = () => { // const newNow: number = performance.now(); // const delta: number = newNow - now; // this.progressMs += delta; // now = newNow; // }; // if (this._scrolling) { // this.stopIndicator(); // } // this._scrolling = window.setInterval(scroll, PROGRESS_INTERVAL); // } // // @action.bound stopIndicator(): void { // window.clearInterval(this._scrolling); // this._scrolling = null; // } // // @action.bound handlePlaybackEnded(): void { // if (this._audio) { // this._audio.src = ''; // } // this.playing = false; // this.stopIndicator(); // this._audio = null; // } // // @action.bound forceStopPlayback(): Promise { // this._playbackStart = 0; // if (this._audio) { // this._audio.src = ''; // } // return Promise.resolve(this.handlePlaybackEnded()); // } // // @action.bound stopPlayback(): Promise { // return Promise.resolve(this.handlePlaybackEnded()); // } // // @action.bound togglePlayback(): void { // if (this.playing) { // this.forceStopPlayback(); // } else { // this.startPlayback(this.sessionStore.currentSelection ? // this.sessionStore.currentSelection[0] : this.progressRatio, true); // } // } // // @action.bound setProgressFromRatio(fromRatio: number): void { // if (this.playing) { // return; // } // const bt: number = this.sessionStore.getBtFromRatio(fromRatio || 0); // this.progressMs = (bt - this.sessionStore.bt) / 1000; // } // // @computed get sessionId(): string { // const source: FDSource = this.sessionStore.currentSource; // if (!source) { // return ''; // } // return `${source._id}_${source.bt}`; // } // // @computed get progressRatio(): number { // return this.progressMs / (this.sessionStore.duration / 1000); // } // }