|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:flutter/foundation.dart'; |
| 4 | +import 'package:rxdart/rxdart.dart'; |
| 5 | + |
| 6 | +import 'listenable_to_stream.dart'; |
| 7 | + |
| 8 | +/// Convert this [ValueListenable] to a [ValueStream]. |
| 9 | +/// The returned [ValueStream] is a Single-Subscription [Stream]. |
| 10 | +/// |
| 11 | +/// If [replayValue] is true, the returned [ValueStream] will replay latest value when listening to it. |
| 12 | +/// Otherwise, it does not. |
| 13 | +extension ValueListenableToValueStream<T> on ValueListenable<T> { |
| 14 | + /// Convert this [ValueListenable] to a [ValueStream]. |
| 15 | + /// The returned [ValueStream] is a Single-Subscription [Stream]. |
| 16 | + /// |
| 17 | + /// If [replayValue] is true, the returned [ValueStream] will replay latest value when listening to it. |
| 18 | + /// Otherwise, it does not. |
| 19 | + ValueStream<T> toValueStream({bool replayValue = false}) => |
| 20 | + ValueListenableStream<T>(this, replayValue); |
| 21 | +} |
| 22 | + |
| 23 | +/// A Single-Subscription Stream will emits data when [ValueListenable.value] changed. |
| 24 | +class ValueListenableStream<T> extends Stream<T> implements ValueStream<T> { |
| 25 | + final ValueListenable<T> _valueListenable; |
| 26 | + final bool _replayValue; |
| 27 | + Stream<T> _stream; |
| 28 | + |
| 29 | + /// Construct a [ValueListenableStream] from [ValueListenable]. |
| 30 | + ValueListenableStream(this._valueListenable, this._replayValue); |
| 31 | + |
| 32 | + @override |
| 33 | + bool get isBroadcast => false; |
| 34 | + |
| 35 | + @override |
| 36 | + ErrorAndStackTrace get errorAndStackTrace => null; |
| 37 | + |
| 38 | + @override |
| 39 | + bool get hasError => false; |
| 40 | + |
| 41 | + @override |
| 42 | + bool get hasValue => true; |
| 43 | + |
| 44 | + @override |
| 45 | + T get value => _valueListenable.value; |
| 46 | + |
| 47 | + @override |
| 48 | + StreamSubscription<T> listen( |
| 49 | + void Function(T) onData, { |
| 50 | + Function onError, |
| 51 | + void Function() onDone, |
| 52 | + bool cancelOnError, |
| 53 | + }) { |
| 54 | + if (_replayValue) { |
| 55 | + _stream ??= _valueListenable |
| 56 | + .toStream() |
| 57 | + .map((_) => _valueListenable.value) |
| 58 | + .shareBehavior(value); |
| 59 | + } else { |
| 60 | + _stream ??= |
| 61 | + _valueListenable.toStream().map((_) => _valueListenable.value); |
| 62 | + } |
| 63 | + |
| 64 | + return _stream.listen( |
| 65 | + onData, |
| 66 | + onError: onError, |
| 67 | + onDone: onDone, |
| 68 | + cancelOnError: cancelOnError, |
| 69 | + ); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +extension _ShareValueExtension<T> on Stream<T> { |
| 74 | + Stream<T> shareBehavior(T seeded) { |
| 75 | + final controllers = <MultiStreamController<T>>[]; |
| 76 | + StreamSubscription<T> subscription; |
| 77 | + |
| 78 | + var latestValue = seeded; |
| 79 | + var cancel = false; |
| 80 | + var done = false; |
| 81 | + |
| 82 | + final listenUpStream = () => listen( |
| 83 | + (event) { |
| 84 | + latestValue = event; |
| 85 | + controllers.forEach((c) => c.addSync(event)); |
| 86 | + }, |
| 87 | + onError: (e, StackTrace st) => |
| 88 | + controllers.forEach((c) => c.addErrorSync(e, st)), |
| 89 | + onDone: () { |
| 90 | + done = true; |
| 91 | + subscription = null; |
| 92 | + |
| 93 | + controllers.forEach((c) { |
| 94 | + c.onCancel = null; |
| 95 | + c.closeSync(); |
| 96 | + }); |
| 97 | + controllers.clear(); |
| 98 | + }, |
| 99 | + ); |
| 100 | + |
| 101 | + final onListen = (MultiStreamController<T> controller) { |
| 102 | + if (cancel) { |
| 103 | + return controller.closeSync(); |
| 104 | + } |
| 105 | + controller.addSync(latestValue); |
| 106 | + if (done) { |
| 107 | + return controller.closeSync(); |
| 108 | + } |
| 109 | + |
| 110 | + final wasEmpty = controllers.isEmpty; |
| 111 | + controllers.add(controller); |
| 112 | + if (wasEmpty) { |
| 113 | + subscription = listenUpStream(); |
| 114 | + } |
| 115 | + |
| 116 | + controller.onCancel = () { |
| 117 | + controllers.remove(controller); |
| 118 | + if (controllers.isEmpty) { |
| 119 | + subscription?.cancel(); |
| 120 | + subscription = null; |
| 121 | + cancel = true; |
| 122 | + } |
| 123 | + }; |
| 124 | + }; |
| 125 | + |
| 126 | + return Stream.multi(onListen, isBroadcast: true); |
| 127 | + } |
| 128 | +} |
0 commit comments