From 99dc34879c7c070b175d502397add05f5152f81c Mon Sep 17 00:00:00 2001 From: Jason Dreyzehner Date: Wed, 28 Sep 2016 22:37:33 -0400 Subject: [PATCH] fix(library): fixes an issue with optional callbacks being required 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 --- readme.md | 2 +- src/browser/src/library.js | 16 ++++++++++++++-- src/common/src/createQRScannerAdapter.js | 3 +++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index d63eccd8..3a6dc859 100755 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/src/browser/src/library.js b/src/browser/src/library.js index 1a2c4ee3..ed7893c0 100644 --- a/src/browser/src/library.js +++ b/src/browser/src/library.js @@ -19,6 +19,18 @@ 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){ @@ -26,9 +38,9 @@ function QRScanner() { 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)); } } }; diff --git a/src/common/src/createQRScannerAdapter.js b/src/common/src/createQRScannerAdapter.js index 45b98dde..184708a2 100644 --- a/src/common/src/createQRScannerAdapter.js +++ b/src/common/src/createQRScannerAdapter.js @@ -49,6 +49,9 @@ function clearBackground() { } function errorCallback(callback) { + if (!callback) { + return null; + } return function(error) { var errorCode = parseInt(error); var QRScannerError = {};