What is the best way to handle multiple events on the same DOM node in rxjs 5.1?
fromEvent($element, 'event_name')
but I can specify only one event at a time.
I want handle scroll wheel touchmove touchend
events.
What is the best way to handle multiple events on the same DOM node in rxjs 5.1?
fromEvent($element, 'event_name')
but I can specify only one event at a time.
I want handle scroll wheel touchmove touchend
events.
This is how I would implement this in the newest version of RxJs
Note: This is for RxJS v5. See bottom of this answer for the v6 equivalent.
You can use the
Rx.Observable.merge
function to merge multiple observable streams into a single stream:If that seems a little bloated, we might clean up a little by creating an array for the events, and then map that array to Observable objects. This works best if you do not need to reference the events their associated observables separately at some point:
You are now able to handle all events with one single subscription:
Update RxJS v6
In RxJS 6 you can import the standalone
merge
function as equivalent to the static method in v5, and use it the same way:This is how I prefer to combine events: