mergeFrom is a helper function that merges the values of Observables or Signals and emits the latest emitted value.
It also gives us the possibility to change the emitted value before emitting it using RxJS operators.
It is similar to merge(), but it also takes Signals into consideration.
From ngxtension perspective, mergeFrom is similar to derivedFrom, but it doesn’t emit the combined value, but the latest emitted value by using the merge operator instead of combineLatest.
Usage
mergeFrom accepts an array of Observables or Signals and returns a Signal that emits the latest value of the Observables or Signals.
By default, it needs to be called in an injection context, but it can also be called outside of it by passing the Injector in the third argument options object.
If your Observable doesn’t emit synchronously, you can use the startWith operator to change the starting value, or pass an initialValue in the third argument options object.
It can be used in multiple ways:
Merge multiple Signals
Merge multiple Observables
Merge multiple Signals and Observables
Using initialValue param
Use it outside of an injection context
1. Merge multiple Signals
We can use mergeFrom to merge multiple Signals into one Signal, which will emit last value of the Signals.
As we said before, mergeFrom allows us to change the last value before emitting it using rxjs operators (applying asynchronous operations).
This will throw an error because the operation pipeline will produce an observable that will not have a sync value because they emit their values later on, so the resulting lastRunner signal doesn’t have an initial value, and this causes the error.
You can solve this by using the initialValue param in the third argument options object, to define the starting value of the resulting Signal and prevent throwing an error in case of real async observable.
This works, and you can copy the above example inside a component constructor and see the result in the console:
Another way to solve this problem is using the startWith rxjs operator in the pipe to force the observable to have a starting value like below.
The console log will be:
2. Combine multiple Observables
We can use mergeFrom to combine multiple Observables into one Signal, which will emit the combined value of the Observables.
This is just a better version of:
And it can be used in the same way as in the previous example with rxjs operators.
3. Combine multiple Signals and Observables
We can use it to combine multiple Signals and Observables into one Signal.
Using startWith operator aproach to change the initial value.
Or, we can set initialValue as the third argument operator to change the initial value.
4. Using initialValue param
Or, you can set the initialValue as the third argument in the options object, to define the starting value of the resulting Signal and prevent throwing error in case of observables that emit later.
5. Use it outside of an injection context
By default, mergeFrom needs to be called in an injection context, but it can also be called outside of it by passing the Injector in the third argument options object.