From 584e932891be4211a3bbd58c14638ee907357492 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Fri, 5 Oct 2018 15:16:22 -0700 Subject: [PATCH] Close recorder on switch away, only send after finish clicked --- js/views/recorder_view.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/js/views/recorder_view.js b/js/views/recorder_view.js index 2a99d986f..d0b583e23 100644 --- a/js/views/recorder_view.js +++ b/js/views/recorder_view.js @@ -1,4 +1,4 @@ -/* global Whisper, moment, WebAudioRecorder */ +/* global $, Whisper, moment, WebAudioRecorder */ /* eslint-disable more/no-then */ @@ -14,6 +14,10 @@ initialize() { this.startTime = Date.now(); this.interval = setInterval(this.updateTime.bind(this), 1000); + + this.onSwitchAwayBound = this.onSwitchAway.bind(this); + $(window).on('blur', this.onSwitchAwayBound); + this.start(); }, events: { @@ -21,6 +25,9 @@ 'click .finish': 'finish', close: 'close', }, + onSwitchAway() { + this.close(); + }, updateTime() { const duration = moment.duration(Date.now() - this.startTime, 'ms'); const minutes = `${Math.trunc(duration.asMinutes())}`; @@ -58,17 +65,23 @@ this.remove(); this.trigger('closed'); + + $(window).off('blur', this.onSwitchAwayBound); }, finish() { + this.clickedFinish = true; this.recorder.finishRecording(); this.close(); }, handleBlob(recorder, blob) { - if (blob) { + if (blob && this.clickedFinish) { this.trigger('send', blob); + } else { + this.close(); } }, start() { + this.clickedFinish = false; this.context = new AudioContext(); this.input = this.context.createGain(); this.recorder = new WebAudioRecorder(this.input, { @@ -76,7 +89,7 @@ workerDir: 'js/', // must end with slash }); this.recorder.onComplete = this.handleBlob.bind(this); - this.recorder.onError = this.onError; + this.recorder.onError = this.onError.bind(this); navigator.webkitGetUserMedia( { audio: true }, stream => {