Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
far-blue opened this issue Jul 1, 2017 · 5 comments
Open

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

far-blue opened this issue Jul 1, 2017 · 5 comments

Comments

@far-blue
Copy link
Contributor

far-blue commented Jul 1, 2017

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
Copy link
Member

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
Copy link
Contributor Author

far-blue commented Jul 1, 2017

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
Copy link
Contributor Author

far-blue commented Jul 1, 2017

An attempt at showing the streams:

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

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

@far-blue
Copy link
Contributor Author

far-blue commented Jul 1, 2017

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
Copy link
Contributor

cloderic commented Aug 10, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants