Skip to content

Commit 50ba72b

Browse files
authored
prepare 2.2.0 (#11)
* bum deps * fix CI * update * update selector.dart * update README.md * imports
1 parent ba6942c commit 50ba72b

File tree

10 files changed

+86
-52
lines changed

10 files changed

+86
-52
lines changed

.github/workflows/dart.yml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,33 @@ jobs:
1212
runs-on: ubuntu-latest
1313

1414
container:
15-
image: google/dart:2.12-beta
15+
image: google/dart:2.12
1616

1717
steps:
18-
- uses: actions/checkout@v2
18+
- uses: actions/checkout@v2
1919

20-
- name: Install dependencies
21-
run: pub get
20+
- name: Install dependencies
21+
run: pub get
2222

23-
- name: Analyze
24-
run: dartanalyzer --fatal-infos --fatal-warnings ./lib ./test
23+
- name: Analyze
24+
run: dart analyze --fatal-infos --fatal-warnings
2525

26-
- name: Format code
27-
run: dartfmt -n ./lib ./test --set-exit-if-changed
26+
- name: Format code
27+
run: dartfmt -n ./lib ./test --set-exit-if-changed
2828

29-
- name: Active coverage
30-
run: pub global activate coverage
29+
- name: Active coverage
30+
run: pub global activate coverage
3131

32-
- name: Run tests
33-
run: pub run test/rx_redux_test.dart
32+
- name: Run tests
33+
run: pub run test test/rx_redux_test.dart
3434

35-
- name: Start VM
36-
run: dart --disable-service-auth-codes --enable-vm-service=8111 --pause-isolates-on-exit test/rx_redux_test.dart &
35+
- name: Start Observatory
36+
run: dart --disable-service-auth-codes --enable-vm-service=8111 --pause-isolates-on-exit test/rx_redux_test.dart &
3737

38-
- name: Collect coverage
39-
run: nohup pub global run coverage:collect_coverage --port=8111 --out=coverage.json --wait-paused --resume-isolates
38+
- name: Collect coverage
39+
run: nohup pub global run coverage:collect_coverage --port=8111 --out=coverage.json --wait-paused --resume-isolates
4040

41-
- name: Format coverage
42-
run: pub global run coverage:format_coverage --lcov --in=coverage.json --out=lcov.info --packages=.packages --report-on=lib
41+
- name: Format coverage
42+
run: pub global run coverage:format_coverage --lcov --in=coverage.json --out=lcov.info --packages=.packages --report-on=lib
4343

44-
- uses: codecov/codecov-action@v1
44+
- uses: codecov/codecov-action@v1

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
language: dart
22
dart:
3-
- dev
3+
- stable
44
- beta
55
script:
6-
- dartanalyzer --fatal-infos --fatal-warnings ./lib ./test
6+
- dart analyze --fatal-infos --fatal-warnings
77
- dartfmt -n ./lib --set-exit-if-changed
88
- pub get
99
- pub global activate coverage

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,17 @@ Whenever a `SideEffect` needs to know the current State it can use `GetState` to
8080

8181
### Selector
8282

83-
TODO
83+
Inspirited by [NgRx memoized selector](https://ngrx.io/guide/store/selectors)
84+
- Selectors can compute derived data, allowing Redux to store the minimal possible state.
85+
86+
- Selectors are efficient. A selector is not recomputed unless one of its arguments changes.
87+
88+
- When using the `select`, `select2` to `select9`, `selectMany` functions,
89+
keeps track of the latest arguments in which your selector function was invoked.
90+
Because selectors are pure functions, the last result can be returned
91+
when the arguments match without re-invoking your selector function.
92+
This can provide performance benefits, particularly with selectors that perform expensive computation.
93+
This practice is known as memoization.
8494

8595
## Usage
8696

@@ -305,6 +315,12 @@ Also `SideEffects` can be invoked by `Actions` from other `SideEffects`.
305315

306316
**For a complete example check [the sample application incl. README](example/README.md)**
307317

318+
## Examples
319+
320+
| [Pagination list (load more) (endless scrolling)](https://github.com/hoc081098/load_more_flutter_BLoC_pattern_RxDart_and_RxRedux/tree/master/lib/pages/rx_redux) | [Flutter github search using rx_redux](https://github.com/hoc081098/flutter_github_search_rx_redux) |
321+
| ------------- | ------------- |
322+
| <img src="https://github.com/hoc081098/hoc081098.github.io/blob/master/load_more/rx_redux_screen.gif?raw=true" height="480"> | <img src="https://github.com/hoc081098/hoc081098.github.io/blob/master/flutter_github_search_rx_redux/demo.gif?raw=true" height="480"> |
323+
308324
## FAQ
309325

310326
### I get a `StackOverflowError`

analysis_options.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
include: package:pedantic/analysis_options.1.9.0.yaml
1+
include: package:pedantic/analysis_options.1.11.0.yaml
22

33
analyzer:
44
strong-mode:
@@ -8,3 +8,4 @@ linter:
88
rules:
99
- prefer_final_locals
1010
- public_member_api_docs
11+
- prefer_relative_imports

example/selector.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'package:built_collection/built_collection.dart';
2+
import 'package:disposebag/disposebag.dart';
23
import 'package:distinct_value_connectable_stream/distinct_value_connectable_stream.dart';
4+
import 'package:pedantic/pedantic.dart';
35
import 'package:rx_redux/rx_redux.dart';
46

57
//
@@ -138,6 +140,7 @@ State reducer(State state, Action action) {
138140
}
139141

140142
void main() async {
143+
final bag = DisposeBag();
141144
final store = RxReduxStore<Action, State>(
142145
initialState: State(null, <Book>[].build(), null),
143146
sideEffects: [],
@@ -148,18 +151,23 @@ void main() async {
148151
(s) => s.selectedUser?.id,
149152
(s) => s.allBooks,
150153
(String? userId, BuiltList<Book> books) {
151-
print(
152-
'<> Call projector with userId=${userId} and books=${books.length}');
154+
print('<> Call projector with userId=$userId and books=${books.length}');
153155

154156
return userId != null && books.isNotEmpty
155157
? books.where((b) => b.userId == userId).toBuiltList()
156158
: books;
157159
},
158-
);
160+
).asBroadcastDistinctValueStream();
159161

160162
// logging state.
161163
print('~> ${visibleBooks$.value}');
162-
visibleBooks$.listen((s) => print('~> $s'));
164+
unawaited(
165+
visibleBooks$.listen((s) => print('1 ~> $s')).disposedBy(bag),
166+
);
167+
Future<void>.delayed(
168+
const Duration(milliseconds: 500),
169+
() => visibleBooks$.listen((s) => print('2 ~> $s')).disposedBy(bag),
170+
);
163171

164172
// dispatch actions.
165173
[
@@ -190,6 +198,7 @@ void main() async {
190198
print('---------------------------------------------------');
191199

192200
// end by disposing store.
201+
await bag.dispose();
193202
await store.dispose();
194203
}
195204

lib/src/selectors.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import 'dart:async';
22

33
import 'package:distinct_value_connectable_stream/distinct_value_connectable_stream.dart';
44

5-
import '../rx_redux.dart';
5+
import 'store.dart';
66

77
/// Inspirited by [NgRx memoized selector](https://ngrx.io/guide/store/selectors)
88
/// - Selectors can compute derived data, allowing Redux to store the minimal possible state.
99
/// - Selectors are efficient. A selector is not recomputed unless one of its arguments changes.
1010
/// - When using the [select], [select2] to [select9], [selectMany] functions,
1111
/// keeps track of the latest arguments in which your selector function was invoked.
1212
/// Because selectors are pure functions, the last result can be returned
13-
/// when the arguments match without reinvoking your selector function.
13+
/// when the arguments match without re-invoking your selector function.
1414
/// This can provide performance benefits, particularly with selectors that perform expensive computation.
1515
/// This practice is known as memoization.
1616
typedef Selector<State, V> = V Function(State state);
@@ -23,7 +23,7 @@ typedef Selector<State, V> = V Function(State state);
2323
/// - When using the [select], [select2] to [select9], [selectMany] functions,
2424
/// keeps track of the latest arguments in which your selector function was invoked.
2525
/// Because selectors are pure functions, the last result can be returned
26-
/// when the arguments match without reinvoking your selector function.
26+
/// when the arguments match without re-invoking your selector function.
2727
/// This can provide performance benefits, particularly with selectors that perform expensive computation.
2828
/// This practice is known as memoization.
2929
extension SelectorsExtension<Action, State> on RxReduxStore<Action, State> {

lib/src/store.dart

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'logger.dart';
77
import 'reducer.dart';
88
import 'redux_store_stream_transformer.dart';
99
import 'side_effect.dart';
10+
import 'utils.dart';
1011

1112
/// Determine equality.
1213
typedef Equals<T> = bool Function(T previous, T next);
@@ -47,7 +48,7 @@ class RxReduxStore<A, S> {
4748
final DistinctValueStream<S> _stateStream;
4849
final Stream<A> _actionStream;
4950

50-
final DisposeBag _bag;
51+
final DisposeBag Function() _bag;
5152

5253
const RxReduxStore._(
5354
this._dispatch,
@@ -91,14 +92,21 @@ class RxReduxStore<A, S> {
9192
.handleErrorIfNeeded(errorHandler)
9293
.publishValueDistinct(initialState, equals: equals);
9394

94-
final bag = DisposeBag(<Object>[stateStream.connect(), actionController]);
95+
late final RxReduxStore<A, S> store;
96+
late final bag = DisposeBag(
97+
const <Object>[], '( RxReduxStore<$A, $S> ~ ${shortHash(store)} )');
9598

96-
return RxReduxStore._(
99+
store = RxReduxStore._(
97100
actionController.add,
98101
stateStream,
99102
actionOutputController.stream,
100-
bag,
103+
() => bag,
101104
);
105+
106+
stateStream.connect().disposedBy(bag);
107+
actionController.disposedBy(bag);
108+
109+
return store;
102110
}
103111

104112
/// Get stream of states.
@@ -172,7 +180,7 @@ class RxReduxStore<A, S> {
172180
/// Stream<LoadNextPageAction> loadNextPageActionStream;
173181
/// store.dispatchMany(loadNextPageActionStream);
174182
void dispatchMany(Stream<A> actionStream) =>
175-
_bag.add(actionStream.listen(_dispatch));
183+
_bag().add(actionStream.listen(_dispatch));
176184

177185
/// Dispose all resources.
178186
/// This method is typically called in `dispose` method of Flutter `State` object.
@@ -186,7 +194,7 @@ class RxReduxStore<A, S> {
186194
/// super.dispose();
187195
/// }
188196
/// }
189-
Future<void> dispose() => _bag.dispose();
197+
Future<void> dispose() => _bag().dispose();
190198
}
191199

192200
/// Get current state synchronously.

lib/src/utils.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ extension MapIndexedIterableExtensison<T> on Iterable<T> {
88
}
99
}
1010
}
11+
12+
/// Returns a 5 character long hexadecimal string generated from
13+
/// [Object.hashCode]'s 20 least-significant bits.
14+
String shortHash(Object? object) =>
15+
object.hashCode.toUnsigned(20).toRadixString(16).padLeft(5, '0');

lib/src/wrapper_action.dart

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@ abstract class ActionType {
1111

1212
static final _sideEffects = <int, _SideEffect>{};
1313

14-
factory ActionType._sideEffect(int index) {
15-
final sideEffect = _sideEffects[index];
16-
if (sideEffect != null) {
17-
return sideEffect;
18-
}
19-
return _sideEffects[index] = _SideEffect(index);
20-
}
14+
factory ActionType._sideEffect(int index) =>
15+
_sideEffects.putIfAbsent(index, () => _SideEffect(index));
2116

2217
@override
2318
String toString() {

pubspec.yaml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
name: rx_redux
22
description: Redux implementation based on Dart Stream, with the power of RxDart. Reactive redux store for Dart & Flutter.
3-
version: 2.2.0-nullsafety.2
3+
version: 2.2.0
44
author: Petrus Nguyen Thai Hoc <[email protected]>
55
homepage: https://github.com/hoc081098/rx_redux.git
66
repository: https://github.com/hoc081098/rx_redux.git
77
issue_tracker: https://github.com/hoc081098/rx_redux/issues
88

99
environment:
10-
sdk: '>=2.12.0-0 <3.0.0'
10+
sdk: '>=2.12.0 <3.0.0'
1111

1212
dependencies:
1313
meta: ^1.3.0
14-
distinct_value_connectable_stream: ^1.2.0-nullsafety.5
15-
disposebag: ^1.5.0-nullsafety.0
14+
distinct_value_connectable_stream: ^1.2.0
15+
disposebag: ^1.5.0
1616

1717
dev_dependencies:
18-
pedantic: ^1.10.0-nullsafety.3
19-
test: ^1.16.0-nullsafety.12
20-
rxdart: ^0.26.0-nullsafety.0
21-
built_collection: ^5.0.0-nullsafety.0
22-
tuple: ^2.0.0-nullsafety.0
18+
pedantic: ^1.11.0
19+
test: ^1.17.3
20+
rxdart: ^0.26.0
21+
built_collection: ^5.0.0
22+
tuple: ^2.0.0

0 commit comments

Comments
 (0)