derivedFrom
derivedFrom
is a helper function that combines the values of Observable
s or Signal
s and emits the combined value.
It also gives us the possibility to change the combined value before emitting it using rxjs operators.
It is similar to combineLatest
, but it also takes Signals
into consideration.
Usage
derivedFrom
accepts an array or object of Observable
s or Signal
s and returns a Signal
that emits the combined value of the Observable
s or Signal
s.
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:
- Combine multiple
Signal
s - Combine multiple
Observable
s - Combine multiple
Signal
s andObservable
s - Using initialValue param
- Use it outside of an injection context
1. Combine multiple Signal
s
We can use derivedFrom
to combine multiple Signal
s into one Signal
, which will emit the combined value of the Signal
s.
At this point we still don’t get any benefit from using derivedFrom
because we can already combine multiple Signal
s using computed
function.
But, what’s better is that derivedFrom
allows us to change the combined value before emitting it using rxjs operators (applying asynchronous operations), which is not possible with computed
.
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 c
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 Observable
s
We can use derivedFrom
to combine multiple Observable
s into one Signal
, which will emit the combined value of the Observable
s.
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 Signal
s and Observable
s
This is where derivedFrom
shines. We can use it to combine multiple Signal
s and Observable
s into one Signal
.
But, we can always use the startWith
operator to change the initial value.
4. Using initialValue param
Or you can pass initialValue
to derivedFrom
in the third argument 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, derivedFrom
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.