asynchronous - Can Dart streams save data? -
it not clear me whether the single subscriber streams in dart save data receive. if do, there way disable this, since seems major memory leak?
with new async*
functions in dart, streams produced these store data?
the code-doc on streamcontroller constructor says
- the controller buffer incoming events until subscriber is
- registered.
to avoid queued events can use broadcast stream
new streamcontroller.broadcast(...);
or pause subscription
streamsubscription sub; sub = s.listen((e) { sub.pause(); // process event sub.resume(); });
a stream created async*
behaves same
import 'dart:async'; stream<int> a() async* { (int = 1; <= 10; ++i) { print('yield $i'); yield i; } } main() { a().listen((e) async { await new future.delayed(const duration(seconds: 1)); print(e); }); streamsubscription sub; sub = a().listen((e) async { sub.pause(); await new future.delayed(const duration(seconds: 1)); print(e); sub.resume(); }); }
try @ dartpad
the first example prints
yield 1 yield 2 yield 3 yield 4 yield 5 yield 6 yield 7 yield 8 yield 9 yield 10 1 2 3 4 5 6 7 8 9 10
the second example (with pause
) prints
yield 1 yield 2 yield 3 1 2 yield 4 3 yield 5 4 yield 6 5 yield 7 6 yield 8 7 yield 9 8 yield 10 9 10
Comments
Post a Comment