Skip to content

Commit efd759c

Browse files
authored
add FlatMapPolicy & release 1.2.1 - Jun 17, 2021 (#5)
* add * docs * change dispose to a func * dart format * docs and bump version * docs and bump version * LICENSE
1 parent 1f298c7 commit efd759c

File tree

7 files changed

+84
-29
lines changed

7 files changed

+84
-29
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
## 1.2.1 - Jun 17, 2021
2+
3+
* Change `dispose` from a field to a function.
4+
* Add `FlatMapPolicy` allow changing _flatMap behavior_ of `loaderFunction` and `refresherFunction`.
5+
```dart
6+
LoaderBloc(
7+
...,
8+
loaderFlatMapPolicy: FlatMapPolicy.concat, // asyncExpand
9+
refreshFlatMapPolicy: FlatMapPolicy.latest, // switchMap
10+
);
11+
```
12+
113
## 1.2.0 - May 10, 2021
214
315
* Update `rxdart` to `0.27.0`.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Petrus Nguyễn Thái Học
3+
Copyright (c) 2020-2021 Petrus Nguyễn Thái Học
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ dependencies:
2929
<img src="https://github.com/hoc081098/hoc081098.github.io/raw/master/stream_loader/untitled.gif" height="480"/>
3030
</p>
3131
32-
#### Model and api
32+
#### 1. Model and api
3333
```dart
3434
abstract class Comment implements Built<Comment, CommentBuilder> { ... }
3535

@@ -40,7 +40,7 @@ class Api {
4040
final api = Api();
4141
```
4242

43-
#### Create LoaderWidget load comments from api
43+
#### 2. Create LoaderWidget load comments from api
4444
```dart
4545
import 'package:stream_loader/stream_loader.dart';
4646
@@ -74,7 +74,7 @@ LoaderWidget<BuiltList<Comment>>(
7474
);
7575
```
7676

77-
#### Create LoaderWidget load comment detail from api
77+
#### 3. Create LoaderWidget load comment detail from api
7878
```dart
7979
import 'package:stream_loader/stream_loader.dart';
8080
@@ -153,7 +153,21 @@ class _CommentsState extends State<Comments> {
153153
}
154154
```
155155

156+
### Change _flatMap behavior_ of `loaderFunction` and `refresherFunction`.
157+
158+
- Default behavior of `loaderFunction` is `FlatMapPolicy.latest` (uses `switchMap`).
159+
- Default behavior of `refreshFlatMapPolicy` is `FlatMapPolicy.first`, (uses `exhaustMap`).
160+
- To change them, passing your value to `LoaderBloc` constructor
161+
162+
```dart
163+
LoaderBloc(
164+
...,
165+
loaderFlatMapPolicy: FlatMapPolicy.concat, // asyncExpand
166+
refreshFlatMapPolicy: FlatMapPolicy.latest, // switchMap
167+
);
168+
```
169+
156170
## License
157171
MIT License
158172

159-
Copyright (c) 2020 Petrus Nguyễn Thái Học
173+
Copyright (c) 2020-2021 Petrus Nguyễn Thái Học

example/pubspec.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ packages:
2828
name: async
2929
url: "https://pub.dartlang.org"
3030
source: hosted
31-
version: "2.5.0"
31+
version: "2.6.1"
3232
boolean_selector:
3333
dependency: transitive
3434
description:
@@ -421,7 +421,7 @@ packages:
421421
name: source_span
422422
url: "https://pub.dartlang.org"
423423
source: hosted
424-
version: "1.8.0"
424+
version: "1.8.1"
425425
stack_trace:
426426
dependency: transitive
427427
description:
@@ -442,7 +442,7 @@ packages:
442442
path: ".."
443443
relative: true
444444
source: path
445-
version: "1.2.0"
445+
version: "1.2.1"
446446
stream_transform:
447447
dependency: transitive
448448
description:
@@ -470,7 +470,7 @@ packages:
470470
name: test_api
471471
url: "https://pub.dartlang.org"
472472
source: hosted
473-
version: "0.2.19"
473+
version: "0.3.0"
474474
timing:
475475
dependency: transitive
476476
description:

lib/src/loader_bloc.dart

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,7 @@ import 'package:disposebag/disposebag.dart' show DisposeBag;
44
import 'package:distinct_value_connectable_stream/distinct_value_connectable_stream.dart'
55
show DistinctValueConnectableExtensions, DistinctValueStream;
66
import 'package:meta/meta.dart' show visibleForTesting;
7-
import 'package:rxdart_ext/rxdart_ext.dart'
8-
show
9-
PublishSubject,
10-
Rx,
11-
SwitchMapExtension,
12-
ExhaustMapExtension,
13-
DoExtensions,
14-
OnErrorExtensions,
15-
ScanExtension,
16-
StartWithExtension;
7+
import 'package:rxdart_ext/rxdart_ext.dart';
178

189
import 'loader_message.dart';
1910
import 'loader_state.dart';
@@ -22,6 +13,37 @@ import 'utils.dart';
2213

2314
// ignore_for_file: close_sinks
2415

16+
/// Defines which flatMap behavior should be applied whenever a new values is emitted.
17+
enum FlatMapPolicy {
18+
/// uses [FlatMapExtension.flatMap].
19+
merge,
20+
21+
/// uses [Stream.asyncExpand].
22+
concat,
23+
24+
/// uses [SwitchMapExtension.switchMap].
25+
latest,
26+
27+
/// uses [ExhaustMapExtension.exhaustMap].
28+
first,
29+
}
30+
31+
extension _FlatMapWithPolicy<T> on Stream<T> {
32+
Stream<R> flatMapWithPolicy<R>(
33+
FlatMapPolicy policy, Stream<R> Function(T) transform) {
34+
switch (policy) {
35+
case FlatMapPolicy.merge:
36+
return flatMap(transform);
37+
case FlatMapPolicy.concat:
38+
return asyncExpand(transform);
39+
case FlatMapPolicy.latest:
40+
return switchMap(transform);
41+
case FlatMapPolicy.first:
42+
return exhaustMap(transform);
43+
}
44+
}
45+
}
46+
2547
/// BLoC that handles loading and refreshing data
2648
class LoaderBloc<Content extends Object> {
2749
static const _tag = '« stream_loader »';
@@ -32,22 +54,23 @@ class LoaderBloc<Content extends Object> {
3254
/// Message stream
3355
final Stream<LoaderMessage<Content>> message$;
3456

35-
/// Call this function fetch data
57+
/// Call this function to fetch data
3658
final void Function() fetch;
3759

3860
/// Call this function to refresh data
3961
final Future<void> Function() refresh;
4062

4163
/// Clean up resources
42-
final Future<void> Function() dispose;
64+
Future<void> dispose() => _dispose();
65+
final Future<void> Function() _dispose;
4366

4467
LoaderBloc._({
45-
required this.dispose,
68+
required Future<void> Function() dispose,
4669
required this.state$,
4770
required this.fetch,
4871
required this.refresh,
4972
required this.message$,
50-
});
73+
}) : _dispose = dispose;
5174

5275
/// Construct a [LoaderBloc]
5376
/// The [loaderFunction] is a function return a stream of [Content]s (must be not null).
@@ -63,6 +86,10 @@ class LoaderBloc<Content extends Object> {
6386
Stream<Content> Function()? refresherFunction,
6487
Content? initialContent,
6588
void Function(String)? logger,
89+
FlatMapPolicy loaderFlatMapPolicy =
90+
FlatMapPolicy.latest, // default is `switchMap`
91+
FlatMapPolicy refreshFlatMapPolicy =
92+
FlatMapPolicy.first, // default is `exhaustMap`
6693
}) {
6794
refresherFunction ??= () => Stream<Content>.empty();
6895

@@ -73,7 +100,8 @@ class LoaderBloc<Content extends Object> {
73100
final controllers = <StreamController<dynamic>>[fetchS, refreshS, messageS];
74101

75102
/// Input actions to state
76-
final fetchChanges = fetchS.stream.switchMap(
103+
final fetchChanges = fetchS.stream.flatMapWithPolicy(
104+
loaderFlatMapPolicy,
77105
(_) => Rx.defer(loaderFunction)
78106
.doOnData(
79107
(content) => messageS.add(LoaderMessage.fetchSuccess(content)))
@@ -84,7 +112,8 @@ class LoaderBloc<Content extends Object> {
84112
.onErrorReturnWith(
85113
(e, _) => LoaderPartialStateChange.fetchFailure(e)),
86114
);
87-
final refreshChanges = refreshS.stream.exhaustMap(
115+
final refreshChanges = refreshS.stream.flatMapWithPolicy(
116+
refreshFlatMapPolicy,
88117
(completer) => Rx.defer(refresherFunction!)
89118
.doOnData(
90119
(content) => messageS.add(LoaderMessage.refreshSuccess(content)))

pubspec.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ packages:
2828
name: async
2929
url: "https://pub.dartlang.org"
3030
source: hosted
31-
version: "2.5.0"
31+
version: "2.6.1"
3232
boolean_selector:
3333
dependency: transitive
3434
description:
@@ -386,7 +386,7 @@ packages:
386386
name: source_span
387387
url: "https://pub.dartlang.org"
388388
source: hosted
389-
version: "1.8.0"
389+
version: "1.8.1"
390390
stack_trace:
391391
dependency: transitive
392392
description:
@@ -428,7 +428,7 @@ packages:
428428
name: test_api
429429
url: "https://pub.dartlang.org"
430430
source: hosted
431-
version: "0.2.19"
431+
version: "0.3.0"
432432
timing:
433433
dependency: transitive
434434
description:

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: stream_loader
22
description: A flutter plugin for loading content asynchronously with Dart stream. RxDart loader bloc.
3-
version: 1.2.0
3+
version: 1.2.1
44
homepage: https://github.com/hoc081098/stream_loader.git
55
repository: https://github.com/hoc081098/stream_loader.git
66
issue_tracker: https://github.com/hoc081098/stream_loader/issues

0 commit comments

Comments
 (0)