Extend Subscriber to Override `_next` in RxJS

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 6 years ago

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.

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.