Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: race condition #686

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/html5-qrcode-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export class Html5QrcodeScanner {
private persistedDataManager: PersistedDataManager;
private scanTypeSelector: ScanTypeSelector;
private logger: Logger;
private isCleared = false;

// Initally null fields.
private html5Qrcode: Html5Qrcode | undefined;
Expand Down Expand Up @@ -232,6 +233,7 @@ export class Html5QrcodeScanner {
public render(
qrCodeSuccessCallback: QrcodeSuccessCallback,
qrCodeErrorCallback: QrcodeErrorCallback | undefined) {
this.isCleared = false;
this.lastMatchFound = null;

// Add wrapper to success callback.
Expand Down Expand Up @@ -324,6 +326,8 @@ export class Html5QrcodeScanner {
* fails otherwise.
*/
public clear(): Promise<void> {
this.isCleared = true;

const emptyHtmlContainer = () => {
const mainContainer = document.getElementById(this.elementId);
if (mainContainer) {
Expand Down Expand Up @@ -546,6 +550,9 @@ export class Html5QrcodeScanner {
}

Html5Qrcode.getCameras().then((cameras) => {
if ($this.isCleared) {
return;
}
// By this point the user has granted camera permissions.
$this.persistedDataManager.setHasPermission(
/* hasPermission */ true);
Expand Down Expand Up @@ -612,6 +619,9 @@ export class Html5QrcodeScanner {
&& this.persistedDataManager.hasCameraPermissions()) {
CameraPermissions.hasPermissions().then(
(hasPermissions: boolean) => {
if ($this.isCleared) {
return;
}
if (hasPermissions) {
$this.createCameraListUi(
scpCameraScanRegion, requestPermissionContainer);
Expand Down Expand Up @@ -964,6 +974,9 @@ export class Html5QrcodeScanner {
CameraPermissions.hasPermissions().then(
(hasPermissions: boolean) => {
if (hasPermissions) {
if ($this.isCleared) {
return;
}
// Start feed.
// Assuming at this point the permission button exists.
let permissionButton = document.getElementById(
Expand Down