@@ -127,16 +127,18 @@ h3#managing-subscriptions Managing Subscriptions
127127 its no longer needed.
128128
129129 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
131131 want to listen for any changes coming from the Observable counter.
132132
133- // Example of hero counter
133+ + makeExcerpt( 'src/app/ hero-counter.component.1.ts' , ' counter-unsubscribe' )
134134
135+ :marked
135136 Since you know Angular has lifecycle hooks, we can use the `ngOnDestroy` lifecycle hook to unsubscribe from this Observable counter
136137 and clean up its resources.
137138
138- // Example of unsubscribe
139+ + makeExcerpt( 'src/app/hero-counter.component.1.ts' , 'ngOnDestroy- unsubscribe' )
139140
141+ :marked
140142 Disposing of a single subscription when your component is destroyed is very manageable, but as you use more Observables managing
141143 multiple subscriptions can get unwieldy. We can use a better approach to managing subscriptions. Observables have `operators`
142144 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
146148 Let's update our hero counter example to use the `takeUntil` operator. In order to use the `takeUntil` operator, we must add it
147149 to the base Observable prototype. We'll import the operator which will add it to the observable.
148150
149- // Example of import for takeUntil operator
151+ + makeExcerpt( 'src/app/hero-counter.component.ts' , ' takeUntil- operator' )
150152
153+ :marked
151154 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.
153156
154- // Example of new Subject( )
157+ + makeExcerpt( 'src/app/hero-counter.component.ts' , 'import-subject' )
155158
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 .
158161
159- // example of destroy$ in ngOnDestroy
162+ + makeExcerpt( 'src/app/hero-counter.component.ts' , 'onDestroy-subject' )
160163
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.
162168
163169+ makeExcerpt('src/app/hero-counter.component.ts' , '' )
164170
0 commit comments