In this lecture, we are going to talk about the most fundamental concept in our SJS called an observable
understanding observables is crucial to moving forward with our SJS.
So what are observables?
You can think of an observable as a wrapper around a data source.
Data can come from an asynchronous operation.
Mouse events, keyboard events, file uploads and CTP requests are examples of data sources other than
being asynchronous.
It's not definitive as to when data is emitted from these sources, when data is emitted from these
sources.
We can capture the data before handing it off.
Wherever it's needed and observable gives us the opportunity to filter sorts and coordinate data.
It addresses the problem we discussed earlier.
By wrapping a data source, we can further refine the data.
We call them observables because we can observe the data emitted from a source who would have thought
right.
We can observe observables with objects we call them observers.
Observers are responsible for receiving data after an observable has emitted the data.
An observer establishing a connection with an observable is called a subscription.
One of the most interesting features about our SJS is being able to have multiple observers subscribe
to an observable by allowing multiple subscriptions.
Different locations in our app can be fed the same stream of data.
If we format data from within an observable, every observer will be given the same formatted data.
Observers never have to worry about how the data has been formatted.
This relationship allows us to separate responsibilities.
Observables don't care what the observers do with the data after it has been pushed.
Let's give observables a try.
We will be working on these script file with the server turned on unless I state otherwise, the server
should always be running at the top of the file.
We will import a class called observable from the R js package.
The observable class will help us with creating a new observable.
Below we will create a variable called observable.
Its value will be a new instance of the observable class without adding anything else we created in
observable.
In its simplest form, an observable is a new instance of the observable class after creating the observable.
We need to store a reference to it.
The instance will have a function called Subscribe.
Let's call it.
The subscribed function will allow us to pass an observer, as I mentioned before, observers are objects.
Let's pass in an object.
Inside this object, we must define a function called next.
The next function is responsible for handling data pushed from the observable.
This function will have one argument called value.
The value argument refers to the data emitted by the observable inside the function.
We will log the value argument.
There's one last step we need to perform at the moment.
Our observable doesn't emit data.
Observers will not receive data regardless of a subscription.
We should update our observable to push data to our observers in the new instance of the observable
class.
We can pass in a function.
This sanction is where we can emit data to observers.
The question is how the function will have an argument called subscriber.
The subscriber argument is an object for interacting with observers.
We can emit data, throw errors or tell observers we're finished emitting data.
For this example, we will emit some data inside the function.
We will run the subscriber dot next function.
We can pass in a value to emit into the next function.
We will pass on a simple message.
After making those changes, let's check out the console in the developer tools, we will find the message
logged in the console.
We've created our first observable and observer observables are responsible for managing data.
There's one last thing I want to mention.
You may have noticed it already.
The relationship between observables and observers is push based.
Observers don't have control over when data is emitted by an observable.
It's up to an observable to push data.
If we don't call the next function, the observer function will never run in the next lecture.
We are going to dive deeply into how we can create observers.