Skip to content

Commit

Permalink
feat: add streamJoin4
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Nov 14, 2024
1 parent 6941b80 commit 725e141
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
21 changes: 21 additions & 0 deletions lib/stream/stream_join.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,24 @@ Stream<(StreamJoinItem<T1>, StreamJoinItem<T2>, StreamJoinItem<T3>)>
(event) =>
(event[0].cast<T1>(), event[1].cast<T2>(), event[2].cast<T3>()));
}

/// Join 4 streams
Stream<(T1, T2, T3, T4)> streamJoin4<T1 extends Object?, T2 extends Object?,
T3 extends Object?, T4 extends Object?>(Stream<T1> stream1,
Stream<T2> stream2, Stream<T3> stream3, Stream<T4> stream4) {
return streamJoinAll<Object?>([stream1, stream2, stream3, stream4]).map(
(event) =>
(event[0] as T1, event[1] as T2, event[2] as T3, event[3] as T4));
}

/// Join 4 streams
Stream<(T1, T2, T3, T4)> streamJoin4OrError<T1 extends Object?,
T2 extends Object?, T3 extends Object?, T4 extends Object?>(
Stream<T1> stream1,
Stream<T2> stream2,
Stream<T3> stream3,
Stream<T4> stream4) {
return streamJoinAllOrError<Object?>([stream1, stream2, stream3, stream4])
.map((event) =>
(event[0] as T1, event[1] as T2, event[2] as T3, event[3] as T4));
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: tekartik_common_utils
version: 0.15.11
version: 0.15.12
description: Common pure dart utilities (not io or browser dependant)
homepage: https://github.com/tekartik/common_utils.dart
publish_to: none
Expand Down
7 changes: 7 additions & 0 deletions test/stream/stream_join_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ void main() {
.first;
expect(value, equals((1, '2', null)));
});
test('streamJoin4', () async {
(int?, String, String?, int?) value;
value = await streamJoin4(oneIntStream(), oneStringStream(),
anotherOptionalStringStream(null), oneIntStream(10))
.first;
expect(value, equals((1, '2', null, 10)));
});
test('streamJoinAll', () {
expect(emptyStream(), emitsInOrder([emitsDone]));
expect(streamJoinAll([emptyStream()]), emitsInOrder([emitsDone]));
Expand Down

0 comments on commit 725e141

Please sign in to comment.