rxjsreactive-extensions-js

How can you seed a first item with bufferWithCount in rx.js?


Say you do something like:

Rx.Observable.range(1, 5).bufferWithCount(2, 1).subscribe(console.log);

This returns:

[1, 2]
[2, 3]
[3, 4]
[4, 5]
[5]

I'd like for the result to look like (basically force the first value to emit):

[<userDefined>, 1]
[1, 2]
[3, 4]
etc...

Solution

  • How about:

    Rx.Observable.range(1, 5)
      // Note this value will get used for every subscription
      // after it is defined.
      .startWith(userDefined)
      .bufferWithCount(2, 1)
      .subscribe(console.log);