Julian Steenbakker

bug: fix stream controller not being closed

@@ -186,7 +186,8 @@ class MobileScannerWebPlugin { @@ -186,7 +186,8 @@ class MobileScannerWebPlugin {
186 186
187 /// Stops the video feed and analyzer 187 /// Stops the video feed and analyzer
188 Future<void> cancel() async { 188 Future<void> cancel() async {
189 - barCodeReader.stop(); 189 + await barCodeReader.stop();
  190 + await barCodeReader.stopDetectBarcodeContinuously();
190 await _barCodeStreamSubscription?.cancel(); 191 await _barCodeStreamSubscription?.cancel();
191 _barCodeStreamSubscription = null; 192 _barCodeStreamSubscription = null;
192 } 193 }
@@ -50,6 +50,9 @@ abstract class WebBarcodeReaderBase { @@ -50,6 +50,9 @@ abstract class WebBarcodeReaderBase {
50 /// Starts scanning QR codes or barcodes 50 /// Starts scanning QR codes or barcodes
51 Stream<Barcode?> detectBarcodeContinuously(); 51 Stream<Barcode?> detectBarcodeContinuously();
52 52
  53 + /// Stops scanning QR codes or barcodes
  54 + Future<void> stopDetectBarcodeContinuously();
  55 +
53 /// Stops streaming video 56 /// Stops streaming video
54 Future<void> stop(); 57 Future<void> stop();
55 58
@@ -83,6 +83,11 @@ class JsQrCodeReader extends WebBarcodeReaderBase @@ -83,6 +83,11 @@ class JsQrCodeReader extends WebBarcodeReaderBase
83 }); 83 });
84 } 84 }
85 85
  86 + @override
  87 + Future<void> stopDetectBarcodeContinuously() async {
  88 + return;
  89 + }
  90 +
86 /// Captures a frame and analyzes it for QR codes 91 /// Captures a frame and analyzes it for QR codes
87 Future<Code?> _captureFrame(VideoElement video) async { 92 Future<Code?> _captureFrame(VideoElement video) async {
88 if (localMediaStream == null) return null; 93 if (localMediaStream == null) return null;
@@ -250,24 +250,30 @@ class ZXingBarcodeReader extends WebBarcodeReaderBase @@ -250,24 +250,30 @@ class ZXingBarcodeReader extends WebBarcodeReaderBase
250 await videoSource.play(); 250 await videoSource.play();
251 } 251 }
252 252
  253 + StreamController<Barcode?>? controller;
  254 +
  255 + @override
  256 + Future<void> stopDetectBarcodeContinuously() async {
  257 + _reader?.stopContinuousDecode();
  258 + controller?.close();
  259 + controller = null;
  260 + }
  261 +
253 @override 262 @override
254 Stream<Barcode?> detectBarcodeContinuously() { 263 Stream<Barcode?> detectBarcodeContinuously() {
255 - final controller = StreamController<Barcode?>();  
256 - controller.onListen = () async { 264 + controller ??= StreamController<Barcode?>();
  265 + controller!.onListen = () async {
257 _reader?.decodeContinuously( 266 _reader?.decodeContinuously(
258 video, 267 video,
259 allowInterop((result, error) { 268 allowInterop((result, error) {
260 if (result != null) { 269 if (result != null) {
261 - controller.add(result.toBarcode()); 270 + controller?.add(result.toBarcode());
262 } 271 }
263 }), 272 }),
264 ); 273 );
265 }; 274 };
266 - controller.onCancel = () {  
267 - _reader?.stopContinuousDecode();  
268 - controller.close();  
269 - };  
270 - return controller.stream; 275 + controller!.onCancel = () => stopDetectBarcodeContinuously();
  276 + return controller!.stream;
271 } 277 }
272 278
273 @override 279 @override