cujojs / most

Ultra-high performance reactive programming
MIT License
3.5k stars 231 forks source link

Suggestions on the best way to replicate RxJS buffer() #464

Open far-blue opened 7 years ago

far-blue commented 7 years ago

Summary

RxJS has a buffer() method (http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-buffer) which is often used in examples of how to use streams/observables. A common example is gathering click events over a short time period in a buffer and then counting the events and emitting a stream of 'double click' events.

Does anyone have a neat way to achieve the same thing in MostJS?

It might be worth adding to the recipes or examples for people coming from tutorials based on other frameworks.

briancavalier commented 7 years ago

Hi @far-blue. There are no buffering operations in the core library.

There have a been a few use cases for buffering brought up in gitter, and they are usually solved fairly easily with scan or loop, plus filter.

Specifically for double click events ('dblclick'), they are supported in @most/dom-event by using browsers' native double click events.

If you have a concrete use case for buffering, that'd be a great way to ground the discussion here.

far-blue commented 7 years ago

Hello :)

I'm pretty new to mostjs so maybe my approaches are wrong but my particular use-case was in 'bucketing' requests for data from a rest api endpoint. I'm kind of starting with the 'everything is a stream' mentality and then stepping back from there if I don't feel a benefit in each case, trying to find a balance :)

So I know I have a number of requests to make to an endpoint and there will be a rush of them at the start followed by sporadic requests later in time. The endpoint allows for multiplexed requests so I thought it would be sensible to gather up a set of requests over a short period of time and then make them as one async request. Then I can unpack the results back into a stream when they come back. But I can't just wait for a set number of requests because they will come in sporadically (other than the initial rush when the web page loads). I figured I could use something like buffer to gather events over 100ms or so and output a new stream of Arrays of those original events which I could then async fetch as a single unit.

far-blue commented 7 years ago

An attempt at showing the streams:

-abc-de---fg---hij--->

-[abcde]---[fg]---[hij]---->

far-blue commented 7 years ago

For info, I have worked on a previous project where this was achieved with arrays of deferred promises and underscore delay(). It works but is pretty ugly.

cloderic commented 6 years ago

FYI, we've just published a package that does part of that: most-buffer. The limit in terms of time is also something we might need (hence implement) in the future.

We're using most to orchestrate calls between a bunch of APIs with different needs in terms of bulk queries and co. having an short way to create pages of events in the stream is really needed.