fromEvent() operator

Now it gets really interesting. The fromEvent() operator allows us to mix a UI event such as a click or a scroll event and turn it into a stream. So far, we have operated under the assumption that asynchronous calls is something that only has to do with AJAX calls. This is far from true. The fact that we can mix UI events with any type of asynchronous calls creates a really interesting situation that allows us to compose really powerful, expressive code. We will touch on this topic further in the coming section, Thinking in streams.

To use this operator, you need to provide it with two arguments: a DOM element and the name of an event, like so:

// creation-operators/fromEvent.js

// we imagine we have an element in our DOM looking like this <input id="id" />
const elem = document.getElementById("input");
const eventStream$ = Rx.Observable
.fromEvent(elem, "click")
.map(ev => ev.key);

// outputs the typed key
eventStream$.subscribe(data => console.log(data));
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.104.120