From 2186f6278324967d25c35c5a9154bd0918172f69 Mon Sep 17 00:00:00 2001 From: Christoph Reiter <reiter.christoph@gmail.com> Date: Wed, 21 Oct 2020 11:23:19 +0200 Subject: [PATCH] Convert qrCodeScannerInit() to an async function fewer confusing callbacks.. --- .../qr-code-scanner/src/qr-code-scanner.js | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/qr-code-scanner/src/qr-code-scanner.js b/packages/qr-code-scanner/src/qr-code-scanner.js index d4d79d9b..ac79fee3 100644 --- a/packages/qr-code-scanner/src/qr-code-scanner.js +++ b/packages/qr-code-scanner/src/qr-code-scanner.js @@ -146,7 +146,7 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { * Init and start the video and QR code scan * */ - qrCodeScannerInit() { + async qrCodeScannerInit() { this.stopScan = false; this.askPermission = true; @@ -185,22 +185,29 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { constraint = { video: { facingMode: "user" } }; } - const that = this; + let stream = null; + try { + stream = await navigator.mediaDevices.getUserMedia(constraint); + } catch(e) { + console.log(e); + this.askPermission = true; + } - navigator.mediaDevices.getUserMedia(constraint).then(function(stream) { + if (stream !== null) { video.srcObject = stream; video.setAttribute("playsinline", true); // required to tell iOS safari we don't want fullscreen video.play(); - that.videoRunning = true; + this.videoRunning = true; qrContainer.scrollIntoView({ behavior: 'smooth', block: 'start' }); - if (that._requestID !== null) { - cancelAnimationFrame(that._requestID); - that._requestID = null; + if (this._requestID !== null) { + cancelAnimationFrame(this._requestID); + this._requestID = null; } - console.assert(that._requestID === null); - that._requestID = requestAnimationFrame(tick); - }).catch((e) => { console.log(e); that.askPermission = true;}); + console.assert(this._requestID === null); + this._requestID = requestAnimationFrame(tick); + } + const that = this; let lastVideoTime = -1; let lastCode = null; -- GitLab