OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, 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 library glob.stream_pool; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 /// A pool of streams whose events are unified and emitted through a central | |
10 /// stream. | |
11 class StreamPool<T> { | |
12 /// The stream through which all events from streams in the pool are emitted. | |
13 Stream<T> get stream => _controller.stream; | |
14 final StreamController<T> _controller; | |
15 | |
16 /// Subscriptions to the streams that make up the pool. | |
17 final _subscriptions = new Map<Stream<T>, StreamSubscription<T>>(); | |
18 | |
19 /// Whether this pool should be closed when it becomes empty. | |
20 bool _closeWhenEmpty = false; | |
21 | |
22 /// Creates a new stream pool that only supports a single subscriber. | |
23 /// | |
24 /// Any events from broadcast streams in the pool will be buffered until a | |
25 /// listener is subscribed. | |
26 StreamPool() | |
27 // Create the controller as sync so that any sync input streams will be | |
28 // forwarded synchronously. Async input streams will have their asynchrony | |
29 // preserved, since _controller.add will be called asynchronously. | |
30 : _controller = new StreamController<T>(sync: true); | |
31 | |
32 /// Creates a new stream pool where [stream] can be listened to more than | |
33 /// once. | |
34 /// | |
35 /// Any events from buffered streams in the pool will be emitted immediately, | |
36 /// regardless of whether [stream] has any subscribers. | |
37 StreamPool.broadcast() | |
38 // Create the controller as sync so that any sync input streams will be | |
39 // forwarded synchronously. Async input streams will have their asynchrony | |
40 // preserved, since _controller.add will be called asynchronously. | |
41 : _controller = new StreamController<T>.broadcast(sync: true); | |
42 | |
43 /// Adds [stream] as a member of this pool. | |
44 /// | |
45 /// Any events from [stream] will be emitted through [this.stream]. If | |
46 /// [stream] is sync, they'll be emitted synchronously; if [stream] is async, | |
47 /// they'll be emitted asynchronously. | |
48 void add(Stream<T> stream) { | |
49 if (_subscriptions.containsKey(stream)) return; | |
50 _subscriptions[stream] = stream.listen(_controller.add, | |
51 onError: _controller.addError, | |
52 onDone: () => remove(stream)); | |
53 } | |
54 | |
55 /// Removes [stream] as a member of this pool. | |
56 void remove(Stream<T> stream) { | |
57 var subscription = _subscriptions.remove(stream); | |
58 if (subscription != null) subscription.cancel(); | |
59 if (_closeWhenEmpty && _subscriptions.isEmpty) close(); | |
60 } | |
61 | |
62 /// Removes all streams from this pool and closes [stream]. | |
63 void close() { | |
64 for (var subscription in _subscriptions.values) { | |
65 subscription.cancel(); | |
66 } | |
67 _subscriptions.clear(); | |
68 _controller.close(); | |
69 } | |
70 | |
71 /// The next time this pool becomes empty, close it. | |
72 void closeWhenEmpty() { | |
73 if (_subscriptions.isEmpty) close(); | |
74 _closeWhenEmpty = true; | |
75 } | |
76 } | |
OLD | NEW |