Pattern for Observables that includes acknowledgement

前端 未结 3 1736
青春惊慌失措
青春惊慌失措 2021-01-21 23:43

I\'m working on something that is recording data coming from a queue. It was easy enough to process the queue into an Observable so that I can have multiple endpoints in my cod

3条回答
  •  忘掉有多难
    2021-01-22 00:24

    What you describe sounds like "backpressure". You can read about it in RxJS 4 documentation https://github.com/Reactive-Extensions/RxJS/blob/master/doc/gettingstarted/backpressure.md. However this is mentioning operators that don't exist in RxJS 5. For example have a look at "Controlled Observables" that should refer to what you need.

    I think you could achieve the same with concatMap and an instance of Subject:

    const asyncOperationEnd = new Subject();
    
    source.concatMap(val => asyncOperationEnd
        .mapTo(void 0)
        .startWith(val)
        .take(2) // that's `val` and the `void 0` that ends this inner Observable
      )
      .filter(Boolean) // Always ignore `void 0`
      .subscribe(val => {
        // do some async operation...
        // call `asyncOperationEnd.next()` and let `concatMap` process another value
      });
    

    Fro your description it actually seems like the "observer" you're mentioning works like Subject so it would make maybe more sense to make a custom Subject class that you could use in any Observable chain.

提交回复
热议问题