How do we manage time when we are building applications? Particular asynchronous operations.
This is typically where control flow problems come from. We need to communicate that this thing has happened and do it in order.
There is a reference to the asynchronous observables course.
This brings us to the fifth element of programming: observable streams.
Observables give us a powerful way to encapsulate, transport and transform data from user interactions to create powerful and immersive experiences.
Observables are essentially the combination of the Iterator pattern and the Observer pattern.
This allows your to communicate state over time.
We are familiar with getting values over time - most notably with Promises. The limitation of the promise is that it is a single resolution.
The difference with observables is that it will emit multiple values over time.
When you call functions, you "pull" values to you. Promises and observables "push" the value to you.
Observables are challenging, but if you focus on just the observable stream it becomes a lot easier to understand.
It just takes a smaller paradigm shift. Instead of it just being "input" to "output" and the "pulling" of data, we need to shift our paradigm to go to "output" to "input".
Basically it is initial output -> magic -> final input
. This looks like event -> operators -> subscribe
.
How do we preserve state in a stream?
What happens if there are multiple streams? An anology is put together about getting soda from a stream at a fastfood. The example shows a merge
operator that brings together multiple streams.
The example given is that there is a partially applied function that can take an argument from the "event" that will fully apply the function and return the value to then enter the merged stream.
This is done in the examples with the switchMap
operator. The switchMap
swaps to a stream and you can pipe until takeUntil
another stream occurs, then come back to the rest of the upstream.
A subject is a mechanism that creates an observable stream but also produces the mechanisms to control that observable stream.
export class NotificationService { private subject = new Subject(); // create Observable notifications$ = this.subject.asObservable(); dispatch(notification) { this.subject.next(notification); } }