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

Commit

Permalink
Add a shared ignoreArgument callback (#190)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
natebosch authored Oct 19, 2024
1 parent da2c8ac commit 9de016f
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 13 deletions.
4 changes: 3 additions & 1 deletion lib/src/aggregate_sample.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import 'dart:async';

import 'common_callbacks.dart';

extension AggregateSample<T> on Stream<T> {
/// Computes a value based on sequences of events, then emits that value when
/// [trigger] emits an event.
Expand Down Expand Up @@ -136,7 +138,7 @@ extension AggregateSample<T> on Stream<T> {
triggerSub!.pause();
}
if (cancels.isEmpty) return null;
return cancels.wait.then((_) => null);
return cancels.wait.then(ignoreArgument);
};
};
return controller.stream;
Expand Down
5 changes: 4 additions & 1 deletion lib/src/async_expand.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:async';

import 'common_callbacks.dart';
import 'switch.dart';

/// Alternatives to [asyncExpand].
Expand Down Expand Up @@ -78,7 +79,9 @@ extension AsyncExpand<T> on Stream<T> {
}
controller.onCancel = () {
if (subscriptions.isEmpty) return null;
return [for (var s in subscriptions) s.cancel()].wait.then((_) => null);
return [for (var s in subscriptions) s.cancel()]
.wait
.then(ignoreArgument);
};
};
return controller.stream;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/async_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';

import 'aggregate_sample.dart';
import 'common_callbacks.dart';
import 'from_handlers.dart';
import 'rate_limit.dart';

Expand Down Expand Up @@ -72,7 +73,7 @@ extension AsyncMap<T> on Stream<T> {
trigger: workFinished.stream,
aggregate: _dropPrevious,
longPoll: true,
onEmpty: _ignore<T>)
onEmpty: ignoreArgument)
._asyncMapThen(convert, workFinished.add);
}

Expand Down Expand Up @@ -133,4 +134,3 @@ extension AsyncMap<T> on Stream<T> {
}

T _dropPrevious<T>(T event, _) => event;
void _ignore<T>(Sink<T> sink) {}
8 changes: 6 additions & 2 deletions lib/src/combine_latest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import 'dart:async';

import 'common_callbacks.dart';

/// Utilities to combine events from multiple streams through a callback or into
/// a list.
extension CombineLatest<T> on Stream<T> {
Expand Down Expand Up @@ -131,7 +133,7 @@ extension CombineLatest<T> on Stream<T> {
];
sourceSubscription = null;
otherSubscription = null;
return cancels.wait.then((_) => null);
return cancels.wait.then(ignoreArgument);
};
};
return controller.stream;
Expand Down Expand Up @@ -228,7 +230,9 @@ extension CombineLatest<T> on Stream<T> {
}
controller.onCancel = () {
if (subscriptions.isEmpty) return null;
return [for (var s in subscriptions) s.cancel()].wait.then((_) => null);
return [for (var s in subscriptions) s.cancel()]
.wait
.then(ignoreArgument);
};
};
return controller.stream;
Expand Down
5 changes: 5 additions & 0 deletions lib/src/common_callbacks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

void ignoreArgument(_) {}
6 changes: 5 additions & 1 deletion lib/src/merge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import 'dart:async';

import 'common_callbacks.dart';

/// Utilities to interleave events from multiple streams.
extension Merge<T> on Stream<T> {
/// Merges values and errors from this stream and [other] in any order as they
Expand Down Expand Up @@ -90,7 +92,9 @@ extension Merge<T> on Stream<T> {
}
controller.onCancel = () {
if (subscriptions.isEmpty) return null;
return [for (var s in subscriptions) s.cancel()].wait.then((_) => null);
return [for (var s in subscriptions) s.cancel()]
.wait
.then(ignoreArgument);
};
};
return controller.stream;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/rate_limit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';

import 'aggregate_sample.dart';
import 'common_callbacks.dart';
import 'from_handlers.dart';

/// Utilities to rate limit events.
Expand Down Expand Up @@ -305,7 +306,7 @@ extension RateLimit<T> on Stream<T> {
trigger: trigger,
aggregate: _dropPrevious,
longPoll: longPoll,
onEmpty: _ignore);
onEmpty: ignoreArgument);

/// Aggregates values until this source stream does not emit for [duration],
/// then emits the aggregated values.
Expand Down Expand Up @@ -353,4 +354,3 @@ extension RateLimit<T> on Stream<T> {
T _dropPrevious<T>(T element, _) => element;
List<T> _collect<T>(T event, List<T>? soFar) => (soFar ?? <T>[])..add(event);
void _empty<T>(Sink<List<T>> sink) => sink.add([]);
void _ignore<T>(Sink<T> sink) {}
6 changes: 2 additions & 4 deletions lib/src/switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';

import 'async_expand.dart';
import 'common_callbacks.dart';

/// A utility to take events from the most recent sub stream returned by a
/// callback.
Expand Down Expand Up @@ -126,12 +127,9 @@ extension SwitchLatest<T> on Stream<Stream<T>> {
if (sub != null) sub.cancel(),
];
if (cancels.isEmpty) return null;
return cancels.wait.then(_ignore);
return cancels.wait.then(ignoreArgument);
};
};
return controller.stream;
}
}

/// Helper function to ignore future callback
void _ignore(_, [__]) {}

0 comments on commit 9de016f

Please sign in to comment.