Skip to content

Commit

Permalink
Scanner#Stop to return Promise (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
mebjas authored Jul 5, 2020
1 parent fabb8da commit 14fbc64
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 70 deletions.
87 changes: 45 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,48 +323,51 @@ class Html5Qrcode {
}

class Html5QrcodeScanner {
/**
* Creates instance of this class.
*
* @param {String} elementId - Id of the HTML element.
* @param {Object} config extra configurations to tune QR code scanner.
* Supported Fields:
* - fps: expected framerate of qr scanning. example { fps: 2 }
* means the scanning would be done every 500 ms.
* - qrbox: width of QR scanning box, this should be smaller than
* the width and height of the box. This would make the scanner
* look like this:
* ----------------------
* |********************|
* |******,,,,,,,,,*****| <--- shaded region
* |******| |*****| <--- non shaded region would be
* |******| |*****| used for QR code scanning.
* |******|_______|*****|
* |********************|
* |********************|
* ----------------------
* @param {Boolean} verbose - Optional argument, if true, all logs
* would be printed to console.
*/
constructor(elementId, config, verbose) {}

/**
* Renders the User Interface
*
* @param {Function} qrCodeSuccessCallback - callback on QR Code found.
* Example:
* function(qrCodeMessage) {}
* @param {Function} qrCodeErrorCallback - callback on QR Code parse error.
* Example:
* function(errorMessage) {}
*
*/
render(qrCodeSuccessCallback, qrCodeErrorCallback) {}

/**
* Removes the QR Code scanner.
*/
clear() {}
/**
* Creates instance of this class.
*
* @param {String} elementId - Id of the HTML element.
* @param {Object} config extra configurations to tune QR code scanner.
* Supported Fields:
* - fps: expected framerate of qr scanning. example { fps: 2 }
* means the scanning would be done every 500 ms.
* - qrbox: width of QR scanning box, this should be smaller than
* the width and height of the box. This would make the scanner
* look like this:
* ----------------------
* |********************|
* |******,,,,,,,,,*****| <--- shaded region
* |******| |*****| <--- non shaded region would be
* |******| |*****| used for QR code scanning.
* |******|_______|*****|
* |********************|
* |********************|
* ----------------------
* @param {Boolean} verbose - Optional argument, if true, all logs
* would be printed to console.
*/
constructor(elementId, config, verbose) {}

/**
* Renders the User Interface
*
* @param {Function} qrCodeSuccessCallback - callback on QR Code found.
* Example:
* function(qrCodeMessage) {}
* @param {Function} qrCodeErrorCallback - callback on QR Code parse error.
* Example:
* function(errorMessage) {}
*
*/
render(qrCodeSuccessCallback, qrCodeErrorCallback) {}

/**
* Removes the QR Code scanner.
*
* @returns Promise which succeeds if the cleanup is complete successfully,
* fails otherwise.
*/
clear() {}
}
```

Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### Version 1.1.7
+ `Html5QrcodeScanner#clear()` returns a `Promise`.

### Version 1.1.6
+ Bug fixes
+ [#74](https://github.com/mebjas/html5-qrcode/issues/74) - state errors on file based scanning.
Expand Down
31 changes: 19 additions & 12 deletions html5-qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,9 @@ class Html5QrcodeScanner {

/**
* Removes the QR Code scanner.
*
* @returns Promise which succeeds if the cleanup is complete successfully,
* fails otherwise.
*/
clear() {
const $this = this;
Expand All @@ -946,21 +949,24 @@ class Html5QrcodeScanner {
}

if (this.html5Qrcode) {
if (this.html5Qrcode._isScanning()) {
this.html5Qrcode.stop().then(_ => {
$this.html5Qrcode.clear();
emptyHtmlContainer();
}).catch(error => {
if ($this.verbose) {
console.error("Unable to stop qrcode scanner", error);
}
$this.html5Qrcode.clear();
emptyHtmlContainer();
})
}
return new Promise((resolve, reject) => {
if (this.html5Qrcode._isScanning()) {
this.html5Qrcode.stop().then(_ => {
$this.html5Qrcode.clear();
emptyHtmlContainer();
resolve();
}).catch(error => {
if ($this.verbose) {
console.error("Unable to stop qrcode scanner", error);
}
reject(error);
})
}
});
}
}

//#region private control methods
__createBasicLayout(parent) {
parent.style.position = "relative";
parent.style.padding = "0px";
Expand Down Expand Up @@ -1373,6 +1379,7 @@ class Html5QrcodeScanner {
this.__getScanRegionId());
qrCodeScanRegion.innerHTML = "";
}
//#endregion

//#region state getters
__getDashboardSectionId() {
Expand Down
2 changes: 1 addition & 1 deletion minified/html5-qrcode.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "html5-qrcode",
"version": "1.1.6",
"version": "1.1.7",
"description": "a cross platform HTML5 QR Code scanner",
"main": "html5-qrcode.js",
"scripts": {
Expand Down
35 changes: 21 additions & 14 deletions transpiled/html5-qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,9 @@ var Html5QrcodeScanner = /*#__PURE__*/function () {
}
/**
* Removes the QR Code scanner.
*
* @returns Promise which succeeds if the cleanup is complete successfully,
* fails otherwise.
*/

}, {
Expand All @@ -1031,21 +1034,24 @@ var Html5QrcodeScanner = /*#__PURE__*/function () {
};

if (this.html5Qrcode) {
if (this.html5Qrcode._isScanning()) {
this.html5Qrcode.stop().then(function (_) {
$this.html5Qrcode.clear();
emptyHtmlContainer();
})["catch"](function (error) {
if ($this.verbose) {
console.error("Unable to stop qrcode scanner", error);
}
return new Promise(function (resolve, reject) {
if (_this4.html5Qrcode._isScanning()) {
_this4.html5Qrcode.stop().then(function (_) {
$this.html5Qrcode.clear();
emptyHtmlContainer();
resolve();
})["catch"](function (error) {
if ($this.verbose) {
console.error("Unable to stop qrcode scanner", error);
}

$this.html5Qrcode.clear();
emptyHtmlContainer();
});
}
reject(error);
});
}
});
}
}
} //#region private control methods

}, {
key: "__createBasicLayout",
value: function __createBasicLayout(parent) {
Expand Down Expand Up @@ -1479,7 +1485,8 @@ var Html5QrcodeScanner = /*#__PURE__*/function () {
value: function __clearScanRegion() {
var qrCodeScanRegion = document.getElementById(this.__getScanRegionId());
qrCodeScanRegion.innerHTML = "";
} //#region state getters
} //#endregion
//#region state getters

}, {
key: "__getDashboardSectionId",
Expand Down

0 comments on commit 14fbc64

Please sign in to comment.