From c10696f88c9c0a1377867e5a8190155352650240 Mon Sep 17 00:00:00 2001 From: Corey Frang Date: Tue, 3 Apr 2018 15:07:50 -0400 Subject: [PATCH] Rewrite the setup/disable process from review comments --- src/io/video.js | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/io/video.js b/src/io/video.js index 59b04d6f0..f8957dd62 100644 --- a/src/io/video.js +++ b/src/io/video.js @@ -103,10 +103,7 @@ class Video { io._requests.splice(index, 1); } if (io._requests.length === 0) { - io._disablePreview(); - // by clearing refs to video and track, we should lose our hold over the camera - io._video = null; - io._track = null; + io._disableVideo(); } } }; @@ -116,24 +113,10 @@ class Video { return Promise.resolve(request); } - if (this._lastSetup) { - return this._lastSetup.then(() => { - this._requests.push(request); - return request; - }); - } - - this._lastSetup = this._setupVideo() - .then(() => { - this._setupPreview(); - this._requests.push(request); - this._lastSetup = null; - return request; - }, err => { - this._lastSetup = null; - throw err; - }); - return this._lastSetup; + return this._setupVideo().then(() => { + this._requests.push(request); + return request; + }); } /** @@ -143,8 +126,12 @@ class Video { * @return {Promise} When video has been received, rejected if video is not received */ _setupVideo () { + if (this._lastSetup) { + return this._lastSetup; + } + this._video = document.createElement('video'); - return new Promise((resolve, reject) => { + const video = new Promise((resolve, reject) => { navigator.getUserMedia({ audio: false, video: { @@ -162,9 +149,20 @@ class Video { resolve(this._video); }, err => { // There are probably some error types we could handle gracefully here. + this._lastSetup = null; reject(err); }); }); + + return video.then(() => this._setupPreview()); + } + + _disableVideo () { + this._disablePreview(); + this._lastSetup = null; + // by clearing refs to video and track, we should lose our hold over the camera + this._video = null; + this._track = null; } _disablePreview () {