Use `lift` to Connect a `source` to a `subscriber` in RxJS

John Lindquist
InstructorJohn Lindquist

Share this video with your friends

Send Tweet
Published 4 years ago
Updated 3 years ago

The lift method on each source hides away the internals of RxJS so you can simply connect a source to the subscriber you're working with. The lift method take an object with a call function with subscriber and source arguments, then it's up to you how you want to connect them together.

Instructor: [00:00] I'm going to grab this entire function, cut it out, and call it, just double, and assign double to that, and pass double into here. Now, everything is working as expected.

[00:13] We get 1, 2, 3, 4, 5 inside of our subscriber which is passed along to the destination, and doubled, so our destination is here, 2, 4, 6, 8, 10. You can see this observable is piping through the double function and properly passing those values down to the subscriber.

[00:31] Now, inside of here, where I said don't do it this way. I said that, because there is a convenience function called source.lift. If I cut out this operator object, and instead of returning this block, I'm going to return source.lift and paste in that operator object.

[00:50] You can see we're back to where we were, where we have 2, 4, 6, 8, 10 and 1, 2, 3, 4, 5, but now we don't have to jump through the hoops of creating the observable, assigning the source, assigning the operator.

[01:01] All we have to do is a say source lift, and pass in as object that has a call on it. Then, when subscribe is invoked, it will check for this object and set up all of that stuff under the hood for us, so the before looks like this where we created, assign the source, assign the operator, and return it which is the exact same thing as just source lift.

[01:24] Then, passing in that operator array object that has call on it. For the call takes the subscriber and the source and wires it up for you, and gives you that opportunity to pass in your custom subscriber.

Ender Akay
Ender Akay
~ 4 years ago

chall(sub, source) should be call(sub,source) right?

J. Matthew
J. Matthew
~ 3 years ago

Yep.