Skip to content
This repository was archived by the owner on Jul 9, 2020. It is now read-only.

Commit de7e96d

Browse files
committed
fix(cubit): async state emission (#57)
1 parent d02b88c commit de7e96d

File tree

6 files changed

+32
-6
lines changed

6 files changed

+32
-6
lines changed

packages/cubit/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.1.1
2+
3+
- fix: offload state emission to next event loop iteration
4+
15
# 0.1.0
26

37
- docs: add `onTransition` documentation to README

packages/cubit/lib/src/cubit_stream.dart

+9-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ abstract class CubitStream<State> extends Stream<State> {
3737
/// {@endtemplate}
3838
@protected
3939
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+
});
4346
}
4447

4548
/// Adds a subscription to the `Stream<State>`.
@@ -72,6 +75,9 @@ abstract class CubitStream<State> extends Stream<State> {
7275
/// which resolves when it is done or an error occurred.
7376
@mustCallSuper
7477
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);
7581
await _controller.close();
7682
await _controller.stream.drain<State>();
7783
}

packages/cubit/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/felangel/cubit
44
issue_tracker: https://github.com/felangel/cubit/issues
55
homepage: https://github.com/felangel/cubit
66

7-
version: 0.1.0
7+
version: 0.1.1
88

99
environment:
1010
sdk: ">=2.7.0 <3.0.0"

packages/cubit/test/cubit_test.dart

+10-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ void main() {
5252
() async {
5353
final transitions = <Transition<int>>[];
5454
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);
5757
await cubit.close();
5858
expect(
5959
transitions,
@@ -125,6 +125,14 @@ void main() {
125125
expect(states, [equals(0)]);
126126
});
127127

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+
128136
test('can call listen multiple times', () async {
129137
final states = <int>[];
130138
final cubit = CounterCubit()..listen(states.add)..listen(states.add);
+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export 'counter_cubit.dart';
2+
export 'fake_async_cubit.dart';
23
export 'seeded_cubit.dart';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
}

0 commit comments

Comments
 (0)