diff --git a/src/windows/BarcodeScannerProxy.js b/src/windows/BarcodeScannerProxy.js index 4385c94d..e2c66976 100644 --- a/src/windows/BarcodeScannerProxy.js +++ b/src/windows/BarcodeScannerProxy.js @@ -284,6 +284,7 @@ module.exports = { closeButton.className = "app-bar-action action-close"; navigationButtonsDiv.appendChild(closeButton); + BarcodeReader.scanCancelled = false; closeButton.addEventListener("click", cancelPreview, false); document.addEventListener('backbutton', cancelPreview, false); @@ -472,14 +473,23 @@ module.exports = { * See https://github.com/phonegap-build/BarcodeScanner#using-the-plugin */ function cancelPreview() { + BarcodeReader.scanCancelled = true; reader && reader.stop(); } + function checkCancelled() { + if (BarcodeReader.scanCancelled) { + throw new Error('Canceled'); + } + } + WinJS.Promise.wrap(createPreview()) .then(function () { + checkCancelled(); return startPreview(); }) .then(function (captureSettings) { + checkCancelled(); reader = BarcodeReader.get(captureSettings.capture); reader.init(captureSettings.capture, captureSettings.width, captureSettings.height); @@ -487,6 +497,7 @@ module.exports = { // we would get an 'Invalid state' error from 'getPreviewFrameAsync' return WinJS.Promise.timeout(200) .then(function () { + checkCancelled(); return reader.readCode(); }); }) @@ -499,7 +510,14 @@ module.exports = { }); }, function (error) { destroyPreview(); - fail(error); + + if (error.message == 'Canceled') { + success({ + cancelled: true + }); + } else { + fail(error); + } }); },