Bridging To Events
Last updated
Was this helpful?
Last updated
Was this helpful?
RxJS provides factory methods for you to bridge with existing asynchronous sources in the DOM or Node.js so that you can employ the rich composing, filtering and resource management features provided by RxJS on any kind of data streams. This topic examines the and operator that allows "importing" a DOM or custom event into RxJS as an observable sequence. Every time an event is raised, an onNext
message will be delivered to the observable sequence. You can then manipulate event data just like any other observable sequences.
RxJS does not aim at replacing existing asynchronous programming models such as promises or callbacks. However, when you attempt to compose events, RxJS’s factory methods will provide you the convenience that cannot be found in the current programming model. This is especially true for resource maintenance (e.g., when to unsubscribe) and filtering (e.g., choosing what kind of data to receive). In this topic and the ones that follow, you can examine how these RxJS features can assist you in asynchronous programming.
Natively, RxJS supports a number of libraries and hooks into them such as , , , and for using their event system. This behavior, however, can be overridden to only use native bindings only. By default, RxJS also has hooks for EventEmitter
events natively supported.
The following sample creates a simple DOM event handler for the mouse move event, and prints out the mouse’s location on the page.
To import an event into RxJS, you can use the operator, and provide the event arguments that will be raised by the event being bridged. It then converts the given event into an observable sequence.
In the following example, we convert the mousemove event stream of the DOM into an observable sequence. Every time a mouse-move event is fired, the subscriber will receive an onNext
notification. We can then examine the event arguments value of such notification and get the location of the mouse-move.
Notice that in this sample, move becomes an observable sequence in which we can manipulate further. The topic will show you how you can project this sequence into a collection of Points type and filter its content, so that your application will only receive values that satisfy a certain criteria.
Cleaning up of the event handler is taken care of by the Disposable
object returned by the subscribe
method. Calling dispose
will release all resources being used by the sequence including the underlying event handler. This essentially takes care of unsubscribing to an event on your behalf.
The fromEvent
method also supports adding event handlers to multiple items, for example a DOM NodeList. This example will add the 'click' to each element in the list.
If this behavior is not desired, you can override it by setting the Rx.config.useNativeEvents
to true
which will disregard any library for which we support events.
Now we can rewrite a simple mouse drag as the following:
The converted code looks like this while using the fromEventPattern
method. Each function passes in the handler function which allows you to call the on
and off
methods to properly handle disposal of events.
Concepts
In addition, fromEvent
also supports libraries such as , , , and :
In addition, you could easily add many shortcuts into the event system for events such as mousemove
, and even extending to and Events.
Note this is already available in the project, but is small enough for you to implement yourself.
Node.js is also supported such as an :
There may be instances dealing with libraries which have different ways of subscribing and unsubscribing from events. The method was created exactly for this purpose to allow you to bridge to each of these custom event emitters.
For example, you might want to bridge to using jQuery method. We can convert the following code which alerts based upon the click of a table row.
In addition to this normal support, we also support if the addHandler
returns an object, it can be passed to the removeHandler
to properly unsubscribe. In this example, we'll use the and the module.