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

Commit 9de016f

Browse files
authored
Add a shared ignoreArgument callback (#190)
Add a `common_callbacks.dart` library with a single top level function `ignoreArgument` which takes a single `dynamic` argument and does nothing. Replace all `(_) => null` function literals, as well as a few library-private functions with unnecessarily specific signatures, with the shared definition. As suggested in #188
1 parent da2c8ac commit 9de016f

File tree

8 files changed

+29
-13
lines changed

8 files changed

+29
-13
lines changed

lib/src/aggregate_sample.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import 'dart:async';
66

7+
import 'common_callbacks.dart';
8+
79
extension AggregateSample<T> on Stream<T> {
810
/// Computes a value based on sequences of events, then emits that value when
911
/// [trigger] emits an event.
@@ -136,7 +138,7 @@ extension AggregateSample<T> on Stream<T> {
136138
triggerSub!.pause();
137139
}
138140
if (cancels.isEmpty) return null;
139-
return cancels.wait.then((_) => null);
141+
return cancels.wait.then(ignoreArgument);
140142
};
141143
};
142144
return controller.stream;

lib/src/async_expand.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66

7+
import 'common_callbacks.dart';
78
import 'switch.dart';
89

910
/// Alternatives to [asyncExpand].
@@ -78,7 +79,9 @@ extension AsyncExpand<T> on Stream<T> {
7879
}
7980
controller.onCancel = () {
8081
if (subscriptions.isEmpty) return null;
81-
return [for (var s in subscriptions) s.cancel()].wait.then((_) => null);
82+
return [for (var s in subscriptions) s.cancel()]
83+
.wait
84+
.then(ignoreArgument);
8285
};
8386
};
8487
return controller.stream;

lib/src/async_map.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66

77
import 'aggregate_sample.dart';
8+
import 'common_callbacks.dart';
89
import 'from_handlers.dart';
910
import 'rate_limit.dart';
1011

@@ -72,7 +73,7 @@ extension AsyncMap<T> on Stream<T> {
7273
trigger: workFinished.stream,
7374
aggregate: _dropPrevious,
7475
longPoll: true,
75-
onEmpty: _ignore<T>)
76+
onEmpty: ignoreArgument)
7677
._asyncMapThen(convert, workFinished.add);
7778
}
7879

@@ -133,4 +134,3 @@ extension AsyncMap<T> on Stream<T> {
133134
}
134135

135136
T _dropPrevious<T>(T event, _) => event;
136-
void _ignore<T>(Sink<T> sink) {}

lib/src/combine_latest.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import 'dart:async';
66

7+
import 'common_callbacks.dart';
8+
79
/// Utilities to combine events from multiple streams through a callback or into
810
/// a list.
911
extension CombineLatest<T> on Stream<T> {
@@ -131,7 +133,7 @@ extension CombineLatest<T> on Stream<T> {
131133
];
132134
sourceSubscription = null;
133135
otherSubscription = null;
134-
return cancels.wait.then((_) => null);
136+
return cancels.wait.then(ignoreArgument);
135137
};
136138
};
137139
return controller.stream;
@@ -228,7 +230,9 @@ extension CombineLatest<T> on Stream<T> {
228230
}
229231
controller.onCancel = () {
230232
if (subscriptions.isEmpty) return null;
231-
return [for (var s in subscriptions) s.cancel()].wait.then((_) => null);
233+
return [for (var s in subscriptions) s.cancel()]
234+
.wait
235+
.then(ignoreArgument);
232236
};
233237
};
234238
return controller.stream;

lib/src/common_callbacks.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
void ignoreArgument(_) {}

lib/src/merge.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import 'dart:async';
66

7+
import 'common_callbacks.dart';
8+
79
/// Utilities to interleave events from multiple streams.
810
extension Merge<T> on Stream<T> {
911
/// Merges values and errors from this stream and [other] in any order as they
@@ -90,7 +92,9 @@ extension Merge<T> on Stream<T> {
9092
}
9193
controller.onCancel = () {
9294
if (subscriptions.isEmpty) return null;
93-
return [for (var s in subscriptions) s.cancel()].wait.then((_) => null);
95+
return [for (var s in subscriptions) s.cancel()]
96+
.wait
97+
.then(ignoreArgument);
9498
};
9599
};
96100
return controller.stream;

lib/src/rate_limit.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66

77
import 'aggregate_sample.dart';
8+
import 'common_callbacks.dart';
89
import 'from_handlers.dart';
910

1011
/// Utilities to rate limit events.
@@ -305,7 +306,7 @@ extension RateLimit<T> on Stream<T> {
305306
trigger: trigger,
306307
aggregate: _dropPrevious,
307308
longPoll: longPoll,
308-
onEmpty: _ignore);
309+
onEmpty: ignoreArgument);
309310

310311
/// Aggregates values until this source stream does not emit for [duration],
311312
/// then emits the aggregated values.
@@ -353,4 +354,3 @@ extension RateLimit<T> on Stream<T> {
353354
T _dropPrevious<T>(T element, _) => element;
354355
List<T> _collect<T>(T event, List<T>? soFar) => (soFar ?? <T>[])..add(event);
355356
void _empty<T>(Sink<List<T>> sink) => sink.add([]);
356-
void _ignore<T>(Sink<T> sink) {}

lib/src/switch.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66

77
import 'async_expand.dart';
8+
import 'common_callbacks.dart';
89

910
/// A utility to take events from the most recent sub stream returned by a
1011
/// callback.
@@ -126,12 +127,9 @@ extension SwitchLatest<T> on Stream<Stream<T>> {
126127
if (sub != null) sub.cancel(),
127128
];
128129
if (cancels.isEmpty) return null;
129-
return cancels.wait.then(_ignore);
130+
return cancels.wait.then(ignoreArgument);
130131
};
131132
};
132133
return controller.stream;
133134
}
134135
}
135-
136-
/// Helper function to ignore future callback
137-
void _ignore(_, [__]) {}

0 commit comments

Comments
 (0)