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

RxJS 5 bufferWithTimeOrCount? (add maxCount to bufferTime) #1295

Closed
avielfedida opened this issue Feb 3, 2016 · 5 comments
Closed

RxJS 5 bufferWithTimeOrCount? (add maxCount to bufferTime) #1295

avielfedida opened this issue Feb 3, 2016 · 5 comments
Labels
feature PRs and issues for features help wanted Issues we wouldn't mind assistance with.
Milestone

Comments

@avielfedida
Copy link

So RxJS 5 currently not containing bufferWithTimeOrCount, is it planned to be added in the future?, anyway I was looking for comparable way to "do" bufferWithTimeOrCount in RxJS 5, should I create my own operator or there is a nicer combo with the already ported operators?

@benlesh
Copy link
Member

benlesh commented Feb 3, 2016

Hmmm... seems like an oversight. I think we can probably add an additional argument to bufferTime to cover this without impacting performance greatly.

Until we add that, you could use something like this:

(I haven't tested it, and it looks a little gross but with some tweaks it may work):

function bufferWithTimeOrCount(source, duration, bufferSize) {
  let reset = false;

  const buffers = source.scan((buffer, x) => {
    if (reset) {
      buffer = [];
      reset = false;
    }
    buffer.push(x);
    return buffer;
  }, []).share();

  const bufferCounted = buffers.filter(buffer => buffer.length < bufferSize);
  const timer = Observable.of(null).merge(bufferCounted).switchMap(() => Observable.timer(duration));

  return buffers.sample(timer.merge(bufferCounted)).do(() => reset = true);
}

@benlesh benlesh added feature PRs and issues for features help wanted Issues we wouldn't mind assistance with. labels Feb 3, 2016
@benlesh benlesh added this to the 5.0.0 release milestone Feb 3, 2016
@benlesh
Copy link
Member

benlesh commented Feb 3, 2016

Thank you for your patience and for bring this issue up for us, @avielfedida. We'll get it fixed.

@benlesh
Copy link
Member

benlesh commented Feb 3, 2016

Contributors or those looking to implement this:

The idea I have here is that we can add an optional maxBufferSize parameter to bufferTime (likewise with windowTime) that would default to Number.POSITIVE_INFINITY. Then rework the the operator to support that functionality.

The operator function itself can be mildly polymorphic: bufferTime(1000), bufferTime(1000, scheduler), or bufferTime(1000, 5, scheduler), but per Rx design, scheduler should be the last argument.

@benlesh benlesh changed the title RxJS 5 bufferWithTimeOrCount? RxJS 5 bufferWithTimeOrCount? (add maxCount to bufferTime) Feb 3, 2016
@justinwoo
Copy link
Contributor

Sorry if I have the details wrong, but bufferTimeOrCount only allows a single buffer at a time, right? And as soon as a buffer is flushed, a new buffer starts immediately? (e.g. this JSBin for single/double clicks)

It looks like in the current implementation of bufferWithTime, you'd have to purposefully limit and rewire the scheduling of opening multiple buffers and all, which sounds hard/like a lot of work.

I have a small implementation for bufferTimeOrCount with this base spec, but I don't know if this necessarily behaves as expected.

@lock
Copy link

lock bot commented Jun 7, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature PRs and issues for features help wanted Issues we wouldn't mind assistance with.
Projects
None yet
Development

No branches or pull requests

3 participants