Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Adds promise scan .done particularly to handle "No cameras" error (#363)
Browse files Browse the repository at this point in the history
Properly dispose capturePreview video element

This fixes #361
  • Loading branch information
Sergey Shakhnazarov authored Nov 17, 2016
1 parent 75cffc3 commit 68b6fd1
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/windows/BarcodeScannerProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,19 +534,39 @@ module.exports = {
Windows.Graphics.Display.DisplayInformation.getForCurrentView().removeEventListener("orientationchanged", updatePreviewForRotation, false);
document.removeEventListener('backbutton', cancelPreview);

capturePreview.pause();
capturePreview.src = null;
if (capturePreview) {
var isPlaying = !capturePreview.paused && !capturePreview.ended && capturePreview.readyState > 2;
if (isPlaying) {
capturePreview.pause();
}

// http://stackoverflow.com/a/28060352/4177762
capturePreview.src = "";
if (capturePreview.load) {
capturePreview.load();
}
}

if (capturePreviewFrame) {
document.body.removeChild(capturePreviewFrame);
try {
document.body.removeChild(capturePreviewFrame);
} catch (e) {
// Catching NotFoundError
console.error(e);
}
}
capturePreviewFrame = null;

reader && reader.stop();
reader = null;

if (capture) {
promise = capture.stopRecordAsync();
try {
promise = capture.stopRecordAsync();
} catch (e) {
// Catching NotFoundError
console.error(e);
}
}
capture = null;

Expand All @@ -570,8 +590,10 @@ module.exports = {
}
}

BarcodeReader.scanPromise = WinJS.Promise.wrap(createPreview())
.then(function () {
// Timeout is needed so that the .done finalizer below can be attached to the promise.
BarcodeReader.scanPromise = WinJS.Promise.timeout()
.then(function() {
createPreview();
checkCancelled();
return startPreview();
})
Expand Down Expand Up @@ -600,7 +622,10 @@ module.exports = {
format: result && BARCODE_FORMAT[result.barcodeFormat],
cancelled: !result
});
}, function (error) {
});

// Catching any errors here
BarcodeReader.scanPromise.done(function () { }, function (error) {
// Suppress null result (cancel) on suspending
if (BarcodeReader.suspended) {
return;
Expand Down

0 comments on commit 68b6fd1

Please sign in to comment.