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

Fix a race when starting the scanner multiple times

We would get two scanning loops both scanning for qr codes. This makes
sure only one loop is active at all times.
parent 06653184
No related branches found
No related tags found
No related merge requests found
...@@ -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._requestID = null;
} }
static get scopedElements() { static get scopedElements() {
...@@ -192,13 +193,20 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { ...@@ -192,13 +193,20 @@ 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); if (that._requestID !== null) {
cancelAnimationFrame(that._requestID);
that._requestID = null;
}
console.assert(that._requestID === null);
that._requestID = 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._requestID = 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 +348,8 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { ...@@ -340,7 +348,8 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) {
qrContainer.scrollIntoView({ behavior: 'smooth', block: 'start' }); qrContainer.scrollIntoView({ behavior: 'smooth', block: 'start' });
scroll = true; scroll = true;
} }
requestAnimationFrame(tick); console.assert(that._requestID === null);
that._requestID = requestAnimationFrame(tick);
} }
} }
......
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