Search code examples
typescriptrxjsobservablengrxrxjs-pipeable-operators

rxjs Subscribe but don't need all to emit


When I subscribe to an observable stream I want to combine, but they don't both need to emit.

Any idea how to achieve this or what RxJs operator I can use?

// function in a service
getContent(): {
  return CombineLatest({
    list: of([ {...} ]) // always want this to be emitted
    ignore: of( null ), // subscribe but don't care if it's emitted or not.
  }).pipe(
     map(({ list }) => list // always return the list response
  )
}

In my specific case I want to stream to content from the store and on the side I also want to listen for web socket updates. And this would be done in one subscribe.

Note: the ignore stream is responded to via a tap outside of this example (it's listening to webSockets for updates). The list is a stream of content from my ngRx store.


Solution

  • Do a merge with a filter

    merge(of([ {...} ]), of( null ).pipe(filter(() => false)))
    

    It will filter out all emission so it will never contribute to the merge.