1 00:00:00,120 --> 00:00:06,540 In this lecture, we are going to look at one of the most useful operators from our SJS called the Tap 2 00:00:06,540 --> 00:00:07,290 operator. 3 00:00:07,560 --> 00:00:10,530 It's an operator purely for debugging a pipeline. 4 00:00:10,830 --> 00:00:15,180 It does not affect a stream or the values emitted from an observable. 5 00:00:15,450 --> 00:00:21,810 We can insert the tap operators throughout our pipeline for debugging our operators by checking their 6 00:00:21,810 --> 00:00:22,560 values. 7 00:00:23,040 --> 00:00:29,070 The Tap operator will be given the value emitted by the previous observable or operator. 8 00:00:29,400 --> 00:00:34,140 We can do whatever we want with the value if we attempt to change the value. 9 00:00:34,290 --> 00:00:35,700 The changes are ignored. 10 00:00:36,060 --> 00:00:39,000 It's not an operator meant to impact a value. 11 00:00:39,990 --> 00:00:46,770 Let's use our operator to spy on the values emitted by the interval operator without affecting the underlying 12 00:00:46,770 --> 00:00:47,370 stream. 13 00:00:47,670 --> 00:00:53,970 First, let's import the tap operator from the Oryx J.S. slash operators package. 14 00:00:56,430 --> 00:01:02,370 Next, we will add the tap operator in between the take and scan operators. 15 00:01:04,780 --> 00:01:09,700 Lastly, let's replace the scan operator with the Reduce operator. 16 00:01:12,350 --> 00:01:17,120 In this scenario, we may not want to view the accumulated value over time. 17 00:01:17,570 --> 00:01:22,160 Instead, we may want the total after the observable has been completed. 18 00:01:22,520 --> 00:01:29,280 However, we may want to see the values push from the observable without having to change the operator. 19 00:01:29,360 --> 00:01:33,260 We can apply the tap operator to spy on values. 20 00:01:33,740 --> 00:01:37,670 The Tap operator accepts a function with a supplied value. 21 00:01:37,970 --> 00:01:41,120 Let's log the value from our operator. 22 00:01:43,640 --> 00:01:50,170 In the console, the value emitted by the interval should be logged by using the tap operator. 23 00:01:50,210 --> 00:01:54,080 We've added a checkpoint for viewing the values in our pipeline. 24 00:01:54,680 --> 00:02:00,110 The power of the tap operator can be incredibly helpful for complex operators. 25 00:02:00,380 --> 00:02:04,820 At the moment, we've been writing pipelines with one or two operators. 26 00:02:05,060 --> 00:02:06,260 What if we had 10? 27 00:02:06,860 --> 00:02:09,320 It's possible we might run into problems. 28 00:02:09,560 --> 00:02:15,740 The tap operator can debug the values from various operators as they're being passed through. 29 00:02:16,100 --> 00:02:21,920 One thing to keep in mind is that the tap operator does not affect the underlying stream of data. 30 00:02:22,280 --> 00:02:26,300 If we attempt to change the value, those changes are ignored. 31 00:02:26,600 --> 00:02:29,510 We have limited power with the tap operator. 32 00:02:29,840 --> 00:02:35,690 At the same time, we can manipulate the value without consequence to the overall stream. 33 00:02:36,260 --> 00:02:40,100 There's one last point I want to mention about the tap operator. 34 00:02:40,370 --> 00:02:47,570 Instead of passing in a function, we can pass in an object with the next complete and error functions. 35 00:02:47,900 --> 00:02:54,140 We can secretly insert an observer like object without having to subscribe to the observable. 36 00:02:54,500 --> 00:02:55,670 Let's give it a try. 37 00:02:55,970 --> 00:02:58,250 Wrap the function with curly brackets. 38 00:03:00,700 --> 00:03:04,510 Next, assign the function to a property called Next. 39 00:03:06,950 --> 00:03:10,520 Inside the console, we get the same effect as before. 40 00:03:10,820 --> 00:03:13,460 That's the tap operator, in a nutshell. 41 00:03:13,700 --> 00:03:20,030 In the next set of lectures, we will start getting into more advanced operators, which is why we began 42 00:03:20,030 --> 00:03:22,010 discussing the tap operator. 43 00:03:22,340 --> 00:03:26,210 It's going to help us understand the next set of operators.