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

    Extend Subscriber to Override `_next` in RxJS

    John LindquistJohn Lindquist
    rxjsRxJS
    ^6.0.0

    The Subscriber class exposes a _next method which you can override to handle how the destination's next function will be called. This allows you to create your own variations of the Subscriber class to intercept what happens between the observable and the destination subscriber.

    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
    Transcript

    Transcript

    Instructor: 00:00 Import subscriber. Then this is a class which we can extend. I'll call this double subscriber. Extends subscriber. Then I'm simply going to implement an _next function, which takes a value and then with that value calls this destination, next. I'm going to pass in that value times two.

    00:27 With this double subscriber, which is not being used yet, I'm going to subscribe to a new instance of the double subscriber and pass in our subscriber from above.

    00:42 You can see up here that the values are now 2, 4, 6, 8, 10. The values coming into here, if I just log these out, are one, two, three, four, five. Then they get doubled and passed down to this subscriber.

    00:56 This destination is simply this subscriber. This double subscriber is wrapping that subscriber and transforming that value before it passes it on.

    Discuss

    Discuss