Skip to content

Commit

Permalink
fix(store): stop contributing to stability once app is stable
Browse files Browse the repository at this point in the history
Stop contributing to stability once the application has become stable, which may happen
on the server before the platform is destroyed or in the browser once hydration is complete.
  • Loading branch information
arturovt committed Feb 18, 2025
1 parent ddff0c6 commit 32f1bfb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ $ npm install @ngxs/store@dev
- Fix(store): Reduce change detection cycles with pending tasks [#2280](https://github.com/ngxs/store/pull/2280)
- Fix(store): Complete action results on destroy [#2282](https://github.com/ngxs/store/pull/2282)
- Fix(store): Complete `dispatched$` in internal actions [#2285](https://github.com/ngxs/store/pull/2285)
- Fix(store): Stop contributing to stability once app is stable [#2306](https://github.com/ngxs/store/pull/2306)
- Build(store): Use `ngServerMode` to check whether we are in SSR [#2287](https://github.com/ngxs/store/pull/2287)
- Build(storage-plugin): Use `ngServerMode` to check whether we are in SSR [#2288](https://github.com/ngxs/store/pull/2288)
- Refactor(form-plugin): Replace `takeUntil` with `takeUntilDestroyed` [#2283](https://github.com/ngxs/store/pull/2283)
Expand Down
15 changes: 11 additions & 4 deletions packages/store/src/pending-tasks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DestroyRef, inject, PendingTasks } from '@angular/core';
import { ApplicationRef, inject, PendingTasks } from '@angular/core';
import { buffer, debounceTime, filter } from 'rxjs';

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

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

actions$
const subscription = actions$
.pipe(
filter(context => {
if (context.status === ActionStatus.Dispatched) {
Expand Down Expand Up @@ -68,5 +68,12 @@ export function withNgxsPendingTasks() {
}
}
});

// Stop contributing to stability once the application has become stable,
// which may happen on the server before the platform is destroyed or in
// the browser once hydration is complete.
appRef.whenStable().then(() => {
subscription.unsubscribe();
});
});
}

0 comments on commit 32f1bfb

Please sign in to comment.