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
// connect anything to the context (this would generate a warning) // close the context after the recorder so the recorder doesn't try to
.then(() => this.recorderContext.close()) // connect anything to the context (this would generate a warning)
// Now stop all the media tracks so we can release them back to the user/OS await this.recorderContext.close();
.then(() => this.recorderStream.getTracks().forEach(t => t.stop()))
// Finally do our post-processing and clean up // Now stop all the media tracks so we can release them back to the user/OS
.then(() => { this.recorderStream.getTracks().forEach(t => t.stop());
clearInterval(this.freqTimerId);
this.recording = false; // Finally do our post-processing and clean up
return this.recorder.close(); clearInterval(this.freqTimerId);
}).then(() => this.buffer); this.recording = false;
await this.recorder.close();
return this.buffer;
} }
public async upload(): Promise<string> { public async upload(): Promise<string> {