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