1
- import { ErrorHandler , Injectable , OnDestroy , Optional } from '@angular/core' ;
1
+ import {
2
+ DestroyRef ,
3
+ ErrorHandler ,
4
+ inject ,
5
+ Injectable ,
6
+ Optional ,
7
+ } from '@angular/core' ;
2
8
import {
3
9
EMPTY ,
4
10
from ,
@@ -65,11 +71,16 @@ import { toHook, untilDestroyed } from './utils';
65
71
* NOTE: Avoid calling register/unregister/subscribe inside the side-effect function.
66
72
*/
67
73
@Injectable ( )
68
- export class RxEffects implements OnDestroy , OnDestroy $ {
74
+ export class RxEffects implements OnDestroy$ {
69
75
constructor (
70
76
@Optional ( )
71
- private readonly errorHandler : ErrorHandler | null
72
- ) { }
77
+ private readonly errorHandler : ErrorHandler | null ,
78
+ ) {
79
+ inject ( DestroyRef ) . onDestroy ( ( ) => {
80
+ this . _hooks$ . next ( { destroy : true } ) ;
81
+ this . subscription . unsubscribe ( ) ;
82
+ } ) ;
83
+ }
73
84
74
85
private static nextId = 0 ;
75
86
readonly _hooks$ = new Subject < DestroyProp > ( ) ;
@@ -95,7 +106,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ {
95
106
*/
96
107
register < T > (
97
108
sourceObs : ObservableInput < T > ,
98
- sideEffectFn : ( value : T ) => void
109
+ sideEffectFn : ( value : T ) => void ,
99
110
) : number ;
100
111
101
112
/**
@@ -119,7 +130,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ {
119
130
register < T > (
120
131
sourceObs : ObservableInput < T > ,
121
132
// tslint:disable-next-line: unified-signatures
122
- observer : PartialObserver < T >
133
+ observer : PartialObserver < T > ,
123
134
) : number ;
124
135
125
136
/**
@@ -152,7 +163,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ {
152
163
153
164
register < T > (
154
165
obsOrSub : ObservableInput < T > | Subscription ,
155
- fnOrObj ?: ( ( value : T ) => void ) | PartialObserver < T >
166
+ fnOrObj ?: ( ( value : T ) => void ) | PartialObserver < T > ,
156
167
) : number | void {
157
168
if ( obsOrSub instanceof Subscription ) {
158
169
this . subscription . add ( obsOrSub ) ;
@@ -170,8 +181,8 @@ export class RxEffects implements OnDestroy, OnDestroy$ {
170
181
this . errorHandler ?. handleError ( err ) ;
171
182
return EMPTY ;
172
183
} ) ,
173
- applyBehavior
174
- )
184
+ applyBehavior ,
185
+ ) ,
175
186
) ;
176
187
} else {
177
188
this . observables$ . next ( from ( obsOrSub ) . pipe ( applyBehavior ) ) ;
@@ -223,15 +234,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ {
223
234
return < V > ( source : Observable < V > ) =>
224
235
source . pipe (
225
236
untilDestroyed ( this ) ,
226
- takeUntil ( this . effects$ . pipe ( filter ( ( eId ) => eId === effectId ) ) )
237
+ takeUntil ( this . effects$ . pipe ( filter ( ( eId ) => eId === effectId ) ) ) ,
227
238
) ;
228
239
}
229
-
230
- /**
231
- * @internal
232
- */
233
- ngOnDestroy ( ) : void {
234
- this . _hooks$ . next ( { destroy : true } ) ;
235
- this . subscription . unsubscribe ( ) ;
236
- }
237
240
}
0 commit comments