Skip to content
This repository was archived by the owner on May 20, 2023. It is now read-only.

Commit df97447

Browse files
Googlernshahan
authored andcommitted
Reduce the cost of create a SimpleStream that has no subscriptions by delaying the initialization of the subscriptions array to the listen method.
Add SimpleEmitter class for replacing LazyStreamEmitter. PiperOrigin-RevId: 204856419
1 parent 7430103 commit df97447

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

lib/src/utils/async/simple_stream.dart

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class SimpleStream<T> extends StreamView<T> implements EventSink<T> {
5151
/// Using array as the assumption is that there will mainly be limited number
5252
/// of subscribers and overhead of a map is more. It can always be made
5353
/// smarter. If it is null, it means that the stream is closed.
54-
List<SimpleStreamSubscription<T>> _subscriptions = [];
54+
List<SimpleStreamSubscription<T>> _subscriptions = const [];
5555

5656
/// List of items to send to avoid scheduling multiple micro tasks for each
5757
/// item to be sent.
@@ -239,7 +239,8 @@ class SimpleStream<T> extends StreamView<T> implements EventSink<T> {
239239
}
240240

241241
@override
242-
StreamSubscription<T> listen(onData, {onError, onDone, cancelOnError}) {
242+
StreamSubscription<T> listen(void onData(T event),
243+
{Function onError, void onDone(), bool cancelOnError}) {
243244
// Don't allow listening to a closed stream, it will throw exception in
244245
// non checked mode since subscriptions will be null once the stream is
245246
// closed.
@@ -250,7 +251,11 @@ class SimpleStream<T> extends StreamView<T> implements EventSink<T> {
250251
}
251252
var sub = new SimpleStreamSubscription<T>(
252253
this, onData, onDone, onError, cancelOnError, contextFunc);
253-
_subscriptions.add(sub);
254+
if (_subscriptions.isEmpty) {
255+
_subscriptions = [sub];
256+
} else {
257+
_subscriptions.add(sub);
258+
}
254259
if (_onListen != null && _subscriptions.length == 1) {
255260
_onListen(sub);
256261
}
@@ -282,7 +287,8 @@ class LastStateStream<T> extends SimpleStream<T> {
282287
}
283288

284289
@override
285-
StreamSubscription<T> listen(onData, {onError, onDone, cancelOnError}) {
290+
StreamSubscription<T> listen(void onData(T event),
291+
{Function onError, void onDone(), bool cancelOnError}) {
286292
SimpleStreamSubscription<T> sub = super.listen(onData,
287293
onError: onError, onDone: onDone, cancelOnError: cancelOnError);
288294
if (_lastItem != null) {
@@ -403,3 +409,27 @@ class SimpleStreamSubscription<T> implements StreamSubscription<T> {
403409
throw new UnsupportedError('Not supported.');
404410
}
405411
}
412+
413+
/// Provides an interface for both StreamController & and Stream for use with
414+
/// output events in Angular components.
415+
///
416+
/// Reduces the amount of boilerplate needed by removing the need for a getter
417+
/// that returns the stream for angular.
418+
class SimpleEmitter<T> extends SimpleStream<T> {
419+
SimpleEmitter(
420+
{bool isSync = true,
421+
bool runInZone = true,
422+
SubscriptionChangeListener onListen,
423+
SubscriptionChangeListener onCancel})
424+
: super.broadcast(
425+
isSync: isSync,
426+
runInZone: runInZone,
427+
onListen: onListen,
428+
onCancel: onCancel);
429+
430+
/// Returns referene to object.
431+
Stream<T> get stream => this;
432+
433+
/// Returns referene to object.
434+
EventSink<T> get sink => this;
435+
}

0 commit comments

Comments
 (0)