Dynamsoft / barcode-reader-javascript

Dynamsoft Barcode Reader JavaScript SDK for package managers. PDF417, QR Code, DataMatrix, MaxiCode and more are supported.
https://www.dynamsoft.com/barcode-reader/sdk-javascript/
Other
169 stars 111 forks source link

async method BarcodeScanner.show throws an error if scanner was destroyed during it's execution #79

Closed JCown closed 3 years ago

JCown commented 3 years ago

Hey, I'm using version 8.1.2 of https://www.npmjs.com/package/dynamsoft-javascript-barcode

Having a valid initialization and working scanner I've noticed the following (edge-case) error: Let's say we have a SPA application, and a view/page that displays fullpage barcode scanner. On view/page leave we call BarcodeScanner.destroy() method (for example in router guard in case of Vue framework) to destroy scanner and close video streams. An example of page leave would be simply using browser/device navigation back button.

If user redirects away from the page/view during barcode scanner initialization (speaking more precisely - during execution of BarcodeScanner.show() method) then the following error is thrown:

Uncaught (in promise) TypeError: Cannot set property 'srcObject' of null
    at h.eval (dbr.browser.mjs?6c78:11)`

and video stream is not closed correctly (a browser indicator that this page is using or camera/microphone is still being displayed).

Example code to reproduce this issue:

this.barcodeScannerInstance.show();
// this simulates cleanup on quick page leave - after 100ms just destroy scanner
setTimeout(() => {
  this.barcodeScannerInstance && this.barcodeScannerInstance.destroy();
}, 100);

BarcodeScanner.show() should perform an extra check for already destroyed scanner and correctly close video stream in such case.

Keillion commented 3 years ago

Fixed in dev branch.

https://github.com/dynamsoft-rd-0/dbrjs-mass-samples/blob/master/destroyDuringShow/fixed.html

While throwing The BarcodeScanner instance has been destroyed, dbrjs will release camera.

Merged to the next official version.

Keillion commented 3 years ago

Fixed in dbrjs 8.1.3.

95919a8