From d929d4839154927c415110ea89db90160fe934dc Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Tue, 23 Mar 2021 18:26:43 -0600 Subject: [PATCH] Clean up promises --- src/voice/VoiceRecorder.ts | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/voice/VoiceRecorder.ts b/src/voice/VoiceRecorder.ts index 9fa2faad1e..06c0d939fc 100644 --- a/src/voice/VoiceRecorder.ts +++ b/src/voice/VoiceRecorder.ts @@ -133,27 +133,32 @@ export class VoiceRecorder { dbMax: this.recorderFreqNode.maxDecibels, }); }, 1000 / FREQ_SAMPLE_RATE) as any as number; // XXX: Linter doesn't understand timer environment - return this.recorder.start().then(() => this.recording = true); + await this.recorder.start(); + this.recording = true; } public async stop(): Promise { if (!this.recording) { throw new Error("No recording to stop"); } + // Disconnect the source early to start shutting down resources this.recorderSource.disconnect(); - return this.recorder.stop() - // close the context after the recorder so the recorder doesn't try to - // connect anything to the context (this would generate a warning) - .then(() => this.recorderContext.close()) - // Now stop all the media tracks so we can release them back to the user/OS - .then(() => this.recorderStream.getTracks().forEach(t => t.stop())) - // Finally do our post-processing and clean up - .then(() => { - clearInterval(this.freqTimerId); - this.recording = false; - return this.recorder.close(); - }).then(() => this.buffer); + await this.recorder.stop(); + + // close the context after the recorder so the recorder doesn't try to + // connect anything to the context (this would generate a warning) + await this.recorderContext.close(); + + // Now stop all the media tracks so we can release them back to the user/OS + this.recorderStream.getTracks().forEach(t => t.stop()); + + // Finally do our post-processing and clean up + clearInterval(this.freqTimerId); + this.recording = false; + await this.recorder.close(); + + return this.buffer; } public async upload(): Promise {