Subject subscription sequence

99 Views Asked by At

I have a replay subject, and it is subscribed in multiple components. I just want to know in what order the subscription goes.

public getValue$ = new ReplaySubject<any>(1);

If I broadcast a value to getValue$ by

getValue$.next(5);

And, let’s say, getValue$ is subscribed in a, b, c and few other components. b is the child component of a and c is a sibling of a. How is the sequence decided?

3

There are 3 best solutions below

0
On

I believe that they will be updated in the order that they subscribed, but I wouldn't trust that is always the case. I imagine that an observable stores its observers in a simple array and just pushes the next subscription to the array. I could be totally wrong about that though.

Writing code that depends on the order of subscriptions though is dangerous and likely to lead to problems. An observable is intended to be asynchronous, so it could be that the order subscriptions are triggered isn't consistent. I would not write code in a way that it assumes or requires that subscriptions would be triggered in any particular order.

If you need to react to new data from multiple observables, consider using RXJS operators built for this purpose, like combineLatest, merge, and forkJoin, to avoid race conditions.

1
On

The order of emission and reception of values in RxJS, including with a ReplaySubject, is generally determined by the order of subscription. In your scenario, where getValue$ is subscribed in components A, B (child component of A), and C (sibling of A), the order of subscription will affect the sequence of value emission to these subscribers.

Here's a breakdown of the likely sequence:

  1. Subscription in Component A: If Component A subscribes to getValue$ first, it will receive any previously emitted values (due to the replay functionality of ReplaySubject) and will continue to receive new values as they are emitted.
  2. Subscription in Component B (child of A): If Component B subscribes next, it will receive the same values as Component A in the order they were emitted. Since ReplaySubject replays the last emitted value to new subscribers, Component B will get the last emitted value, and then any subsequent values as they are emitted.
  3. Subscription in Component C (sibling of A): If Component C subscribes after A and B, it will receive the same values as A and B in the order they were emitted. The replay functionality ensures that new subscribers receive the entire history of values emitted by the subject.

Keep in mind that this sequence is based on the order of subscription. If you subscribe to getValue$ in a different order, the sequence of value emission will reflect that order of subscription. Additionally, the replay functionality ensures that new subscribers receive the specified number of previously emitted values when they subscribe, in this case, the last emitted value due to the (1) parameter in ReplaySubject(1).

0
On

In RxJS, the subscription order does not necessarily determine the order in which values ​​are received by the subscriber. The subscription order mainly affects when the subscriber starts receiving values ​​related to the time of subscription.
In your example, when you broadcast a value with getValue$.next(5), all subscribers (components a, b, c, etc.) will receive that value. The order in which they receive the value depends on when they signed up.

For example:

If the component is registered first, it will receive the value immediately after calling getValue$.next(5).

If component b subscribes after component a, it will receive the value after the next broadcast.

If component c registers at the same time as component a, they will receive the value at the same time.

The hierarchy of components (e.g., parent-child relationships) does not necessarily affect the sequence of value generation. It's more about the registration order.