Operators Flashcards
What are combination operators in Rxjs
The combination operators allow the joining of information from multiple observables. Order, time, and structure of emitted values is the primary variation among these operators.
Some commonly used combinatory operators.
CombineLatest Concat Merge StartWith WithLatestFrom Zip
What is CombineLatest used for
This operator is best used when you have multiple, long-lived observables that rely on each other for some calculation or determination. combineLatest will not emit an initial value until each observable emits at least one value.
For the very first time, all of the values should have emitted at least once, from that point, any change in one of the values will cause the observable to emit
Explain Concat operator
When multiple observables are used in this method, the
subscription to the subsequent observables will happen only after the previous ones have completed
Explain Merge Operator
The output isn’t in the same order as subscribed. Whichever emits first will have its value passed to the subscribe method.
Explain Start With
It takes an initial value and when piped with other observables, the initial value will be the one emitted first
Explain WithLatestFrom
(Master/Slave)we have source and the value passed to WithLatestFrom.
They are in a kind of Master/Slave relationship. After the slave emits for the first time, master takes over. After that the subscription fires only when the Master changes, with the value of Master and whatever value is available from the Slave at that time
Explain Zip Operator
(Love Birds)For example:There is a Source 1 and Source 2.When they are zipped and subscribed to, the subscription will yield only when the values in source 1 and source 2 have changed in a pairing fashion.
what are Transformative operators
Transforming values as they pass through the operator chain is a common task. These operators provide transformation techniques for nearly any use-case you will encounter.
Commonly used transformation operators
BufferTime ConcatMap Map MergeMap/FlatMap scan SwitchMap
Explain BufferTime
Collect emitted values until provided time has passed, emit as array.
Explain ConcatMap
ConcatMap subscribes to the inner Observable for you. But unlike switchMap, that unsubscribes from the current Observable if a new Observable comes in, concatMap will not subscribe to the next Observable until the current one completes. The benefit of this is that the order in which the Observables are emitting is maintained
Explain Map
Similiar to select in C#.
Apply projection with each value from source.
Explain MergeMap /FlatMap
Used when the order of emission is not important.Similar to Merge in Combinations.
This operator is best used when you wish to flatten an inner observable but want to manually control the number of inner subscriptions.
When we have an outer observable and an inner Observable, we don’t have to call subscribe twice on the outer and the inner values. MergeMap takes care of it for us.It is possible to have multiple active subscriptions at a time with MergeMap
Explain Switch Map
Switch map will come in handy if you compose a list of filters into a data stream and perform an API call when a filter is changed. If the previous filter changes are still being processed while a new change is already made, it will cancel the previous subscription and start a new subscription on the latest change