Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Implement the `map` Operator from Scratch in RxJS

    John LindquistJohn Lindquist
    rxjsRxJS
    ^6.0.0

    While it's great to use the RxJS built-in operators, it's also important to realize you now have the knowledge to write them by yourself if needed. The map operator turns out to be a simple MapSubscriber which takes a function and applies it to the value passed to next.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: As an ironic follow up the previous lesson, let's implement map to see what that would like. I'll instead import our own map file. You can see map is not a function. We'll export const to map, and take the source and return the source. Right now, you'll see map the signature is taking a function.

    Let's take that function as well. You can see with this signature using this operator, we're getting 1, 2, 3, 4, 5, 1, 2, 3, 4, 5. The multiply operator is simply passing those values through and ignoring this function which we're trying to pass in right here.

    Let's do our source lift here, so source.lift. We'll take an object and you can see already it says, "call is not a function," so we need to create call. That's a function which takes in the subscriber at the destination and the source, so we want to source subscribe.

    If I pass in the current subscription, you can see we're still at 1, 2, 3, 4, 5, so nothing is happening, because we didn't create our own map subscriber. Let's do that now. I'll have a map subscriber which extends subscriber. I'll wrap this around this subscriber, so new map subscriber.

    Even with this, we are still at 1, 2, 3, 4, 5, because we haven't overwritten next.anything to it. This is a quick test. We can do it next and take the value, and do this.destination.next try our value * 2. You'll see 2, 4, 6, 8, 10, 2, 4, 6, 8, 10, because we're taking a value coming through and multiplying by 2.

    That's not what we want. We want this function here. I'll go ahead and pass the function in. Up here in the constructor, I can take the subscriber at the destination, whatever you want to call it, and the function, or projection, whatever you want to call that. I'll say super passing that subscriber, and I'll say this.function is the function we passed in.

    Now, I'm just going to call this.next, but I'll invoke the function and pass in the value. Let's save there. You can see you're back to 3, 6, 9, 12 and 4, 8, 12, 16, so multiples of 3 and multiples of 4, because we said multiple of 3 and multiple of 4, because we now have a multiply operator taking in our own custom map operator, which is simply taking in that function, passing it on to a subscriber.

    Inside of a next, we're invoking that function with this current value. I can switch back and forth between the RxJS operators' version. Get save. Get the same result, as if I just take my own version, import map and get the same result.

    Under the hood, if you look at the subscriber, it's really about what you do in this _next, and how you invoke this destination, and the value you pass in that determines what comes out on this side. This destination over here determines this value that's coming through all of this going on here just wiring pieces together.