Clean up promises

This commit is contained in:
Travis Ralston 2021-03-23 18:26:43 -06:00
parent c9938ff704
commit d929d48391

View file

@ -133,27 +133,32 @@ export class VoiceRecorder {
dbMax: this.recorderFreqNode.maxDecibels, dbMax: this.recorderFreqNode.maxDecibels,
}); });
}, 1000 / FREQ_SAMPLE_RATE) as any as number; // XXX: Linter doesn't understand timer environment }, 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<Uint8Array> { public async stop(): Promise<Uint8Array> {
if (!this.recording) { if (!this.recording) {
throw new Error("No recording to stop"); throw new Error("No recording to stop");
} }
// Disconnect the source early to start shutting down resources // Disconnect the source early to start shutting down resources
this.recorderSource.disconnect(); this.recorderSource.disconnect();
return this.recorder.stop() await this.recorder.stop();
// close the context after the recorder so the recorder doesn't try to // close the context after the recorder so the recorder doesn't try to
// connect anything to the context (this would generate a warning) // connect anything to the context (this would generate a warning)
.then(() => this.recorderContext.close()) await this.recorderContext.close();
// Now stop all the media tracks so we can release them back to the user/OS // Now stop all the media tracks so we can release them back to the user/OS
.then(() => this.recorderStream.getTracks().forEach(t => t.stop())) this.recorderStream.getTracks().forEach(t => t.stop());
// Finally do our post-processing and clean up // Finally do our post-processing and clean up
.then(() => {
clearInterval(this.freqTimerId); clearInterval(this.freqTimerId);
this.recording = false; this.recording = false;
return this.recorder.close(); await this.recorder.close();
}).then(() => this.buffer);
return this.buffer;
} }
public async upload(): Promise<string> { public async upload(): Promise<string> {