Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(pairwise): change return type of pairwise to Stream<List<T>> #685

Merged
merged 3 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

### Documentation

### Code refactoring

* Change return type of `pairwise` to `Stream<List<T>>`.

## 0.27.4 (2022-05-29)

### Bug fixes
Expand Down
10 changes: 5 additions & 5 deletions lib/src/transformers/backpressure/buffer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class BufferStreamTransformer<T>
/// The [List] is cleared upon every [window] event.
BufferStreamTransformer(Stream Function(T event) window)
: super(WindowStrategy.firstEventOnly, window,
onWindowEnd: (List<T> queue) => queue, ignoreEmptyWindows: false);
onWindowEnd: (queue) => queue, ignoreEmptyWindows: false);
}

/// Buffers a number of values from the source Stream by count then
Expand Down Expand Up @@ -58,9 +58,9 @@ class BufferCountStreamTransformer<T>
/// the previous one reaches a length of [count].
BufferCountStreamTransformer(int count, [int startBufferEvery = 0])
: super(WindowStrategy.onHandler, null,
onWindowEnd: (List<T> queue) => queue,
onWindowEnd: (queue) => queue,
startBufferEvery: startBufferEvery,
closeWindowWhen: (Iterable<T> queue) => queue.length == count) {
closeWindowWhen: (queue) => queue.length == count) {
if (count < 1) throw ArgumentError.value(count, 'count');
if (startBufferEvery < 0) {
throw ArgumentError.value(startBufferEvery, 'startBufferEvery');
Expand All @@ -82,8 +82,8 @@ class BufferTestStreamTransformer<T>
/// emits this [List] whenever the [test] Function yields true.
BufferTestStreamTransformer(bool Function(T value) test)
: super(WindowStrategy.onHandler, null,
onWindowEnd: (List<T> queue) => queue,
closeWindowWhen: (Iterable<T> queue) => test(queue.last));
onWindowEnd: (queue) => queue,
closeWindowWhen: (queue) => test(queue.last));
}

/// Extends the Stream class with the ability to buffer events in various ways
Expand Down
2 changes: 1 addition & 1 deletion lib/src/transformers/backpressure/debounce.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DebounceStreamTransformer<T> extends BackpressureStreamTransformer<T, T> {
: super(
WindowStrategy.everyEvent,
window,
onWindowEnd: (Iterable<T> queue) => queue.last,
onWindowEnd: (queue) => queue.last,
maxLengthQueue: 1,
);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/src/transformers/backpressure/pairwise.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import 'package:rxdart/src/transformers/backpressure/backpressure.dart';
/// .pairwise()
/// .listen(print); // prints [1, 2], [2, 3], [3, 4]
class PairwiseStreamTransformer<T>
extends BackpressureStreamTransformer<T, Iterable<T>> {
extends BackpressureStreamTransformer<T, List<T>> {
/// Constructs a [StreamTransformer] which buffers events into pairs as a [List].
PairwiseStreamTransformer()
: super(WindowStrategy.firstEventOnly, (_) => NeverStream<void>(),
onWindowEnd: (Iterable<T> queue) => queue,
onWindowEnd: (queue) => queue,
startBufferEvery: 1,
closeWindowWhen: (Iterable<T> queue) => queue.length == 2,
closeWindowWhen: (queue) => queue.length == 2,
dispatchOnClose: false);
}

Expand All @@ -31,5 +31,5 @@ extension PairwiseExtension<T> on Stream<T> {
/// RangeStream(1, 4)
/// .pairwise()
/// .listen(print); // prints [1, 2], [2, 3], [3, 4]
Stream<Iterable<T>> pairwise() => PairwiseStreamTransformer<T>().bind(this);
Stream<List<T>> pairwise() => PairwiseStreamTransformer<T>().bind(this);
}
2 changes: 1 addition & 1 deletion lib/src/transformers/backpressure/sample.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SampleStreamTransformer<T> extends BackpressureStreamTransformer<T, T> {
/// the sample [Stream].
SampleStreamTransformer(Stream Function(T event) window)
: super(WindowStrategy.firstEventOnly, window,
onWindowEnd: (Iterable<T> queue) => queue.last);
onWindowEnd: (queue) => queue.last);
}

/// Extends the Stream class with the ability to sample events from the Stream
Expand Down
10 changes: 5 additions & 5 deletions lib/src/transformers/backpressure/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class WindowStreamTransformer<T>
/// The [Stream] is recreated and starts empty upon every [window] event.
WindowStreamTransformer(Stream Function(T event) window)
: super(WindowStrategy.firstEventOnly, window,
onWindowEnd: (List<T> queue) => Stream.fromIterable(queue),
onWindowEnd: (queue) => Stream.fromIterable(queue),
ignoreEmptyWindows: false);
}

Expand Down Expand Up @@ -61,9 +61,9 @@ class WindowCountStreamTransformer<T>
/// the previous one reaches a length of [count].
WindowCountStreamTransformer(int count, [int startBufferEvery = 0])
: super(WindowStrategy.onHandler, null,
onWindowEnd: (List<T> queue) => Stream.fromIterable(queue),
onWindowEnd: (queue) => Stream.fromIterable(queue),
startBufferEvery: startBufferEvery,
closeWindowWhen: (Iterable<T> queue) => queue.length == count) {
closeWindowWhen: (queue) => queue.length == count) {
if (count < 1) throw ArgumentError.value(count, 'count');
if (startBufferEvery < 0) {
throw ArgumentError.value(startBufferEvery, 'startBufferEvery');
Expand All @@ -86,8 +86,8 @@ class WindowTestStreamTransformer<T>
/// emits this [Stream] whenever the [test] Function yields true.
WindowTestStreamTransformer(bool Function(T value) test)
: super(WindowStrategy.onHandler, null,
onWindowEnd: (List<T> queue) => Stream.fromIterable(queue),
closeWindowWhen: (Iterable<T> queue) => test(queue.last));
onWindowEnd: (queue) => Stream.fromIterable(queue),
closeWindowWhen: (queue) => test(queue.last));
}

/// Extends the Stream class with the ability to window
Expand Down
41 changes: 32 additions & 9 deletions test/transformers/backpressure/pairwise_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,38 @@ void main() {

final stream = Rx.range(1, 4).pairwise();

stream.listen(expectAsync1((result) {
// test to see if the combined output matches
expect(expectedOutput[count].length, result.length);
expect(expectedOutput[count][0], result.elementAt(0));
if (expectedOutput[count].length > 1) {
expect(expectedOutput[count][1], result.elementAt(1));
}
count++;
}, count: expectedOutput.length));
stream.listen(
expectAsync1((result) {
// test to see if the combined output matches
expect(result, expectedOutput[count++]);
}, count: expectedOutput.length),
onError: expectAsync2((Object e, StackTrace s) {}, count: 0),
onDone: expectAsync0(() {}, count: 1),
);
});

test('Rx.pairwise.empty', () {
expect(Stream<int>.empty().pairwise(), emitsDone);
});

test('Rx.pairwise.single', () {
expect(Stream.value(1).pairwise(), emitsDone);
});

test('Rx.pairwise.compatible', () {
expect(
Stream.fromIterable([1, 2]).pairwise(),
isA<Stream<Iterable<int>>>(),
);

Stream<Iterable<int>> s = Stream.fromIterable([1, 2]).pairwise();
expect(
s,
emitsInOrder(<Object>[
[1, 2],
emitsDone
]),
);
});

test('Rx.pairwise.asBroadcastStream', () async {
Expand Down