Exploring Major Concepts In RxJS
Last updated
Was this helpful?
Last updated
Was this helpful?
This topic describes the major Reactive Extensions for JavaScript (Rx) objects used to represent observable sequences and subscribe to them.
The Observable
/ Observer
objects are available in the core distribution of RxJS.
Observable
/ Observer
##Rx exposes asynchronous and event-based data sources as push-based, observable sequences abstracted by the object in the core distribution of RxJS. It represents a data source that can be observed, meaning that it can send data to anyone who is interested.
As described in , the other half of the push model is represented by the object, which represents an observer who registers an interest through a subscription. Items are subsequently handed to the observer from the observable sequence to which it subscribes.
In order to receive notifications from an observable collection, you use the method of Observable
to hand it an Observer
object. In return for this observer, the method returns a Disposable
object that acts as a handle for the subscription. This allows you to clean up the subscription after you are done. Calling dispose
on this object detaches the observer from the source so that notifications are no longer delivered. As you can infer, in RxJS you do not need to explicitly unsubscribe from an event as in the common JavaScript event model.
Observers support three publication events, reflected by the object's methods. The can be called zero or more times, when the observable data source has data available. For example, an observable data source used for mouse move events can send out an event object every time the mouse has moved. The other two methods are used to indicate completion or errors.
The following lists the Observable
/ Observer
objects in addition to the Disposable
object.
RxJS also provides subscribe
capabilities so that you can avoid implementing the Observer
object yourself. For each publication event (onNext
, onError
, onCompleted
) of an observable sequence, you can specify a function that will be invoked, as shown in the following example. If you do not specify an action for an event, the default behavior will occur.
Concepts
Other Resources
You can treat the observable sequence (such as a sequence of mouse-over events) as if it were a normal collection. Thus you can write queries over the collection to do things like filtering, grouping, composing, etc. To make observable sequences more useful, the RxJS libraries provide many factory operators so that you do not need to implement any of these on your own. This will be covered in the topic.
You do not need to implement the Observable/Observer objects yourself. Rx provides internal implementations of these interfaces for you and exposes them through various extension methods provided by the Observable and Observer types. See the topic for more information.