Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(store): add withExperimentalNgxsPendingTasks #2186

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ $ npm install @ngxs/store@dev

### To become next patch version

- Feat(store): Add `withExperimentalNgxsPendingTasks` [#2186](https://github.com/ngxs/store/pull/2186)
- Fix(store): Decouple state signal updates from synchronous changes [#2189](https://github.com/ngxs/store/pull/2189)

### 18.0.0 2024-06-10
Expand Down
2 changes: 1 addition & 1 deletion docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
- [Module Federation](recipes/module-federation.md)
- [Unit Testing](recipes/unit-testing.md)
- [RxAngular Integration](recipes/intregration-with-rxangular.md)
- [Waiting For App Stability](recipes/waiting-for-app-stability.md)
- [Zoneless Server-Side Rendering](recipes/zoneless-ssr.md)

## COMMUNITY & LABS

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Waiting For App Stability
# Zoneless Server-Side Rendering

> :warning: Note that the current recipe may be used starting from Angular 18 because the experimental API was publicly exposed in Angular 18.

Expand All @@ -11,37 +11,11 @@ NGXS also executes actions during server-side rendering, and some of these actio
Let's examine the recipe for updating the "pending tasks" state whenever any action is dispatched and completed:

```ts
import { ApplicationConfig, inject, ExperimentalPendingTasks } from '@angular/core';
import { ActionStatus, Actions, provideStore, withNgxsPreboot } from '@ngxs/store';
import { ApplicationConfig } from '@angular/core';
import { provideStore } from '@ngxs/store';
import { withExperimentalNgxsPendingTasks } from '@ngxs/store/experimental';

export const appConfig: ApplicationConfig = {
providers: [
provideStore(
[],
withNgxsPreboot(() => {
const pendingTasks = inject(ExperimentalPendingTasks);
const actions$ = inject(Actions);

const actionToRemoveTaskFnMap = new Map<any, () => void>();

// Note that you don't have to unsubscribe from the actions stream in
// this specific case, as we complete the actions subject when the root
// view is destroyed. In server-side rendering, the root view is destroyed
// immediately once the app stabilizes and its HTML is serialized.
actions$.subscribe(ctx => {
if (ctx.status === ActionStatus.Dispatched) {
const removeTaskFn = pendingTasks.add();
actionToRemoveTaskFnMap.set(ctx.action, removeTaskFn);
} else {
const removeTaskFn = actionToRemoveTaskFnMap.get(ctx.action);
if (typeof removeTaskFn === 'function') {
removeTaskFn();
actionToRemoveTaskFnMap.delete(ctx.action);
}
}
});
})
)
]
providers: [provideStore([], withExperimentalNgxsPendingTasks())]
};
```
7 changes: 7 additions & 0 deletions packages/store/experimental/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
"lib": {
"entryFile": "src/index.ts",
"flatModuleFile": "ngxs-store-experimental"
}
}
1 change: 1 addition & 0 deletions packages/store/experimental/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { withExperimentalNgxsPendingTasks } from './pending-tasks';
31 changes: 31 additions & 0 deletions packages/store/experimental/src/pending-tasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { inject, ExperimentalPendingTasks } from '@angular/core';
import { ActionStatus, Actions, withNgxsPreboot } from '@ngxs/store';

/**
* This is an experimental feature that contributes to app stability,
* which is required during server-side rendering. With asynchronous
* actions being dispatched and handled, Angular is unaware of them in
* zoneless mode and doesn't know whether the app is still unstable.
* This may prematurely serialize the final HTML that is sent to the client.
*/
export function withExperimentalNgxsPendingTasks() {
return withNgxsPreboot(() => {
const pendingTasks = inject(ExperimentalPendingTasks);
const actions$ = inject(Actions);

const actionToRemoveTaskFnMap = new Map<any, () => void>();

actions$.subscribe(ctx => {
if (ctx.status === ActionStatus.Dispatched) {
const removeTaskFn = pendingTasks.add();
actionToRemoveTaskFnMap.set(ctx.action, removeTaskFn);
} else {
const removeTaskFn = actionToRemoveTaskFnMap.get(ctx.action);
if (typeof removeTaskFn === 'function') {
removeTaskFn();
actionToRemoveTaskFnMap.delete(ctx.action);
}
}
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,22 @@ import {
AfterViewInit,
Component,
Injectable,
inject,
ExperimentalPendingTasks,
provideExperimentalZonelessChangeDetection,
ChangeDetectionStrategy
} from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { renderApplication } from '@angular/platform-server';
import {
Action,
ActionStatus,
Actions,
State,
StateContext,
StateToken,
dispatch,
provideStore,
select,
withNgxsPreboot
select
} from '@ngxs/store';
import { freshPlatform } from '@ngxs/store/internals/testing';

function executeRecipeFromDocs() {
const pendingTasks = inject(ExperimentalPendingTasks);
const actions$ = inject(Actions);

const actionToRemoveTaskFnMap = new Map<any, () => void>();

actions$.subscribe(ctx => {
if (ctx.status === ActionStatus.Dispatched) {
const removeTaskFn = pendingTasks.add();
actionToRemoveTaskFnMap.set(ctx.action, removeTaskFn);
} else {
const removeTaskFn = actionToRemoveTaskFnMap.get(ctx.action);
if (typeof removeTaskFn === 'function') {
removeTaskFn();
actionToRemoveTaskFnMap.delete(ctx.action);
}
}
});
}
import { withExperimentalNgxsPendingTasks } from '@ngxs/store/experimental';

describe('preboot feature + stable', () => {
const COUNTRIES_STATE_TOKEN = new StateToken<string[]>('countries');
Expand Down Expand Up @@ -92,7 +68,7 @@ describe('preboot feature + stable', () => {
providers: [
provideExperimentalZonelessChangeDetection(),

provideStore([CountriesState], withNgxsPreboot(executeRecipeFromDocs))
provideStore([CountriesState], withExperimentalNgxsPendingTasks())
]
}),
{
Expand Down
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@ngxs/storage-plugin": ["packages/storage-plugin/index.ts"],
"@ngxs/storage-plugin/internals": ["packages/storage-plugin/internals/src/index.ts"],
"@ngxs/store": ["packages/store/index.ts"],
"@ngxs/store/experimental": ["packages/store/experimental/src/index.ts"],
"@ngxs/store/internals": ["packages/store/internals/src/index.ts"],
"@ngxs/store/internals/testing": ["packages/store/internals/testing/src/index.ts"],
"@ngxs/store/operators": ["packages/store/operators/src/index.ts"],
Expand Down
Loading