Check out a free preview of the full Rx.js Fundamentals course

The "Events" Lesson is part of the full, Rx.js Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Steve discusses the event helper function fromEvent, which creates an observable based on event listeners. A brief discussion regarding the bindCallback and fromFetch functions is also provided in this segment.


Transcript from the "Events" Lesson

>> In a lot in a lot of cases, right, once that page loads, trying to think of all the things that could happen that effectively cause change on the page. API requests, any kind of event a mouse move, click, whatever you've registered. Click event listeners for some of the more common ones that will kind of come through if you are getting fancy with client side routing, that could be another one.

Right, you have a stream of route changes, right? But generally speaking, events are a pretty common one. If you think about under the hood prior to the Fetch API, right, XML HTTP requests had events, your route changes, probably listening to events. Events are basically the bread and butter of our actual UIs.

So just like we have have of and from, we have one called fromEvent. And it creates an observable from an event. And it's looks somewhat similar to an event listener, right? So on top, I have basically an event listener where we addEventListener click and we say console.login. With our observable, we separate those two things.

We separate the creation of the subscription to the event, and then what we want to do with it, right? And so again, this gives us the ability to basically have one event listener that we can use multiple places, different things can subscribe to those events. Like I said before, one of the interesting parts about observables is that each get their own subscriptions, 0, 1, 2, 3, 4.

In this case, those will be the events as they come through, right? And one of the things we can kind of take a look at later is there are ways to kind of replay events that have happened, if you need to catch up or stuff along those lines there are operators that will allow you to do that.

But generally speaking, this allows you to separate what to do with a subscription from the subscription itself. So it does take, if you do need to modify that event at all though, and you wanna say, okay, listen, I always want the event target value in this case, you can pass it a function that will go and do something to that event upon subscribing to it.

However later we'll see that you can take observables, you can map them. A lot of things you can do with arrays, you can also do with observables. Just like in the fourth dimension effectively and have some of those in place. There are a few more that we're not gonna get into today cuz they don't necessarily apply to us in quote unquote front end engineering.

But they are kind of useful and I just wanna put into your brain, so that you're aware that they exist, so you don't try to write them yourself. There is one called bindCallback. And this is super useful if you're using node js, for instance. Right, which basically deals with, you know how node takes the errors, the first argument and then the results is the second argument.

It's basically a helper function that will take the kind of node style of callbacks and turn it into an observable for you. There is another one that's kind of in its own separate module in rxjs, called fromFetch. You can take a lucky guess what it does, is it creates an observable using the Fetch API, right?

You're like isn't that a promise? Yes, but it's a promise that has a few interesting things like the ability to abort the request, so on and so forth. So it depends, if you're using a library like Axios or what have you, you might choose to just use from promise.

If you're using the Fetch API directly, then you might choose to use this as well. Yes, a lot of times a lot of our API fetching libraries tend to be promises and you can use from and create the promise. But this is also another option if you If you're using the Fetch API directly.

We will use this one later today, we will not use bind callback cuz there's just not a use case for it.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now