Jonatas

clean states class

@@ -11,7 +11,10 @@ part 'rx_num.dart'; @@ -11,7 +11,10 @@ part 'rx_num.dart';
11 RxInterface getObs; 11 RxInterface getObs;
12 12
13 /// Base Rx class that manages all the stream logic for any Type. 13 /// Base Rx class that manages all the stream logic for any Type.
14 -class _RxImpl<T> implements RxInterface<T> { 14 +abstract class _RxImpl<T> implements RxInterface<T> {
  15 + _RxImpl(T initial) {
  16 + _value = initial;
  17 + }
15 StreamController<T> subject = StreamController<T>.broadcast(); 18 StreamController<T> subject = StreamController<T>.broadcast();
16 final _subscriptions = HashMap<Stream<T>, StreamSubscription>(); 19 final _subscriptions = HashMap<Stream<T>, StreamSubscription>();
17 20
@@ -183,9 +186,7 @@ class _RxImpl<T> implements RxInterface<T> { @@ -183,9 +186,7 @@ class _RxImpl<T> implements RxInterface<T> {
183 186
184 /// Rx class for `bool` Type. 187 /// Rx class for `bool` Type.
185 class RxBool extends _RxImpl<bool> { 188 class RxBool extends _RxImpl<bool> {
186 - RxBool([bool initial]) {  
187 - _value = initial;  
188 - } 189 + RxBool([bool initial]) : super(initial);
189 190
190 bool operator &(bool other) => other && value; 191 bool operator &(bool other) => other && value;
191 192
@@ -210,9 +211,7 @@ class RxBool extends _RxImpl<bool> { @@ -210,9 +211,7 @@ class RxBool extends _RxImpl<bool> {
210 211
211 /// Rx class for `String` Type. 212 /// Rx class for `String` Type.
212 class RxString extends _RxImpl<String> { 213 class RxString extends _RxImpl<String> {
213 - RxString([String initial]) {  
214 - _value = initial;  
215 - } 214 + RxString([String initial]) : super(initial);
216 215
217 String operator +(String val) => _value + val; 216 String operator +(String val) => _value + val;
218 } 217 }
@@ -222,9 +221,7 @@ class RxString extends _RxImpl<String> { @@ -222,9 +221,7 @@ class RxString extends _RxImpl<String> {
222 /// For example, any custom "Model" class, like User().obs will use `Rx` as 221 /// For example, any custom "Model" class, like User().obs will use `Rx` as
223 /// wrapper. 222 /// wrapper.
224 class Rx<T> extends _RxImpl<T> { 223 class Rx<T> extends _RxImpl<T> {
225 - Rx([T initial]) {  
226 - _value = initial;  
227 - } 224 + Rx([T initial]) : super(initial);
228 225
229 // TODO: Look for a way to throw the Exception with proper details when the 226 // TODO: Look for a way to throw the Exception with proper details when the
230 // value [T] doesn't implement toJson(). 227 // value [T] doesn't implement toJson().
@@ -233,37 +230,26 @@ class Rx<T> extends _RxImpl<T> { @@ -233,37 +230,26 @@ class Rx<T> extends _RxImpl<T> {
233 } 230 }
234 231
235 /// It's Experimental class, the Api can be change 232 /// 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 - } 233 +abstract class RxState<T> extends _RxImpl<T> {
  234 + RxState(T initial) : super(initial);
240 235
241 - T _value;  
242 -  
243 - StreamController<T> subject = StreamController<T>.broadcast();  
244 - final _subscriptions = HashMap<Stream<T>, StreamSubscription>(); 236 + @protected
  237 + void refresh() {
  238 + subject.add(_value);
  239 + }
245 240
246 - bool get canUpdate => _subscriptions.isNotEmpty; 241 + @protected
  242 + void update(void fn(T val)) {
  243 + fn(_value);
  244 + subject.add(_value);
  245 + }
247 246
248 @protected 247 @protected
249 T call([T v]) { 248 T call([T v]) {
250 - if (v != null) {  
251 - value = v;  
252 - } 249 + if (v != null) value = v;
253 return value; 250 return value;
254 } 251 }
255 252
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 253 @protected
268 set value(T val) { 254 set value(T val) {
269 if (_value == val && !firstRebuild) return; 255 if (_value == val && !firstRebuild) return;
@@ -272,24 +258,6 @@ abstract class RxState<T> extends RxInterface<T> { @@ -272,24 +258,6 @@ abstract class RxState<T> extends RxInterface<T> {
272 subject.add(_value); 258 subject.add(_value);
273 } 259 }
274 260
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 261 @protected
294 void change(T newState) { 262 void change(T newState) {
295 if (newState != _value) { 263 if (newState != _value) {
1 part of 'rx_impl.dart'; 1 part of 'rx_impl.dart';
2 2
3 /// Base Rx class for all num Rx's. 3 /// Base Rx class for all num Rx's.
4 -class _BaseRxNum<T extends num> extends _RxImpl<T> { 4 +abstract class _BaseRxNum<T extends num> extends _RxImpl<T> {
  5 + _BaseRxNum(T initial) : super(initial);
  6 +
5 /// Addition operator. */ 7 /// Addition operator. */
6 8
7 /// Multiplication operator. 9 /// Multiplication operator.
@@ -267,6 +269,8 @@ class _BaseRxNum<T extends num> extends _RxImpl<T> { @@ -267,6 +269,8 @@ class _BaseRxNum<T extends num> extends _RxImpl<T> {
267 } 269 }
268 270
269 class RxNum extends _BaseRxNum<num> { 271 class RxNum extends _BaseRxNum<num> {
  272 + RxNum(num initial) : super(initial);
  273 +
270 num operator +(num other) { 274 num operator +(num other) {
271 value += other; 275 value += other;
272 return value; 276 return value;
@@ -280,9 +284,7 @@ class RxNum extends _BaseRxNum<num> { @@ -280,9 +284,7 @@ class RxNum extends _BaseRxNum<num> {
280 } 284 }
281 285
282 class RxDouble extends _BaseRxNum<double> { 286 class RxDouble extends _BaseRxNum<double> {
283 - RxDouble([double initial]) {  
284 - value = initial;  
285 - } 287 + RxDouble([double initial]) : super(initial);
286 288
287 /// Addition operator. 289 /// Addition operator.
288 RxDouble operator +(num other) { 290 RxDouble operator +(num other) {
@@ -392,9 +394,7 @@ class RxDouble extends _BaseRxNum<double> { @@ -392,9 +394,7 @@ class RxDouble extends _BaseRxNum<double> {
392 } 394 }
393 395
394 class RxInt extends _BaseRxNum<int> { 396 class RxInt extends _BaseRxNum<int> {
395 - RxInt([int initial]) {  
396 - value = initial;  
397 - } 397 + RxInt([int initial]) : super(initial);
398 398
399 /// Addition operator. 399 /// Addition operator.
400 RxInt operator +(int other) { 400 RxInt operator +(int other) {