Skip to content

Commit

Permalink
bug: fix stream controller not being closed
Browse files Browse the repository at this point in the history
  • Loading branch information
juliansteenbakker committed Dec 5, 2023
1 parent 91a0394 commit 327a114
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/mobile_scanner_web_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ class MobileScannerWebPlugin {

/// Stops the video feed and analyzer
Future<void> cancel() async {
barCodeReader.stop();
await barCodeReader.stop();
await barCodeReader.stopDetectBarcodeContinuously();
await _barCodeStreamSubscription?.cancel();
_barCodeStreamSubscription = null;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/src/web/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ abstract class WebBarcodeReaderBase {
/// Starts scanning QR codes or barcodes
Stream<Barcode?> detectBarcodeContinuously();

/// Stops scanning QR codes or barcodes
Future<void> stopDetectBarcodeContinuously();

/// Stops streaming video
Future<void> stop();

Expand Down
5 changes: 5 additions & 0 deletions lib/src/web/jsqr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class JsQrCodeReader extends WebBarcodeReaderBase
});
}

@override
Future<void> stopDetectBarcodeContinuously() async {
return;
}

/// Captures a frame and analyzes it for QR codes
Future<Code?> _captureFrame(VideoElement video) async {
if (localMediaStream == null) return null;
Expand Down
22 changes: 14 additions & 8 deletions lib/src/web/zxing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,24 +250,30 @@ class ZXingBarcodeReader extends WebBarcodeReaderBase
await videoSource.play();
}

StreamController<Barcode?>? controller;

@override
Future<void> stopDetectBarcodeContinuously() async {
_reader?.stopContinuousDecode();
controller?.close();
controller = null;
}

@override
Stream<Barcode?> detectBarcodeContinuously() {
final controller = StreamController<Barcode?>();
controller.onListen = () async {
controller ??= StreamController<Barcode?>();
controller!.onListen = () async {
_reader?.decodeContinuously(
video,
allowInterop((result, error) {
if (result != null) {
controller.add(result.toBarcode());
controller?.add(result.toBarcode());
}
}),
);
};
controller.onCancel = () {
_reader?.stopContinuousDecode();
controller.close();
};
return controller.stream;
controller!.onCancel = () => stopDetectBarcodeContinuously();
return controller!.stream;
}

@override
Expand Down

0 comments on commit 327a114

Please sign in to comment.