@@ -127,16 +127,18 @@ h3#managing-subscriptions Managing Subscriptions
127
127
its no longer needed.
128
128
129
129
We'll create a component named `HeroCounterComponent` that will do a simple task of increasing a total of heroes. We'll simulate
130
- that this hero counter is running as long as the component is active an in view. Once the component is destroyed, we no longer
130
+ that this hero counter is running as long as the component is active in the view. Once the component is destroyed, we no longer
131
131
want to listen for any changes coming from the Observable counter.
132
132
133
- // Example of hero counter
133
+ + makeExcerpt( 'src/app/ hero-counter.component.1.ts' , ' counter-unsubscribe' )
134
134
135
+ :marked
135
136
Since you know Angular has lifecycle hooks, we can use the `ngOnDestroy` lifecycle hook to unsubscribe from this Observable counter
136
137
and clean up its resources.
137
138
138
- // Example of unsubscribe
139
+ + makeExcerpt( 'src/app/hero-counter.component.1.ts' , 'ngOnDestroy- unsubscribe' )
139
140
141
+ :marked
140
142
Disposing of a single subscription when your component is destroyed is very manageable, but as you use more Observables managing
141
143
multiple subscriptions can get unwieldy. We can use a better approach to managing subscriptions. Observables have `operators`
142
144
that can cancel other observable streams. We can end multiple observable streams with one observable using the `takeUntil` operator.
@@ -146,19 +148,23 @@ h3#managing-subscriptions Managing Subscriptions
146
148
Let's update our hero counter example to use the `takeUntil` operator. In order to use the `takeUntil` operator, we must add it
147
149
to the base Observable prototype. We'll import the operator which will add it to the observable.
148
150
149
- // Example of import for takeUntil operator
151
+ + makeExcerpt( 'src/app/hero-counter.component.ts' , ' takeUntil- operator' )
150
152
153
+ :marked
151
154
Since we need an Observable that emits a value, we can use a `Subject`. We'll cover streams you can create on your own later in
152
- the chapter, as a `Subject` is a special type of Observable. We'll create a `destroy$` observable using the Subject.
155
+ the chapter, as a `Subject` is a special type of Observable.
153
156
154
- // Example of new Subject( )
157
+ + makeExcerpt( 'src/app/hero-counter.component.ts' , 'import-subject' )
155
158
156
- Now we can add the `takeUntil` operator to our Observable and once the `destroy$` Observable produces a value,
157
- the counter Observable will complete and will no longer produce any values .
159
+ : marked
160
+ You'll need to create an `onDestroy$` observable using the Subject .
158
161
159
- // example of destroy$ in ngOnDestroy
162
+ + makeExcerpt( 'src/app/hero-counter.component.ts' , 'onDestroy-subject' )
160
163
161
- This approach scales and you can use a single observable to trigger completion across multiple subscriptions.
164
+ :marked
165
+ Now we can add the `takeUntil` operator to our Observable and once the `onDestroy$` Observable completes,
166
+ the counter Observable will complete and will no longer produce any values. This approach scales and you can use a single observable
167
+ to trigger completion across multiple subscriptions.
162
168
163
169
+ makeExcerpt('src/app/hero-counter.component.ts' , '' )
164
170
0 commit comments