0
---abcde-----f-------gh-----i---->  //Events

I have a "work queue" that I want to observe/subscribe to. This is an array of command objects to process. New items of work typically arrive in bursts and they need to be processed serially (in the order received, one at a time, until fully processed).

I'm using RxJS 5.0.0-beta.6. (version imposed by other libraries)

Here is a working example that illustrates the behavior I want, but uses RxJS v4.

The main code in question is this...

var events$ = Rx.Observable.fromEvent(produceEvent, 'click')
  .timestamp()
  .tap(({timestamp}) => updatePanelAppend(pending, timestamp));

var inProgress$ = events$;

var done$ = inProgress$
  .flatMapWithMaxConcurrent(1, ({timestamp}) => 
                            Rx.Observable.fromPromise(() => {
                              updatePanelAppend(inProgress, timestamp);
                              removeFromPanel(pending, timestamp);
                              return expensiveComputation(getRandomInt(1, 5) * 1000, timestamp)
                           }));

done$.subscribeOnNext((timestamp) => {
  updatePanelAppend(done, timestamp);
  removeFromPanel(inProgress, timestamp);
});

http://jsbin.com/meyife/edit?js,output

Given the current beta state of the API and incomplete/changing documentation, I can't figure out how to do this in RxJS 5.

Update: This migration guide for moving from v4 to v5 shows many function that were removed, but does not direct how to do things the new way. Examples of removed operations: .tap, .controlled, .flatMapWithMaxConcurrent (renamed).

Mark Eric
  • 753
  • 1
  • 7
  • 14
  • Question where v4 solution came from: http://stackoverflow.com/questions/38601451/how-to-process-rxjs-stream-n-items-at-a-time-and-once-an-item-is-done-autofill – Mark Eric Aug 02 '16 at 12:48

1 Answers1

2
  • flatMap/mergeMap - Now takes a concurrency parameter

  • tap -> do

  • subscribeOnNext no longer exists, so just use subscribe with a single parameter.

  • fromPromise overload does not exist on RxJS 5 so use defer instead.

See the updated jsbin here

paulpdaniels
  • 18,395
  • 2
  • 51
  • 55
  • Excellent! Very helpful! Note: after changing the RxJS version to 5.0.0-beta.6 it needed some changes. Like the .timestamp() operator doesn't exist in that version. So I just pulled the timeStamp from the mouse click event as that wasn't central to the code. – Mark Eric Aug 19 '16 at 05:18