Skip to content

Commit

Permalink
switchPath should keep broadcast behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
rbellens committed Dec 16, 2020
1 parent 9ca31e1 commit aa885f2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## 0.1.0-dev.3
## 0.1.0-dev.4

- Initial version
19 changes: 11 additions & 8 deletions lib/src/stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,18 @@ extension SnapshotStreamX on Stream<Snapshot> {
/// [path] whenever this child changes.
Stream<Snapshot> switchPath(
String path, Stream<dynamic> Function(Snapshot snapshot) mapper) {
var controller = StreamController<Snapshot>(sync: true);
var stream = doOnDone(() {
controller.close();
var controller = BehaviorSubject<Snapshot>(sync: true);

return doOnData((v) => controller.add(v))
.doOnDone(() => controller.close())
.map((v) => v.child(path))
.distinct()
.switchMap((v) {
return CombineLatestStream.combine2<Snapshot, dynamic, Snapshot>(
controller.stream, mapper(v), (a, b) {
return a.setPath(path, b);
});
});
return stream.map((v) {
controller.add(v);
return v;
}).asyncSetPath(path,
controller.stream.child(path).switchMap((value) => mapper(value)));
}

/// Updates the content of each child with the value returned by [mapper]
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: snapshot
description: A library that can be used to implement data classes and simplifies conversion of JSON to data classes
version: 0.1.0-dev.3
version: 0.1.0-dev.4
homepage: https://github.com/appsup-dart/snapshot

environment:
Expand All @@ -13,7 +13,7 @@ dependencies:
meta: ^1.1.8
collection: ^1.14.0
quiver: ^2.1.0
rxdart: ^0.24.0
rxdart: ^0.25.0

dev_dependencies:
pedantic: ^1.9.0
Expand Down
14 changes: 14 additions & 0 deletions test/stream_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ void main() async {
]);
expect(values, l);
});

test('Should keep broadcast behavior', () async {
var controller = StreamController.broadcast();

var stream = controller.stream.toSnapshots();

stream = stream.switchPath('address', (s) {
var v =
'${s.child('street').as()} ${s.child('number').as()}, ${s.child('city').as()}';
return Stream.value(v);
});

expect(stream.isBroadcast, true);
});
});

group('Stream<Snapshot>.mapChildren', () {
Expand Down

0 comments on commit aa885f2

Please sign in to comment.