stream_ext – bringing more Rx API to the Dart

Over the last week or so, I’ve been look­ing at and play­ing 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 par­al­lels 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 absen­tees which I have often found use­ful, such as merge, zip, delay and win­dow.

With these absen­tees in mind I start­ed a small Dart project called stream_ext (for stream exten­sions) to port them over, and here are some details of the first cou­ple of func­tions I have done along with some live demo exam­ples.

 

merge

The merge func­tion merges two streams into a sin­gle uni­fied out­put stream.

stream_ext_merge

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

 

com­bineLat­est

The com­bineLat­est func­tion merges two streams into one by using the sup­plied selec­tor func­tion when­ev­er one of the streams pro­duces an event.

stream_ext_combineLatest

You can try a live demo use of the com­bineLat­est func­tion here, with the source code here.

 

delay

The delay func­tion cre­ates a new stream whose events are direct­ly sourced from the input stream but each deliv­ered after the spec­i­fied dura­tion.

stream_ext_delay

You can try a live demo (a port of the ‘Time flies like an array’ exam­ple) use of the delay func­tion here, with the source code here.

 

throt­tle

The throt­tle func­tion cre­ates a new stream based on events pro­duced by the spec­i­fied input, upon for­ward­ing an event from the input stream it’ll ignore any sub­se­quent events pro­duced by the input stream until the flow of new events has paused for the spec­i­fied dura­tion, after which the last event pro­duced by the input stream is then deliv­ered.

stream_ext_throttle

You can try a live demo use of the throt­tle func­tion here, with the source code here.

 

zip

The zip func­tion com­bines two streams into one by com­bin­ing their ele­ments in a pair­wise fash­ion, com­bin­ing the lat­est val­ue on each stream and pro­duc­ing a new event using the sup­plied zip­per func­tion.

stream_ext_zip

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

 

It’s still ear­ly days and there are quite a few more use­ful Rx func­tion I think will be use­ful to peo­ple work­ing with Dart, and I hope you find these exten­sion func­tions use­ful and please don’t hes­i­tate to get in touch and leave some feed­backs on the project!