Skip to content

Commit 43807bb

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

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-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

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
title: withCallState
3+
description: Add call state management to your NgRx signal stores
4+
---
5+
6+
# withCallState
7+
8+
The `withCallState` feature adds call state management capabilities to your NgRx signal stores, making it easy to track the status of asynchronous operations. It provides built-in states for loading, loaded, and error conditions.
9+
10+
## Basic Usage
11+
12+
The simplest way to use `withCallState` is without any configuration:
13+
14+
```typescript
15+
export const TodosStore = signalStore(
16+
withCallState(),
17+
// ... other features
18+
);
19+
```
20+
21+
This provides you with:
22+
- A `callState` state property of type `'init' | 'loading' | 'loaded' | { error: string }`
23+
- Computed signals: `loading`, `loaded`, and `error`
24+
- Helper methods: `setLoading()`, `setLoaded()`, and `setError()`
25+
26+
Example usage:
27+
28+
```typescript
29+
export class TodosComponent {
30+
store = inject(TodosStore);
31+
32+
loadTodos() {
33+
this.store.setLoading(); // callState = 'loading'
34+
try {
35+
// ... fetch todos
36+
this.store.setLoaded(); // callState = 'loaded'
37+
} catch (error) {
38+
this.store.setError(error); // callState = { error: 'error message' }
39+
}
40+
}
41+
}
42+
```
43+
44+
## Named Collection
45+
46+
You can track state for a specific collection by providing a collection name:
47+
48+
```typescript
49+
export const TodosStore = signalStore(
50+
withCallState({ collection: 'todos' }),
51+
// ... other features
52+
);
53+
```
54+
55+
This provides:
56+
- A `todosCallState` state property
57+
- Computed signals: `todosLoading`, `todosLoaded`, and `todosError`
58+
- Helper methods with optional collection parameter
59+
60+
## Multiple Collections
61+
62+
For managing multiple async operations, use the collections configuration:
63+
64+
```typescript
65+
export const TodosStore = signalStore(
66+
withCallState({ collections: ['todos', 'categories'] }),
67+
// ... other features
68+
);
69+
```
70+
71+
This creates separate states and signals for each collection:
72+
- States: `todosCallState`, `categoriesCallState`
73+
- Signals: `todosLoading`, `todosLoaded`, `todosError`, `categoriesLoading`, etc.
74+
75+
## State Types
76+
77+
The call state can be one of these values:
78+
- `'init'`: Initial state
79+
- `'loading'`: Operation in progress
80+
- `'loaded'`: Operation completed successfully
81+
- `{ error: string }`: Operation failed with error message
82+
83+
## Helper Methods
84+
85+
### setLoading()
86+
Sets the call state to 'loading':
87+
```typescript
88+
store.setLoading(); // Basic usage
89+
store.setLoading('todos'); // With collection
90+
```
91+
92+
### setLoaded()
93+
Sets the call state to 'loaded':
94+
```typescript
95+
store.setLoaded(); // Basic usage
96+
store.setLoaded('todos'); // With collection
97+
```
98+
99+
### setError()
100+
Sets the call state to error with a message:
101+
```typescript
102+
store.setError(error); // Basic usage
103+
store.setError(error, 'todos'); // With collection
104+
```
105+
106+
## Accessing State
107+
108+
Access the computed signals in your templates or component code:
109+
110+
```typescript
111+
@Component({
112+
template: `
113+
@if (store.loading()) {
114+
<div>Loading...</div>
115+
}
116+
@if (store.error()) {
117+
<div>Error: {{ store.error() }}</div>
118+
}
119+
@if (store.loaded()) {
120+
<div>Content loaded!</div>
121+
}
122+
`
123+
})
124+
export class MyComponent {
125+
store = inject(MyStore);
126+
}
127+
```
128+
129+
For collections:
130+
```typescript
131+
@Component({
132+
template: `
133+
@if (store.todosLoading()) {
134+
<div>Loading todos...</div>
135+
}
136+
@if (store.categoriesLoading()) {
137+
<div>Loading categories...</div>
138+
}
139+
`
140+
})

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)