Refreshing the major interfaces in RX

Source: Internet
Author: User

This article describes the primary reactive extensions (RX) interfaces used to represent the observable sequences and subscribe them.

Iobservable <t>/iobserver <t> appears in the. NET 4.0 base class library. They can also be installed as a. Net 3.5, Silverlight 3 and 4, and JavaScript package.

Iobservable <t>/iobserver <t>

RX exposes asynchronous and event-based data sources as push-based, observable sequences specified acted by the new iobservable <t> interface in. net Framework 4.0. this iobservable <t> interface is a dual of the familiar ienumerable <t> interface used for pull-based, enumerable collections. it represents a data source that can be observed, meaning that it can send data to anyone who is interested. it maintains a list of dependent iobserver <t> implementations representing such interested listeners, and notifies them automatically of any state changes.

RX exposes asynchronous and event-based data sources as the basis of the push mode. The observable sequence is abstracted by the iobservable <t> interface in. NET Framework 4.0. The iobservable <t> interface is very similar to the ienumerable <t> interface, but the ienumerable <t> interface is used for pull-based and can enumerate sets. The iobservable <t> interface represents the data source that can be observed-that is, it can send data to any object that follows him. It maintains a list of objects dependent on iobserver <t> implementation, such as listeners that follow the data source and automatically notifies them of any state changes.

An Implementation of the iobservable <t> interface can be viewed as a collection of elements of type T. therefore, an iobservable <int> can be viewed as a collection of integers, in which integers will be pushed to the subscribed observers.

The implementation of the iobservable <t> interface can be displayed as a set of element T. Therefore, iobservable <int> can be considered as an integer set in which integers are pushed to each subscribed observers.

As described in what is RX, the other half of the push model is represented by the iobserver <t> interface, which represents an observer who registers an interest through a submodules. items are subsequently handed to the observer from the observable sequence to which it subscribes.

As described in what is RX, the other half of the push mode is represented by the iobserver <t> interface. It represents an observer, and it registers the focus (interest) through the sublistener ). Item is then handed over from the observable sequence to the observer that subscribes to it.

In order to receive configurations from an observable collection, you use the subscribe method of iobservable to hand it an iobserver <t> object. in return for this observer, the subscribe method returns an idisposable object that acts as a handle for the sublistener. this allows you to clean up the subtasks after you are done. calling dispose on this object detaches the observer from the source so that communications are no longer delivered. as you can infer, in RX you do not need to explicitly unsubscribe from an event as in. net event model.

To receive notifications from the observable set, you must use the subscribe method of the iobservable interface to hand over iobservable to an iobserver <t> object. In the return of this observer, the subscribe method returns an idisposable object, which represents the handle of this subscribe. This allows you to clear subdomains after processing. Calling the dispose method on this object will separate the observer from the source, so that the notification will not be transmitted to it. As you can infer, in RX, you do not need to explicitly unsubscribe events as in the. NET event model.

Observers support three publication events, reflected by the interface's methods. onnext 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 a point object every time the mouse has moved. the other two methods are used to indicate completion or errors.

Observers supports three public events, all of which are reflected by interface methods. Onnext can be called 0 times or multiple times. When the observable data source has valid data. For example, the observable data source used for the mouse move event can send a point object each time the mouse moves. The other two methods are used to indicate the completion or error status.

The following lists the iobservable <t>/iobserver <t> interfaces.

The following is the iobservable <t>/iobserver <t> interface definition.

public interface IObservable<out T> { IDisposable Subscribe(IObserver<T> observer); } public interface IObserver<in T> { void OnCompleted(); // Notifies the observer that the source has finished sending messages.void OnError(Exception error); // Notifies the observer about any exception or error.void OnNext(T value); // Pushes the next data value from the source to the observer.} 

RX also provides subscribe extension methods so that you can avoid implementing the iobserver <t> interface yourself. for each publication event (onnext, onerror, oncompleted) of an observable sequence, you can specify a delegate 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.

RX also provides the subscribe extension method, so you can avoid implementing the iobserver <t> interface yourself. For each public event (onnext, onerror, oncompleted) of the observable sequence, you can specify a delegate and it will be called when appropriate, as shown in the following example. If you do not specify an action for the event, the default action will occur.

IObservable<int> source = Observable.Range(1, 5); //creates an observable sequence of 5 integers, starting from 1IDisposable subscription = source.Subscribe(x => Console.WriteLine("OnNext: {0}", x), //prints out the value being pushedex => Console.WriteLine("OnError: {0}", ex.Message),() => Console.WriteLine("OnCompleted"));

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 LINQ queries over the collection to do things like filtering, grouping, composing, etc. to make observable sequences more useful, the RX Assemblies provide provided factory LINQ operators so that you do not need to implement any of these on your own. this will be covered in the querying observable sequences using LINQ operators topic.

You can treat observable sequence like a normal set (for example, a mouse over event sequence ). Therefore, you can write a LINQ query on this set and execute such queries as filtering, grouping, and combination. To make the observable sequences more useful, the RX Assembly provides many factory LINQ operations. For more information, see querying observable sequences using LINQ operators.

Caution:

You do not need to implement the iobservable <t>/iobserver <t> interfaces yourself. RX provides internal implementations of these interfaces for you and exposes them through varous extension methods provided by the observable and observer types. see the creating and querying observable sequences topic for more information.

Note: you do not need to implement the iobservable <t>/iobserver <t> interface on your own. RX provides the internal implementation of these interfaces and exposes them through various extension methods of the observable and observer types. For details, refer to creating and querying observable sequences.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.