ecmascript-6rxjsreactive-extensions-js

Rx: Run code after all subscriptions have completed for one 'next'


Say I have a subject somewhere in my code

mySubject$ = new Subject()

Elsewhere in the code, any number of subscriptions are made, e.g.

someSubscription = mySubject$.subscribe(() => console.log('I love streams'))
anotherSubscription =  mySubject$.subscribe(() => console.log('me too!'))

When I .next()the subject, both subscription fire as expected.

How can I hook into the rx mechanism so that I can run some code after all subscriptions have finished running their code (for that single next event)?

Is there something like mySubject$.onAllSubscriptionsCompleted(() => console.log('all done')

Or what are alternatives to achieving this?


Solution

  • RxJS doesn't have such backward (upward?) event propagation, unless its a subscription/unsubscription.

    Strictly saying: its not an Rx-way to have that relation between subscriber and an observable.

    Yet, there are ways to achieve that:

    Heres a rough illustration of a flow using concatMap

                                    handler1
    source$ -> concatMap( forkJoin( handler2 ) ) -> subscribe( here all handlers finished )
                                    handler3
    

    If you want to learn more on back pressure, heres an article on backpressure techniques in RxJS