Julian Steenbakker

bug: fix stream controller not being closed

... ... @@ -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;
}
... ...
... ... @@ -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();
... ...
... ... @@ -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;
... ...
... ... @@ -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
... ...