From 3c5c82f9799503890cbe5b937c2a0eee21723acb Mon Sep 17 00:00:00 2001 From: Christoph Reiter <reiter.christoph@gmail.com> Date: Tue, 20 Oct 2020 16:14:30 +0200 Subject: [PATCH] Properly cancel all queued callbacks when stopping --- packages/qr-code-scanner/src/qr-code-scanner.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/qr-code-scanner/src/qr-code-scanner.js b/packages/qr-code-scanner/src/qr-code-scanner.js index 642fef71..e9d814f2 100644 --- a/packages/qr-code-scanner/src/qr-code-scanner.js +++ b/packages/qr-code-scanner/src/qr-code-scanner.js @@ -86,6 +86,7 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { this.clipMask = false; this._devices = new Map(); + this._frameRequestID = null; } static get scopedElements() { @@ -192,13 +193,15 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { video.play(); that.videoRunning = true; qrContainer.scrollIntoView({ behavior: 'smooth', block: 'start' }); - requestAnimationFrame(tick); + that._frameRequestID = requestAnimationFrame(tick); }).catch((e) => { console.log(e); that.askPermission = true;}); let lastVideoTime = -1; let lastCode = null; function tick() { + that._frameRequestID = null; + if (that.sourceChanged) { video.srcObject.getTracks().forEach(function(track) { track.stop(); @@ -340,7 +343,7 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { qrContainer.scrollIntoView({ behavior: 'smooth', block: 'start' }); scroll = true; } - requestAnimationFrame(tick); + that._frameRequestID = requestAnimationFrame(tick); } } @@ -359,6 +362,11 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { * */ stopScanning() { + if (this._frameRequestID !== null) { + cancelAnimationFrame(this._frameRequestID); + this._frameRequestID = null; + } + this.askPermission = false; this.videoRunning = false; } -- GitLab