These are the most central concepts in Observable and Observers.
Observable
An observable is:
- A pushable collection of multiple values
- Is a stream of data or a source that can arrive over time
- Observables are mostly created from events - e. g. mouse actions, clicks, text input
Usually, we have a Data Provider. Then we have an Observable on this Data Provider.
The Observable pushes to our subscribers / observers.
Observables should be created with an $at the end to highlight it.
Observables only
Creating Observables:
Example
import { Observable } from 'rxjs';
const observable = new Observable(subscriber => {
subscriber.next('Hello');
subscriber.next('World');
subscriber.complete();
});
observable.subscribe({
next(x) { console.log(x); },
complete() { console.log('Done'); }
});
This will log "Hello" and then "World".
After this, completewill close the observable. any other calls of nextwould not push a notification.
Creating observables
Mostly created using functions like of , fromand interval.
When creating an observable, it takes in the subscribe function as the only parameter.