stream_ext — bringing more Rx API to the Dart

Yan Cui
theburningmonk.com
Published in
3 min readSep 8, 2013

Over the last week or so, I’ve been looking at and playing around with the Streams API in Dart, which has been (in part at least) based on the Rx API, and it’s easy to see the parallels between the two sets of APIs and you can find most of the core Rx APIs on Dart’s Stream type already, but there were a few notable absentees which I have often found useful, such as merge, zip, delay and window.

With these absentees in mind I started a small Dart project called stream_ext (for stream extensions) to port them over, and here are some details of the first couple of functions I have done along with some live demo examples.

merge

The merge function merges two streams into a single unified output stream.

stream_ext_merge

You can try a live demo use of the merge function here, with the source code here.

combineLatest

The combineLatest function merges two streams into one by using the supplied selector function whenever one of the streams produces an event.

stream_ext_combineLatest

You can try a live demo use of the combineLatest function here, with the source code here.

delay

The delay function creates a new stream whose events are directly sourced from the input stream but each delivered after the specified duration.

stream_ext_delay

You can try a live demo (a port of the ‘Time flies like an array’ example) use of the delay function here, with the source code here.

throttle

The throttle function creates a new stream based on events produced by the specified input, upon forwarding an event from the input stream it’ll ignore any subsequent events produced by the input stream until the flow of new events has paused for the specified duration, after which the last event produced by the input stream is then delivered.

stream_ext_throttle

You can try a live demo use of the throttle function here, with the source code here.

zip

The zip function combines two streams into one by combining their elements in a pairwise fashion, combining the latest value on each stream and producing a new event using the supplied zipper function.

stream_ext_zip

You can try a live demo use of the zip function here, with the source code here.

It’s still early days and there are quite a few more useful Rx function I think will be useful to people working with Dart, and I hope you find these extension functions useful and please don’t hesitate to get in touch and leave some feedbacks on the project!

--

--

Yan Cui
theburningmonk.com

AWS Serverless Hero. Follow me to learn practical tips and best practices for AWS and Serverless.