1
- import { DestroyRef , inject , PendingTasks } from '@angular/core' ;
1
+ import { ApplicationRef , inject , PendingTasks } from '@angular/core' ;
2
2
import { buffer , debounceTime , filter } from 'rxjs' ;
3
3
4
4
import { Actions , ActionStatus } from './actions-stream' ;
@@ -15,7 +15,7 @@ import { withNgxsPreboot } from './standalone-features/preboot';
15
15
export function withNgxsPendingTasks ( ) {
16
16
return withNgxsPreboot ( ( ) => {
17
17
const actions$ = inject ( Actions ) ;
18
- const destroyRef = inject ( DestroyRef ) ;
18
+ const appRef = inject ( ApplicationRef ) ;
19
19
const pendingTasks = inject ( PendingTasks ) ;
20
20
21
21
// Removing a pending task via the public API forces a scheduled tick, ensuring that
@@ -34,9 +34,9 @@ export function withNgxsPendingTasks() {
34
34
// If the app is forcely destroyed before all actions are completed,
35
35
// we clean up the set of actions being executed to prevent memory leaks
36
36
// and remove the pending task to stabilize the app.
37
- destroyRef . onDestroy ( ( ) => executedActions . clear ( ) ) ;
37
+ appRef . onDestroy ( ( ) => executedActions . clear ( ) ) ;
38
38
39
- actions$
39
+ const subscription = actions$
40
40
. pipe (
41
41
filter ( context => {
42
42
if ( context . status === ActionStatus . Dispatched ) {
@@ -68,5 +68,12 @@ export function withNgxsPendingTasks() {
68
68
}
69
69
}
70
70
} ) ;
71
+
72
+ // Stop contributing to stability once the application has become stable,
73
+ // which may happen on the server before the platform is destroyed or in
74
+ // the browser once hydration is complete.
75
+ appRef . whenStable ( ) . then ( ( ) => {
76
+ subscription . unsubscribe ( ) ;
77
+ } ) ;
71
78
} ) ;
72
79
}
0 commit comments