Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit a1f8da1

Browse files
authored
Remove default onData for transformByHandlers (#187)
Towards #111 The default callback is not used in practice outside of tests, but it included a cast to satisfy the type signature which looks worrying for this type of library. Make the callback required and add explicit callbacks (without a cast since the types are consistent) in the tests which used it.
1 parent f00dedc commit a1f8da1

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

lib/src/from_handlers.dart

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ extension TransformByHandlers<S> on Stream<S> {
1111
/// that the handlers are called once per event rather than called for the
1212
/// same event for each listener on a broadcast stream.
1313
Stream<T> transformByHandlers<T>(
14-
{void Function(S, EventSink<T>)? onData,
14+
{required void Function(S, EventSink<T>) onData,
1515
void Function(Object, StackTrace, EventSink<T>)? onError,
1616
void Function(EventSink<T>)? onDone}) {
17-
final handleData = onData ?? _defaultHandleData;
1817
final handleError = onError ?? _defaultHandleError;
1918
final handleDone = onDone ?? _defaultHandleDone;
2019

@@ -26,7 +25,7 @@ extension TransformByHandlers<S> on Stream<S> {
2625
controller.onListen = () {
2726
assert(subscription == null);
2827
var valuesDone = false;
29-
subscription = listen((value) => handleData(value, controller),
28+
subscription = listen((value) => onData(value, controller),
3029
onError: (Object error, StackTrace stackTrace) {
3130
handleError(error, stackTrace, controller);
3231
}, onDone: () {
@@ -48,10 +47,6 @@ extension TransformByHandlers<S> on Stream<S> {
4847
return controller.stream;
4948
}
5049

51-
static void _defaultHandleData<S, T>(S value, EventSink<T> sink) {
52-
sink.add(value as T);
53-
}
54-
5550
static void _defaultHandleError<T>(
5651
Object error, StackTrace stackTrace, EventSink<T> sink) {
5752
sink.addError(error, stackTrace);

test/from_handlers_test.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ void main() {
3636
group('default from_handlers', () {
3737
group('Single subscription stream', () {
3838
setUp(() {
39-
setUpForController(StreamController(), (s) => s.transformByHandlers());
39+
setUpForController(StreamController(),
40+
(s) => s.transformByHandlers(onData: (e, sink) => sink.add(e)));
4041
});
4142

4243
test('has correct stream type', () {
@@ -75,8 +76,8 @@ void main() {
7576
late StreamSubscription<int> subscription2;
7677

7778
setUp(() {
78-
setUpForController(
79-
StreamController.broadcast(), (s) => s.transformByHandlers());
79+
setUpForController(StreamController.broadcast(),
80+
(s) => s.transformByHandlers(onData: (e, sink) => sink.add(e)));
8081
emittedValues2 = [];
8182
errors2 = [];
8283
isDone2 = false;

0 commit comments

Comments
 (0)