Committed by
GitHub
Merge pull request #1950 from gslender/Rx.trigger-fires-twice-fix
Fix to rx.trigger so that it checks for previous send to stream by set value
Showing
2 changed files
with
5 additions
and
3 deletions
@@ -67,6 +67,7 @@ mixin RxObjectMixin<T> on NotifyManager<T> { | @@ -67,6 +67,7 @@ mixin RxObjectMixin<T> on NotifyManager<T> { | ||
67 | } | 67 | } |
68 | 68 | ||
69 | bool firstRebuild = true; | 69 | bool firstRebuild = true; |
70 | + bool sentToStream = false; | ||
70 | 71 | ||
71 | /// Same as `toString()` but using a getter. | 72 | /// Same as `toString()` but using a getter. |
72 | String get string => value.toString(); | 73 | String get string => value.toString(); |
@@ -96,10 +97,11 @@ mixin RxObjectMixin<T> on NotifyManager<T> { | @@ -96,10 +97,11 @@ mixin RxObjectMixin<T> on NotifyManager<T> { | ||
96 | /// Widget, only if it's different from the previous value. | 97 | /// Widget, only if it's different from the previous value. |
97 | set value(T val) { | 98 | set value(T val) { |
98 | if (subject.isClosed) return; | 99 | if (subject.isClosed) return; |
100 | + sentToStream = false; | ||
99 | if (_value == val && !firstRebuild) return; | 101 | if (_value == val && !firstRebuild) return; |
100 | firstRebuild = false; | 102 | firstRebuild = false; |
101 | _value = val; | 103 | _value = val; |
102 | - | 104 | + sentToStream = true; |
103 | subject.add(_value); | 105 | subject.add(_value); |
104 | } | 106 | } |
105 | 107 | ||
@@ -254,7 +256,7 @@ abstract class _RxImpl<T> extends RxNotifier<T> with RxObjectMixin<T> { | @@ -254,7 +256,7 @@ abstract class _RxImpl<T> extends RxNotifier<T> with RxObjectMixin<T> { | ||
254 | value = v; | 256 | value = v; |
255 | // If it's not the first rebuild, the listeners have been called already | 257 | // If it's not the first rebuild, the listeners have been called already |
256 | // So we won't call them again. | 258 | // So we won't call them again. |
257 | - if (!firstRebuild) { | 259 | + if (!firstRebuild && !sentToStream) { |
258 | subject.add(v); | 260 | subject.add(v); |
259 | } | 261 | } |
260 | } | 262 | } |
@@ -125,7 +125,7 @@ void main() { | @@ -125,7 +125,7 @@ void main() { | ||
125 | reactiveInteger.trigger(3); | 125 | reactiveInteger.trigger(3); |
126 | // then repeat twice | 126 | // then repeat twice |
127 | reactiveInteger.trigger(3); | 127 | reactiveInteger.trigger(3); |
128 | - reactiveInteger.trigger(3); | 128 | + reactiveInteger.trigger(1); |
129 | 129 | ||
130 | await Future.delayed(Duration(milliseconds: 100)); | 130 | await Future.delayed(Duration(milliseconds: 100)); |
131 | expect(3, timesCalled); | 131 | expect(3, timesCalled); |
-
Please register or login to post a comment