Skip to content
Snippets Groups Projects
Commit 3c5c82f9 authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Properly cancel all queued callbacks when stopping

parent dcbe79dc
No related branches found
No related tags found
No related merge requests found
Pipeline #13611 passed
...@@ -86,6 +86,7 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { ...@@ -86,6 +86,7 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) {
this.clipMask = false; this.clipMask = false;
this._devices = new Map(); this._devices = new Map();
this._frameRequestID = null;
} }
static get scopedElements() { static get scopedElements() {
...@@ -192,13 +193,15 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { ...@@ -192,13 +193,15 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) {
video.play(); video.play();
that.videoRunning = true; that.videoRunning = true;
qrContainer.scrollIntoView({ behavior: 'smooth', block: 'start' }); qrContainer.scrollIntoView({ behavior: 'smooth', block: 'start' });
requestAnimationFrame(tick); that._frameRequestID = requestAnimationFrame(tick);
}).catch((e) => { console.log(e); that.askPermission = true;}); }).catch((e) => { console.log(e); that.askPermission = true;});
let lastVideoTime = -1; let lastVideoTime = -1;
let lastCode = null; let lastCode = null;
function tick() { function tick() {
that._frameRequestID = null;
if (that.sourceChanged) { if (that.sourceChanged) {
video.srcObject.getTracks().forEach(function(track) { video.srcObject.getTracks().forEach(function(track) {
track.stop(); track.stop();
...@@ -340,7 +343,7 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { ...@@ -340,7 +343,7 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) {
qrContainer.scrollIntoView({ behavior: 'smooth', block: 'start' }); qrContainer.scrollIntoView({ behavior: 'smooth', block: 'start' });
scroll = true; scroll = true;
} }
requestAnimationFrame(tick); that._frameRequestID = requestAnimationFrame(tick);
} }
} }
...@@ -359,6 +362,11 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { ...@@ -359,6 +362,11 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) {
* *
*/ */
stopScanning() { stopScanning() {
if (this._frameRequestID !== null) {
cancelAnimationFrame(this._frameRequestID);
this._frameRequestID = null;
}
this.askPermission = false; this.askPermission = false;
this.videoRunning = false; this.videoRunning = false;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment