Simplify setup method promise, add onError

This commit is contained in:
Corey Frang 2018-04-05 16:48:42 -04:00 committed by Michael "Z" Goddard
parent 89dd4720a4
commit 0180964333
No known key found for this signature in database
GPG key ID: 762CD40DD5349872

View file

@ -98,7 +98,7 @@ class Video {
*/ */
enableVideo () { enableVideo () {
this.enabled = true; this.enabled = true;
return this._setupVideo().then(() => this); return this._setupVideo();
} }
/** /**
@ -222,15 +222,17 @@ class Video {
return this._singleSetup; return this._singleSetup;
} }
this._video = document.createElement('video'); this._singleSetup = new Promise((resolve, reject) => {
const video = new Promise((resolve, reject) => {
navigator.getUserMedia({ navigator.getUserMedia({
audio: false, audio: false,
video: { video: {
width: {min: 480, ideal: 640}, width: {min: 480, ideal: 640},
height: {min: 360, ideal: 480} height: {min: 360, ideal: 480}
} }
}, stream => { }, resolve, reject);
})
.then(stream => {
this._video = document.createElement('video');
this._video.src = window.URL.createObjectURL(stream); this._video.src = window.URL.createObjectURL(stream);
// Hint to the stream that it should load. A standard way to do this // Hint to the stream that it should load. A standard way to do this
// is add the video tag to the DOM. Since this extension wants to // is add the video tag to the DOM. Since this extension wants to
@ -238,20 +240,23 @@ class Video {
// the webgl rendered Scratch canvas, another hint like this one is // the webgl rendered Scratch canvas, another hint like this one is
// needed. // needed.
this._track = stream.getTracks()[0]; this._track = stream.getTracks()[0];
resolve(this._video); this._setupPreview();
}, err => { return this;
// There are probably some error types we could handle gracefully here. })
.catch(err => {
this._singleSetup = null; this._singleSetup = null;
reject(err); if (this.onError) {
}); this.onError(err);
} else {
log.error(`Unhandled io device error`, err);
}
}); });
this._singleSetup = video.then(() => this._setupPreview());
return this._singleSetup; return this._singleSetup;
} }
_disablePreview () { _disablePreview () {
if (this._skin && this._drawable) { if (this._skin) {
this._skin.clear(); this._skin.clear();
this.runtime.renderer.updateDrawableProperties(this._drawable, {visible: false}); this.runtime.renderer.updateDrawableProperties(this._drawable, {visible: false});
} }