Showing
2 changed files
with
101 additions
and
1 deletions
1 | import 'dart:async'; | 1 | import 'dart:async'; |
2 | import 'dart:collection'; | 2 | import 'dart:collection'; |
3 | +import 'package:meta/meta.dart'; | ||
4 | + | ||
3 | import '../rx_core/rx_interface.dart'; | 5 | import '../rx_core/rx_interface.dart'; |
4 | part 'rx_num.dart'; | 6 | part 'rx_num.dart'; |
5 | 7 | ||
@@ -230,6 +232,72 @@ class Rx<T> extends _RxImpl<T> { | @@ -230,6 +232,72 @@ class Rx<T> extends _RxImpl<T> { | ||
230 | dynamic toJson() => (value as dynamic)?.toJson(); | 232 | dynamic toJson() => (value as dynamic)?.toJson(); |
231 | } | 233 | } |
232 | 234 | ||
235 | +/// It's Experimental class, the Api can be change | ||
236 | +abstract class RxState<T> extends RxInterface<T> { | ||
237 | + RxState(T initial) { | ||
238 | + _value = initial; | ||
239 | + } | ||
240 | + | ||
241 | + T _value; | ||
242 | + | ||
243 | + StreamController<T> subject = StreamController<T>.broadcast(); | ||
244 | + final _subscriptions = HashMap<Stream<T>, StreamSubscription>(); | ||
245 | + | ||
246 | + bool get canUpdate => _subscriptions.isNotEmpty; | ||
247 | + | ||
248 | + @protected | ||
249 | + T call([T v]) { | ||
250 | + if (v != null) { | ||
251 | + value = v; | ||
252 | + } | ||
253 | + return value; | ||
254 | + } | ||
255 | + | ||
256 | + bool firstRebuild = true; | ||
257 | + | ||
258 | + void addListener(Stream<T> rxGetx) { | ||
259 | + if (_subscriptions.containsKey(rxGetx)) { | ||
260 | + return; | ||
261 | + } | ||
262 | + _subscriptions[rxGetx] = rxGetx.listen((data) { | ||
263 | + subject.add(data); | ||
264 | + }); | ||
265 | + } | ||
266 | + | ||
267 | + @protected | ||
268 | + set value(T val) { | ||
269 | + if (_value == val && !firstRebuild) return; | ||
270 | + firstRebuild = false; | ||
271 | + _value = val; | ||
272 | + subject.add(_value); | ||
273 | + } | ||
274 | + | ||
275 | + /// Returns the current [value] | ||
276 | + T get value { | ||
277 | + if (getObs != null) { | ||
278 | + getObs.addListener(subject.stream); | ||
279 | + } | ||
280 | + return _value; | ||
281 | + } | ||
282 | + | ||
283 | + Stream<T> get stream => subject.stream; | ||
284 | + | ||
285 | + StreamSubscription<T> listen(void Function(T) onData, | ||
286 | + {Function onError, void Function() onDone, bool cancelOnError}) => | ||
287 | + stream.listen(onData, onError: onError, onDone: onDone); | ||
288 | + | ||
289 | + void bindStream(Stream<T> stream) { | ||
290 | + _subscriptions[stream] = stream.listen((va) => value = va); | ||
291 | + } | ||
292 | + | ||
293 | + @protected | ||
294 | + void change(T newState) { | ||
295 | + if (newState != _value) { | ||
296 | + value = newState; | ||
297 | + } | ||
298 | + } | ||
299 | +} | ||
300 | + | ||
233 | extension StringExtension on String { | 301 | extension StringExtension on String { |
234 | /// Returns a `RxString` with [this] `String` as initial value. | 302 | /// Returns a `RxString` with [this] `String` as initial value. |
235 | RxString get obs => RxString(this); | 303 | RxString get obs => RxString(this); |
1 | import 'dart:collection'; | 1 | import 'dart:collection'; |
2 | - | ||
3 | import 'package:flutter/material.dart'; | 2 | import 'package:flutter/material.dart'; |
4 | import 'package:get_core/get_core.dart'; | 3 | import 'package:get_core/get_core.dart'; |
5 | import 'package:get_instance/get_instance.dart'; | 4 | import 'package:get_instance/get_instance.dart'; |
@@ -271,3 +270,36 @@ class Value<T> extends GetxController { | @@ -271,3 +270,36 @@ class Value<T> extends GetxController { | ||
271 | update(); | 270 | update(); |
272 | } | 271 | } |
273 | } | 272 | } |
273 | + | ||
274 | +/// It's Experimental class, the Api can be change | ||
275 | +abstract class GetState<T> extends GetxController { | ||
276 | + GetState(T initialValue) { | ||
277 | + _state = initialValue; | ||
278 | + } | ||
279 | + | ||
280 | + // StreamController<T> _subject; | ||
281 | + | ||
282 | + // @override | ||
283 | + // void onClose() { | ||
284 | + // _subject?.close(); | ||
285 | + // } | ||
286 | + | ||
287 | + // Stream<T> get stream { | ||
288 | + // if (_subject == null) { | ||
289 | + // _subject = StreamController<T>.broadcast(); | ||
290 | + // } | ||
291 | + // return _subject.stream; | ||
292 | + // } | ||
293 | + | ||
294 | + T _state; | ||
295 | + | ||
296 | + T get state => _state; | ||
297 | + | ||
298 | + @protected | ||
299 | + void change(T newState) { | ||
300 | + if (newState != _state) { | ||
301 | + _state = newState; | ||
302 | + update(); | ||
303 | + } | ||
304 | + } | ||
305 | +} |
-
Please register or login to post a comment