Skip to content

Commit 730f221

Browse files
committed
fix(store): stop contributing to stability once app is stable
1 parent ddff0c6 commit 730f221

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

packages/store/src/pending-tasks.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DestroyRef, inject, PendingTasks } from '@angular/core';
1+
import { ApplicationRef, inject, PendingTasks } from '@angular/core';
22
import { buffer, debounceTime, filter } from 'rxjs';
33

44
import { Actions, ActionStatus } from './actions-stream';
@@ -15,7 +15,7 @@ import { withNgxsPreboot } from './standalone-features/preboot';
1515
export function withNgxsPendingTasks() {
1616
return withNgxsPreboot(() => {
1717
const actions$ = inject(Actions);
18-
const destroyRef = inject(DestroyRef);
18+
const appRef = inject(ApplicationRef);
1919
const pendingTasks = inject(PendingTasks);
2020

2121
// Removing a pending task via the public API forces a scheduled tick, ensuring that
@@ -34,9 +34,9 @@ export function withNgxsPendingTasks() {
3434
// If the app is forcely destroyed before all actions are completed,
3535
// we clean up the set of actions being executed to prevent memory leaks
3636
// and remove the pending task to stabilize the app.
37-
destroyRef.onDestroy(() => executedActions.clear());
37+
appRef.onDestroy(() => executedActions.clear());
3838

39-
actions$
39+
const subscription = actions$
4040
.pipe(
4141
filter(context => {
4242
if (context.status === ActionStatus.Dispatched) {
@@ -68,5 +68,12 @@ export function withNgxsPendingTasks() {
6868
}
6969
}
7070
});
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+
});
7178
});
7279
}

0 commit comments

Comments
 (0)