1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//! React-style useReducer hook for state management with actions
//!
//! This module provides a professional useReducer hook implementation that follows
//! React's API patterns for complex state management scenarios.
use cratewith_hook_context;
use ;
use Arc;
/// A handle to the current state managed by useReducer
///
/// This provides read-only access to the current state value with efficient
/// concurrent access patterns and version tracking for change detection.
/// A dispatch function for sending actions to the reducer
///
/// This function is used to dispatch actions that will be processed by the
/// reducer function to produce new state.
/// Internal container for reducer state management
/// React-style useReducer hook for complex state management
///
/// This hook provides a way to manage complex state logic using a reducer function,
/// similar to React's useReducer hook. It's particularly useful when you have complex
/// state logic that involves multiple sub-values or when the next state depends on
/// the previous one.
///
/// # Arguments
///
/// * `reducer` - A function that takes the current state and an action, returning new state
/// * `initial_state` - The initial state value
///
/// # Returns
///
/// A tuple containing:
/// * `ReducerStateHandle<S>` - Handle for accessing the current state
/// * `DispatchFn<A>` - Function for dispatching actions to update state
///
/// # Examples
///
/// ## Simple Counter with Actions
/// ```rust,no_run
/// # use reratui_hooks::reducer::use_reducer;
/// #[derive(Clone)]
/// enum CounterAction {
/// Increment,
/// Decrement,
/// Reset,
/// SetValue(i32),
/// }
///
/// fn counter_reducer(state: i32, action: CounterAction) -> i32 {
/// match action {
/// CounterAction::Increment => state + 1,
/// CounterAction::Decrement => state - 1,
/// CounterAction::Reset => 0,
/// CounterAction::SetValue(value) => value,
/// }
/// }
///
/// // Example usage in a component context
/// let (state, dispatch) = use_reducer(counter_reducer, 0);
/// let count = state.get();
/// // count is now 0
///
/// dispatch.call(CounterAction::Increment);
/// // In a real component, this would trigger a re-render
/// // and the state would be updated
/// ```
///
/// ## Complex State Management
/// ```rust,no_run
/// # use reratui_hooks::reducer::use_reducer;
/// #[derive(Clone)]
/// struct TodoState {
/// todos: Vec<Todo>,
/// filter: Filter,
/// next_id: u32,
/// }
///
/// #[derive(Clone)]
/// struct Todo {
/// id: u32,
/// text: String,
/// completed: bool,
/// }
///
/// #[derive(Clone)]
/// enum Filter {
/// All,
/// Active,
/// Completed,
/// }
///
/// #[derive(Clone)]
/// enum TodoAction {
/// AddTodo(String),
/// ToggleTodo(u32),
/// RemoveTodo(u32),
/// SetFilter(Filter),
/// ClearCompleted,
/// }
///
/// fn todo_reducer(state: TodoState, action: TodoAction) -> TodoState {
/// match action {
/// TodoAction::AddTodo(text) => TodoState {
/// todos: {
/// let mut todos = state.todos;
/// todos.push(Todo {
/// id: state.next_id,
/// text,
/// completed: false,
/// });
/// todos
/// },
/// next_id: state.next_id + 1,
/// ..state
/// },
/// TodoAction::ToggleTodo(id) => TodoState {
/// todos: state.todos.into_iter().map(|mut todo| {
/// if todo.id == id {
/// todo.completed = !todo.completed;
/// }
/// todo
/// }).collect(),
/// ..state
/// },
/// _ => state, // Handle other actions
/// }
/// }
///
/// // Example usage in a component context
/// let initial_state = TodoState {
/// todos: vec![],
/// filter: Filter::All,
/// next_id: 1,
/// };
///
/// let (state, dispatch) = use_reducer(todo_reducer, initial_state);
/// // state.get().todos.len() is now 0
///
/// dispatch.call(TodoAction::AddTodo("Learn Rust".to_string()));
/// // In a real component, this would trigger a re-render
/// ```
///
/// # Thread Safety
///
/// The returned state handle and dispatch function are both thread-safe and can be
/// safely shared across async tasks.
///
/// # Performance Notes
///
/// - State reads are optimized using RwLock for concurrent access
/// - State updates are atomic and thread-safe
/// - The reducer function should be pure (no side effects)
/// - State transitions are immutable - the reducer should return new state