Skip to content

Commit

Permalink
fix(library): fixes an issue with optional callbacks being required
Browse files Browse the repository at this point in the history
Only the `scan` and `getStatus` methods should be required. This fixes a bug which threw and error

when other methods were called without providing a callback.

#32
  • Loading branch information
bitjson committed Sep 29, 2016
1 parent 9110981 commit 99dc348
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Or alternatively, the library can be included in a page as-is, and the QRScanner
On the browser platform, performance is improved by running the processing-intensive scanning operation in a [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API). For more information about the browser platform, see [Browser Platform Specific Details](#browser).

## API
With the exception of `QRScanner.scan(callback)`, all callbacks are optional.
With the exception of `QRScanner.scan(callback)` and `QRScanner.getStatus(callback)`, all callbacks are optional.

### Prepare

Expand Down
16 changes: 14 additions & 2 deletions src/browser/src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,28 @@ function QRScanner() {
destroy: internal.destroy
};

// always returns an executable function for use by the internal component
// if a callback is provided, use it
function getFunc(callback){
if(typeof callback === "function"){
return callback;
}
return function(){
// callback is not needed
return;
};
}

// shim cordova's functionality for library usage
var shimCordova = {
exec: function(successCallback, errorCallback, className, functionName, inputArray){
if(className !== 'QRScanner' || !functionList[functionName]){
return errorCallback(0);
}
if(inputArray){
functionList[functionName](successCallback, errorCallback, inputArray);
functionList[functionName](getFunc(successCallback), getFunc(errorCallback), inputArray);
} else {
functionList[functionName](successCallback, errorCallback);
functionList[functionName](getFunc(successCallback), getFunc(errorCallback));
}
}
};
Expand Down
3 changes: 3 additions & 0 deletions src/common/src/createQRScannerAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ function clearBackground() {
}

function errorCallback(callback) {
if (!callback) {
return null;
}
return function(error) {
var errorCode = parseInt(error);
var QRScannerError = {};
Expand Down

0 comments on commit 99dc348

Please sign in to comment.