in rx.js make source.subscribe await it's observer using async/await

Viewed 496

I have an observer that goes like this.

var source = rx.Observable.fromEvent(eventAppeared.emitter, 'event')
      .filter(mAndF.isValidStreamType)
      .map(mAndF.transformEvent)
      .share();

I then share it with a number of subscribers. These subscribers all take the event and perform some async operations on them. so my subscribers are like

 source.subscribe(async function(x) {
  const func = handler[x.eventName];
  if (func) {
    await eventWorkflow(x, handler.handlerName, func.bind(handler));
  }
});

There's a bit of extra stuff in there but I think the intent is clear. I need every "handler" that handles this particular event to handle it and block till it gets back. Then process the next event.

What I've found with the above code is that it's just calling the event with out awaiting it and my handlers are stepping on themselves.

I've read a fair number of posts, but I can't really see how to do it. Most people are talking about making the observer awaitable. But that's not what I need is it? It seems like what I need is to make the observer awaitable. I can't find anything on that which usually means it's either super easy or a super ridiculous thing to do. I'm hoping for the former.

Please let me know if you need any further clarification.

---update---

what I have realized is that what I need is a fifo queue or buffer ( first in first out ), sometimes referred to as back pressure. I need all messages processed in order and only when the preceding message is done processing.

---end update---

at first I thought it was cuz I was using rx 2.5.3 but I just upgraded to 4.1.0 and it's still not synchronous.

2 Answers
Related