Skip to content

Commit ad8d67d

Browse files
committed
2.1.0
1 parent c27453c commit ad8d67d

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.1.0 - Aug 28, 2020
2+
3+
- State stream returned from `RxReduxStore` will not replay the latest state
4+
(Use `RxReduxStore.state` getter instead).
5+
16
## 2.0.0 - Aug 27, 2020
27

38
- Added `Logger` which allows logging current state, action and new state.

lib/src/store.dart

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class RxReduxStore<A, S> {
3131
final void Function(A) _dispatch;
3232

3333
final GetState<S> _getState;
34-
final Stream<S> Function() _stateStream;
34+
final Stream<S> _stateStream;
3535
final Stream<A> _actionStream;
3636

3737
final Future<void> Function() _dispose;
@@ -62,18 +62,22 @@ class RxReduxStore<A, S> {
6262
bool Function(S previous, S next) equals,
6363
RxReduxLogger logger,
6464
}) {
65-
final actionSubject = StreamController<A>.broadcast(sync: true);
65+
final actionSubject = StreamController<A>(sync: true);
6666
final actionOutputController = StreamController<A>.broadcast(sync: true);
6767

68-
final stateStream = actionSubject.stream.reduxStore<S>(
69-
initialStateSupplier: () => initialState,
70-
sideEffects: [
71-
...sideEffects,
72-
_onEachActionSideEffect(actionOutputController),
73-
],
74-
reducer: reducer,
75-
logger: logger,
76-
);
68+
final stateStream = actionSubject.stream
69+
.reduxStore<S>(
70+
initialStateSupplier: () => initialState,
71+
sideEffects: [
72+
...sideEffects,
73+
_onEachActionSideEffect(actionOutputController),
74+
],
75+
reducer: reducer,
76+
logger: logger,
77+
)
78+
.distinct(equals)
79+
.skip(1)
80+
.asBroadcastStream(onCancel: (subscription) => subscription.cancel());
7781

7882
var currentState = initialState;
7983
final subscription = stateStream.listen(
@@ -84,11 +88,7 @@ class RxReduxStore<A, S> {
8488
return RxReduxStore._(
8589
actionSubject.add,
8690
() => currentState,
87-
() => () async* {
88-
yield currentState;
89-
yield* stateStream;
90-
}()
91-
.distinct(equals),
91+
stateStream,
9292
actionOutputController.stream,
9393
() => Future.wait([
9494
actionSubject.close(),
@@ -115,7 +115,7 @@ class RxReduxStore<A, S> {
115115
/// return LoginWidget(state); // build widget based on state.
116116
/// },
117117
/// );
118-
Stream<S> get stateStream => _stateStream();
118+
Stream<S> get stateStream => _stateStream;
119119

120120
/// Get current state synchronously.
121121
/// This is useful for filling `initialData` when using `StreamBuilder` in Flutter.

test/store_test.dart

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ void main() {
4646
expect(store.state, 0);
4747
});
4848

49-
test('Get state stream that emits initial state', () {
49+
test('Get state stream that never emits', () {
5050
final store = RxReduxStore<int, int>(
5151
initialState: 0,
5252
sideEffects: [],
5353
reducer: (s, a) => s + a,
5454
);
5555

56-
expect(store.stateStream, emits(0));
56+
store.stateStream.listen((event) => expect(true, isFalse));
57+
Future<void>.delayed(const Duration(seconds: 1));
5758
});
5859

5960
test('Get state stream', () async {
@@ -67,7 +68,6 @@ void main() {
6768
store.stateStream,
6869
emitsInOrder(
6970
<String>[
70-
'0',
7171
'0+1',
7272
'0+1+2',
7373
'0+1+2+3',
@@ -82,7 +82,8 @@ void main() {
8282
store.dispatch(3);
8383

8484
await future;
85-
expect(store.stateStream, emits('0+1+2+3'));
85+
store.stateStream.listen((_) => expect(true, isFalse));
86+
await Future<void>.delayed(const Duration(seconds: 1));
8687
});
8788

8889
test('Get state stream with SideEffects', () async {
@@ -123,7 +124,6 @@ void main() {
123124
store.stateStream,
124125
emitsInOrder(
125126
<int>[
126-
0,
127127
1,
128128
3,
129129
6,
@@ -146,7 +146,6 @@ void main() {
146146
store.stateStream,
147147
emitsInOrder(
148148
<int>[
149-
6,
150149
7,
151150
9,
152151
12,
@@ -286,16 +285,10 @@ void main() {
286285

287286
await rxReduxStore.dispose();
288287

289-
rxReduxStore.stateStream.listen(
290-
expectAsync1(
291-
(v) => expect(v, '0'),
292-
count: 1,
293-
),
294-
onDone: expectAsync0(
295-
() {},
296-
count: 1,
297-
),
298-
);
288+
expect(rxReduxStore.state, '0');
289+
expect(() => rxReduxStore.dispatch(0), throwsStateError);
290+
expect(rxReduxStore.actionStream, emitsDone);
291+
expect(rxReduxStore.stateStream, emitsDone);
299292
});
300293

301294
test('Error handler called when reducer throws', () async {

0 commit comments

Comments
 (0)