This repository was archived by the owner on Jul 9, 2020. It is now read-only.
File tree 6 files changed +32
-6
lines changed
6 files changed +32
-6
lines changed Original file line number Diff line number Diff line change
1
+ # 0.1.1
2
+
3
+ - fix: offload state emission to next event loop iteration
4
+
1
5
# 0.1.0
2
6
3
7
- docs: add ` onTransition ` documentation to README
Original file line number Diff line number Diff line change @@ -37,9 +37,12 @@ abstract class CubitStream<State> extends Stream<State> {
37
37
/// {@endtemplate}
38
38
@protected
39
39
void emit (State state) {
40
- if (state == _state || _controller.isClosed) return ;
41
- _state = state;
42
- _controller.add (_state);
40
+ // Wait for next event-loop iteration before adding new state
41
+ Future <void >.delayed (Duration .zero, () {
42
+ if (state == _state || _controller.isClosed) return ;
43
+ _state = state;
44
+ _controller.add (_state);
45
+ });
43
46
}
44
47
45
48
/// Adds a subscription to the `Stream<State>` .
@@ -72,6 +75,9 @@ abstract class CubitStream<State> extends Stream<State> {
72
75
/// which resolves when it is done or an error occurred.
73
76
@mustCallSuper
74
77
Future <void > close () async {
78
+ // Wait for next event-loop iteration
79
+ // to allow propagation of newly emitted states.
80
+ await Future <void >.delayed (Duration .zero);
75
81
await _controller.close ();
76
82
await _controller.stream.drain <State >();
77
83
}
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ repository: https://github.com/felangel/cubit
4
4
issue_tracker : https://github.com/felangel/cubit/issues
5
5
homepage : https://github.com/felangel/cubit
6
6
7
- version : 0.1.0
7
+ version : 0.1.1
8
8
9
9
environment :
10
10
sdk : " >=2.7.0 <3.0.0"
Original file line number Diff line number Diff line change @@ -52,8 +52,8 @@ void main() {
52
52
() async {
53
53
final transitions = < Transition <int >> [];
54
54
final cubit = CounterCubit (onTransitionCallback: transitions.add);
55
- await Future <void >.delayed (Duration .zero);
56
- cubit.. increment (). .increment ( );
55
+ await Future <void >.delayed (Duration .zero, cubit.increment );
56
+ await Future < void >. delayed ( Duration .zero, cubit .increment);
57
57
await cubit.close ();
58
58
expect (
59
59
transitions,
@@ -125,6 +125,14 @@ void main() {
125
125
expect (states, [equals (0 )]);
126
126
});
127
127
128
+ test ('receives fake async states immediately upon subscribing' , () async {
129
+ final states = < int > [];
130
+ final cubit = FakeAsyncCounterCubit ()..skip (1 ).listen (states.add);
131
+ await cubit.increment ();
132
+ await cubit.close ();
133
+ expect (states, [equals (1 )]);
134
+ });
135
+
128
136
test ('can call listen multiple times' , () async {
129
137
final states = < int > [];
130
138
final cubit = CounterCubit ()..listen (states.add)..listen (states.add);
Original file line number Diff line number Diff line change 1
1
export 'counter_cubit.dart' ;
2
+ export 'fake_async_cubit.dart' ;
2
3
export 'seeded_cubit.dart' ;
Original file line number Diff line number Diff line change
1
+ import 'package:cubit/cubit.dart' ;
2
+
3
+ class FakeAsyncCounterCubit extends Cubit <int > {
4
+ FakeAsyncCounterCubit () : super (0 );
5
+
6
+ Future <void > increment () async => emit (state + 1 );
7
+ }
You can’t perform that action at this time.
0 commit comments