Skip to content

Commit 0e763e1

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

File tree

3 files changed

+141
-0
lines changed

3 files changed

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

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

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)