Skip to content

Commit a445756

Browse files
docs: add withCallState feature documentation
1 parent 90a37ca commit a445756

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed

docs/docs/extensions.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ It offers extensions like:
1313
- [Immutable State Protection](./with-immutable-state): Protects the state from being mutated outside or inside the Store.
1414
- [Redux](./with-redux): Possibility to use the Redux Pattern (Reducer, Actions, Effects)
1515
- [Reset](./with-reset): Adds a `resetState` method to your store
16+
- [Call State](./with-call-state): Add call state management to your signal stores
1617
- [Storage Sync](./with-storage-sync): Synchronizes the Store with Web Storage
1718
- [Undo Redo](./with-undo-redo): Adds Undo/Redo functionality to your store
1819

docs/docs/with-call-state.md

+198
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
---
2+
title: withCallState()
3+
---
4+
5+
```typescript
6+
import { withCallState } from '@angular-architects/ngrx-toolkit';
7+
```
8+
9+
`withCallState` adds call state management capabilities to NgRx signal stores, tracking the status of asynchronous operations with built-in states for loading, loaded, and error conditions.
10+
11+
## Basic Usage
12+
13+
The simplest way to use `withCallState` is without any configuration:
14+
15+
```typescript
16+
export const TodosStore = signalStore(
17+
withCallState(),
18+
// ... other features
19+
);
20+
```
21+
22+
This provides you with:
23+
- A `callState` state property of type `'init' | 'loading' | 'loaded' | { error: string }`
24+
- Computed signals: `loading`, `loaded`, and `error`
25+
- Helper methods: `setLoading()`, `setLoaded()`, and `setError()`
26+
27+
Example usage:
28+
29+
```typescript
30+
export class TodosComponent {
31+
store = inject(TodosStore);
32+
33+
loadTodos() {
34+
this.store.setLoading(); // callState = 'loading'
35+
try {
36+
// ... fetch todos
37+
this.store.setLoaded(); // callState = 'loaded'
38+
} catch (error) {
39+
this.store.setError(error); // callState = { error: 'error message' }
40+
}
41+
}
42+
}
43+
```
44+
45+
## Use Cases
46+
47+
- Track **loading states** for async operations
48+
- Handle **error states** consistently
49+
- Manage multiple **named collections** of call states
50+
51+
## Type Constraints
52+
53+
The call state can be one of these types:
54+
- `'init'` - Initial state
55+
- `'loading'` - Operation in progress
56+
- `'loaded'` - Operation completed successfully
57+
- `{ error: string }` - Operation failed with error
58+
59+
## Usage
60+
61+
```typescript
62+
import { withCallState } from '@angular-architects/ngrx-toolkit';
63+
64+
const store = signalStore(
65+
withCallState(),
66+
withMethods((store) => ({
67+
async loadData() {
68+
store.setLoading();
69+
try {
70+
// ... async operation
71+
store.setLoaded();
72+
} catch (error) {
73+
store.setError(error);
74+
}
75+
}
76+
}))
77+
);
78+
```
79+
80+
### Named Collection
81+
82+
You can track state for a specific collection by providing a collection name:
83+
84+
```typescript
85+
export const TodosStore = signalStore(
86+
withCallState({ collection: 'todos' }),
87+
// ... other features
88+
);
89+
```
90+
91+
This provides:
92+
- A `todosCallState` state property
93+
- Computed signals: `todosLoading`, `todosLoaded`, and `todosError`
94+
- Helper methods with optional collection parameter
95+
96+
```typescript
97+
const store = signalStore(
98+
withCallState({ collection: 'todos' }),
99+
withMethods((store) => ({
100+
async loadTodos() {
101+
store.todosSetLoading();
102+
try {
103+
// ... load todos
104+
store.todosSetLoaded();
105+
} catch (error) {
106+
store.todosSetError(error);
107+
}
108+
}
109+
}))
110+
);
111+
```
112+
113+
### Multiple Collections
114+
115+
For managing multiple async operations, use the collections configuration:
116+
117+
```typescript
118+
export const TodosStore = signalStore(
119+
withCallState({ collections: ['todos', 'categories'] }),
120+
// ... other features
121+
);
122+
```
123+
124+
This creates separate states and signals for each collection:
125+
- States: `todosCallState`, `categoriesCallState`
126+
- Signals: `todosLoading`, `todosLoaded`, `todosError`, `categoriesLoading`, etc.
127+
128+
```typescript
129+
const store = signalStore(
130+
withCallState({ collections: ['todos', 'users'] }),
131+
withMethods((store) => ({
132+
async loadAll() {
133+
store.todosSetLoading();
134+
store.usersSetLoading();
135+
// ... load data for both collections
136+
}
137+
}))
138+
);
139+
```
140+
141+
## Helper Methods
142+
143+
### setLoading()
144+
Sets the call state to 'loading':
145+
```typescript
146+
store.setLoading(); // Basic usage
147+
store.setLoading('todos'); // With collection
148+
```
149+
150+
### setLoaded()
151+
Sets the call state to 'loaded':
152+
```typescript
153+
store.setLoaded(); // Basic usage
154+
store.setLoaded('todos'); // With collection
155+
```
156+
157+
### setError()
158+
Sets the call state to error with a message:
159+
```typescript
160+
store.setError(error); // Basic usage
161+
store.setError(error, 'todos'); // With collection
162+
```
163+
164+
## Accessing State
165+
166+
Access the computed signals in your templates or component code:
167+
168+
```typescript
169+
@Component({
170+
template: `
171+
@if (store.loading()) {
172+
<div>Loading...</div>
173+
}
174+
@if (store.error()) {
175+
<div>Error: {{ store.error() }}</div>
176+
}
177+
@if (store.loaded()) {
178+
<div>Content loaded!</div>
179+
}
180+
`
181+
})
182+
export class MyComponent {
183+
store = inject(MyStore);
184+
}
185+
```
186+
187+
For collections:
188+
```typescript
189+
@Component({
190+
template: `
191+
@if (store.todosLoading()) {
192+
<div>Loading todos...</div>
193+
}
194+
@if (store.categoriesLoading()) {
195+
<div>Loading categories...</div>
196+
}
197+
`
198+
})

docs/sidebars.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const sidebars: SidebarsConfig = {
2525
'with-immutable-state',
2626
'with-feature-factory',
2727
'with-conditional',
28+
'with-call-state',
2829
],
2930
reduxConnectorSidebar: [
3031
{

0 commit comments

Comments
 (0)