1
00:00:03,670 --> 00:00:05,900
Hello, everyone, and welcome back.

2
00:00:05,920 --> 00:00:11,890
In this lesson, we are going to continue to dive deeper into angular on pushing detection and see how

3
00:00:11,890 --> 00:00:15,330
this change detection works with observable streams.

4
00:00:15,460 --> 00:00:21,170
Now, in the previous example we were using here the courses file containing here's some data we were

5
00:00:21,170 --> 00:00:23,960
applying here, the data directly on the template.

6
00:00:24,070 --> 00:00:28,010
Let's now refactor this and fetch the data from the back end.

7
00:00:28,210 --> 00:00:30,800
Again, this will give us an observable data stream.

8
00:00:30,820 --> 00:00:36,770
We're going to pass it to the template and see how angular on pushchairs detection handles that.

9
00:00:36,910 --> 00:00:38,470
So let's remove here.

10
00:00:38,470 --> 00:00:41,230
This course is variable and let's define here.

11
00:00:41,380 --> 00:00:47,560
The course is observable like before, which is an observable, of course, array, meaning that the

12
00:00:47,560 --> 00:00:51,580
value emitted by these observable is an array of courses.

13
00:00:51,610 --> 00:00:55,940
Now let's populate the courses variable here on engy on init.

14
00:00:55,990 --> 00:01:02,440
We are going to assign the value returned here by the course service whenever we call the low with courses

15
00:01:02,440 --> 00:01:03,040
function.

16
00:01:03,190 --> 00:01:05,800
Now we want to display this data on the screen.

17
00:01:05,800 --> 00:01:11,260
Let's for that first remove here the implementation of unpadded course because we are getting here a

18
00:01:11,260 --> 00:01:15,930
compilation error and we are going to see how can we display this data on the screen.

19
00:01:15,940 --> 00:01:21,000
Let's take the courses observable and let's use it here directly in the template.

20
00:01:21,010 --> 00:01:27,160
So just like we have done before, we are going to apply here NGF and we are going to take the courses

21
00:01:27,160 --> 00:01:30,280
observable and we are going to apply it to the async pipe.

22
00:01:30,460 --> 00:01:35,560
We are going to take the result of evaluating this expression and we are going to define this as being

23
00:01:35,560 --> 00:01:38,160
the courses local template variable.

24
00:01:38,210 --> 00:01:39,070
Let's try this out.

25
00:01:39,100 --> 00:01:42,040
If we were lower the application, we are going to see that again.

26
00:01:42,040 --> 00:01:45,320
We have here our data working as expected.

27
00:01:45,370 --> 00:01:51,250
Let's remember that in these implementation, we are using here normal change detection here at the

28
00:01:51,250 --> 00:01:53,000
level of the application component.

29
00:01:53,140 --> 00:01:58,720
Let's see if we would still get this list of courses on the screen, if we would use, for example,

30
00:01:58,720 --> 00:02:00,530
on push change detection.

31
00:02:00,550 --> 00:02:06,010
So if we try this out, we are going to see that we still have here the data being displayed correctly

32
00:02:06,010 --> 00:02:06,790
on the screen.

33
00:02:06,820 --> 00:02:13,930
This means that unposed change detection will look not only at the inputs of each component to determine

34
00:02:13,930 --> 00:02:17,620
if something has changed and the component needs to be rendered.

35
00:02:17,620 --> 00:02:24,430
But on pushchairs, detection will also look at any observable streams that are registered in the template

36
00:02:24,430 --> 00:02:30,520
using the async pipe and see if a new value got emitted by the registered observable.

37
00:02:30,700 --> 00:02:36,370
This means that with on push change detection, there are two ways to push data to a component, either

38
00:02:36,370 --> 00:02:42,880
via input properties of the component or via observable streams via the async pipe.

39
00:02:42,880 --> 00:02:48,400
Both ways of pushing data to the component will trigger the rendering of the component.

40
00:02:48,460 --> 00:02:54,320
Let's now see what will happen in the case of push change detection if we would not be using here via

41
00:02:54,320 --> 00:02:55,200
a sync pipe.

42
00:02:55,210 --> 00:03:00,970
So let's see that instead of having here, this course is observable directly being passed to the template

43
00:03:00,990 --> 00:03:07,640
that instead we will have here at course's member variable D member variable is of course, array.

44
00:03:07,660 --> 00:03:10,860
So this is the variable that we are going to be passing to the template.

45
00:03:10,990 --> 00:03:16,960
Now, instead of assigning this call to the courses observable, let's instead take the return, the

46
00:03:16,960 --> 00:03:19,740
observable and we we're going to subscribe to it directly.

47
00:03:19,900 --> 00:03:25,330
We are going to receive here the courses value each time that the observable emits a value.

48
00:03:25,330 --> 00:03:31,490
And we are going to take this value and we are going to assign it to our new member variable courses.

49
00:03:31,510 --> 00:03:35,070
So in order to avoid any confusion, let's comment out here.

50
00:03:35,080 --> 00:03:37,240
The course is observable member variable.

51
00:03:37,240 --> 00:03:42,310
So we have just refactor our components so that we don't have here and observable member variable.

52
00:03:42,310 --> 00:03:45,310
Instead, we have a plain member variable.

53
00:03:45,310 --> 00:03:50,230
It's just an array of courses and we're going to try to pass these to the component, just like we were

54
00:03:50,230 --> 00:03:51,020
doing before.

55
00:03:51,130 --> 00:03:57,580
So instead of using here the async pipe, we are simply going to come out this expression and we are

56
00:03:57,580 --> 00:04:03,370
going to see that the courses member variable is already available here at the level of the component.

57
00:04:03,520 --> 00:04:07,480
This means that if we try this out, we could expect to see the same result.

58
00:04:07,480 --> 00:04:09,310
But let's see what happens.

59
00:04:09,520 --> 00:04:15,280
So if we reload here, the application, we can see that we have no data getting filled in on the screen,

60
00:04:15,400 --> 00:04:18,839
even though we are populating here, the courses member variable.

61
00:04:18,940 --> 00:04:20,490
So what is going on here?

62
00:04:20,500 --> 00:04:23,980
Why is the data not getting displayed this time around?

63
00:04:24,010 --> 00:04:28,760
Well, the problem is that this component is using on push change detection.

64
00:04:28,810 --> 00:04:34,540
Again, we fall into a situation where even though it's more performant to use on push, we run into

65
00:04:34,540 --> 00:04:37,630
situations where the view does not display the data.

66
00:04:37,640 --> 00:04:42,910
So we are going to comment out here on push change detection or you're going to see if this would work

67
00:04:42,910 --> 00:04:44,710
with default change detection.

68
00:04:44,710 --> 00:04:47,290
And indeed, it does work correctly.

69
00:04:47,440 --> 00:04:53,200
So the way that default change detection works is every expression here on the template is going to

70
00:04:53,200 --> 00:04:55,990
get evaluated, see if there were any changes.

71
00:04:56,200 --> 00:05:00,200
This includes here the expression path to energy for which is the.

72
00:05:00,410 --> 00:05:06,470
Racecourses, because whenever the data arrives from the weekend, we assign it here to the cast member

73
00:05:06,470 --> 00:05:12,800
variable, we are going to do a switch of the value of these variable from undefined to a value containing

74
00:05:12,800 --> 00:05:18,200
an array of courses that just arrived from the backend that is going to trigger change detection, which

75
00:05:18,200 --> 00:05:22,490
means that the ANGULAR is going to render the template of this component.

76
00:05:22,490 --> 00:05:25,430
And then we see the data on the screen again.

77
00:05:25,430 --> 00:05:32,390
We can see the default change detection just works in the larger variety of scenarios without us having

78
00:05:32,390 --> 00:05:34,640
to reason about the mechanism itself.

79
00:05:34,730 --> 00:05:38,760
Let's not turn on on push again at the level of the application component.

80
00:05:38,840 --> 00:05:40,650
So again, the data is gone.

81
00:05:40,760 --> 00:05:47,600
What happens here is that the component is not getting notified in any way that it needs to be rendered.

82
00:05:47,630 --> 00:05:51,160
So we on push that can happen in three different ways.

83
00:05:51,170 --> 00:05:56,660
This could happen if an event would occur at the level of the application, either a DOM event that

84
00:05:56,660 --> 00:06:01,010
we have registered using the angular template syntax or a custom event.

85
00:06:01,070 --> 00:06:06,020
Another way that we could notify the application component that it needs to be rendered would be if

86
00:06:06,020 --> 00:06:08,020
some input data has changed.

87
00:06:08,030 --> 00:06:14,500
But this component here has no input member variables such as, for example, the cost curve component,

88
00:06:14,610 --> 00:06:20,300
the first way that the application component could be notified that it needs to be updated when using

89
00:06:20,300 --> 00:06:26,260
on Bush would be if unobservable registered with the sync pipe would trigger a new value.

90
00:06:26,360 --> 00:06:32,820
And these observable here that we are using is not being passed to the template using the sync pipe.

91
00:06:32,840 --> 00:06:39,440
So in summary, there is no way for the application component using unpunched change detection to know

92
00:06:39,440 --> 00:06:42,620
that it needs to be rendered with on Bush.

93
00:06:42,620 --> 00:06:47,290
We are no longer evaluating all the expressions here in the template.

94
00:06:47,300 --> 00:06:53,440
Instead, we are pushing data to the component, either via inputs or via the async by.

95
00:06:53,490 --> 00:06:57,080
So the component does not get updated in this case.

96
00:06:57,080 --> 00:07:04,100
If we know refactor our implementation to use again the async pipe by removing here the call to subscribe

97
00:07:04,250 --> 00:07:10,640
and then applying here the previous NGF expression with the use of the async pipe to the template.

98
00:07:10,640 --> 00:07:15,710
Then this time around, after reloading the application, we are going to see that we get the data again

99
00:07:15,710 --> 00:07:18,240
and that is because we are using the async pipe.

100
00:07:18,260 --> 00:07:25,370
So in summary, if you were working with observable data streams and you are using on change detection

101
00:07:25,370 --> 00:07:30,170
in some of your components, make sure to use the sync pipe as much as possible.

102
00:07:30,320 --> 00:07:36,710
This is one of the multiple benefits of using the async pipe it makes on push that much easier to reason

103
00:07:36,710 --> 00:07:37,130
about.

104
00:07:37,280 --> 00:07:43,880
And of course, the sync by will also take care of and registering from any observables when the component

105
00:07:43,880 --> 00:07:44,750
gets destroyed.

106
00:07:44,930 --> 00:07:48,070
So there are many advantages for using the async pipe.

107
00:07:48,260 --> 00:07:53,480
Let's now quickly summarize everything that we have learned about on push change detection.

108
00:07:53,570 --> 00:07:57,890
As we have seen in these examples on change detection is faster.

109
00:07:57,890 --> 00:08:01,490
It's more performance, but it's also trickier to use default.

110
00:08:01,490 --> 00:08:03,440
Change detection just works.

111
00:08:03,440 --> 00:08:05,360
We don't have to think about it.

112
00:08:05,360 --> 00:08:11,270
It simply keeps the data in sync with the view at all times in a transparent way.

113
00:08:11,420 --> 00:08:17,810
If by some reason you have to display a lot of data on the screen and you are running into performance

114
00:08:17,810 --> 00:08:24,170
issues while doing so, then you might consider using on push to solve that problem if by some reason

115
00:08:24,170 --> 00:08:26,040
you decide to use on push.

116
00:08:26,060 --> 00:08:28,850
Here are a couple of important recommendations.

117
00:08:28,880 --> 00:08:33,909
When working with observable data streams, use the sink pipe as much as possible.

118
00:08:33,950 --> 00:08:39,440
This is going to reduce the number of cases that you will run into where the view is not getting updated

119
00:08:39,440 --> 00:08:42,049
to reflect the latest changes in the data.

120
00:08:42,200 --> 00:08:48,650
Also, if you are using on PUSH, it's important to avoid to mutate the data directly at the level of

121
00:08:48,650 --> 00:08:54,520
the view, which is a good idea anyway, because it leads to programs that are easier to reason about.

122
00:08:54,530 --> 00:09:00,920
So you could consider making your data objects immutable either by freezing them before passing them

123
00:09:00,920 --> 00:09:01,520
to The View.

124
00:09:01,520 --> 00:09:04,220
So this is standard JavaScript functionality.

125
00:09:04,310 --> 00:09:07,950
You just make all the existing properties only.

126
00:09:08,000 --> 00:09:14,480
Alternatively, you can consider using a library such as Immutable G.S. or IMOD for the vast majority

127
00:09:14,480 --> 00:09:15,230
of use cases.

128
00:09:15,230 --> 00:09:21,500
I would recommend to you to use default angular change detection and to not use on push.

129
00:09:21,530 --> 00:09:25,570
But if you ever run into the need of using unpunched now you know how to do it.

130
00:09:25,580 --> 00:09:30,410
Let's not talk about another performance optimization that we have available in ANGULAR.

131
00:09:30,410 --> 00:09:32,570
It's the attribute decorator.

