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
//! Professional interval hooks for periodic execution
//!
//! This module provides both synchronous and asynchronous interval hooks for executing
//! callbacks at regular intervals, similar to JavaScript's `setInterval` but with proper
//! Rust async integration and cleanup.
//!
//! ## Key Features:
//! - **Synchronous intervals**: `use_interval` for simple periodic callbacks
//! - **Asynchronous intervals**: `use_async_interval` for async periodic operations
//! - Automatic cleanup when component unmounts or dependencies change
//! - Proper async/await integration with tokio runtime
//! - Thread-safe execution with proper error handling
//! - Professional resource management and memory safety
//! - Integration with the hook lifecycle system
//!
//! ## Usage Examples:
//!
//! ### Basic Synchronous Interval
//! ```rust,no_run
//! use reratui_hooks::interval::use_interval;
//! use std::time::Duration;
//!
//! // Simple interval example
//! use_interval(|| {
//! println!("This runs every second!");
//! }, Duration::from_secs(1));
//! ```
//!
//! ### Synchronous Interval with State Updates
//! ```rust,no_run
//! use reratui_hooks::state::use_state;
//! use reratui_hooks::interval::use_interval;
//! use std::time::Duration;
//!
//! // Interval with state updates
//! let (count, set_count) = use_state(|| 0);
//! use_interval({
//! let set_count = set_count.clone();
//! move || {
//! set_count.update(|prev| prev + 1);
//! }
//! }, Duration::from_millis(500));
//! ```
//!
//! ### Asynchronous Interval
//! ```rust,no_run
//! use reratui_hooks::interval::use_async_interval;
//! use reratui_hooks::state::use_state;
//! use std::time::Duration;
//!
//! // Async interval example
//! let (data, set_data) = use_state(|| String::new());
//! use_async_interval({
//! let set_data = set_data.clone();
//! move || {
//! let set_data = set_data.clone();
//! async move {
//! // Simulate async data fetching
//! let new_data = format!("Data updated at: {:?}", std::time::SystemTime::now());
//! set_data.set(new_data);
//! }
//! }
//! }, Duration::from_secs(2));
//! ```
use Duration;
use crateEffectDependencies;
// Implement EffectDependencies for Duration to enable dependency tracking
/// Professional synchronous interval hook for periodic callback execution
///
/// This hook provides a React-like `setInterval` functionality with proper cleanup
/// and integration with the component lifecycle. The interval automatically starts
/// when the component mounts and stops when it unmounts.
///
/// ## Key Features:
/// - Automatic cleanup on component unmount
/// - Thread-safe execution with proper error handling
/// - No async runtime dependency (uses std::thread)
/// - Professional resource management
/// - Consistent timing with std::thread::sleep
///
/// ## Parameters:
/// - `callback`: Synchronous function to execute at each interval
/// - `duration`: Time between executions
///
/// ## Behavior:
/// - The interval starts immediately when the hook is called
/// - If the duration changes, the interval is restarted with the new duration
/// - The interval is automatically cancelled when the component unmounts
/// - All spawned threads are properly cleaned up to prevent memory leaks
///
/// ## Thread Safety:
/// The callback must be `Send + 'static` to ensure thread safety across thread boundaries.
/// State updates should use thread-safe mechanisms like the state hooks.
///
/// ## Performance:
/// Uses std::thread with sleep for timing. The implementation avoids busy-waiting
/// and provides accurate timing without requiring an async runtime.
/// Professional asynchronous interval hook for periodic async callback execution
///
/// This hook provides async interval functionality with proper cleanup and integration
/// with the component lifecycle. Perfect for periodic async operations like data fetching,
/// API calls, or other async tasks. **Requires a tokio runtime to be active.**
///
/// ## Key Features:
/// - Automatic cleanup on component unmount
/// - Full async/await support for callbacks
/// - Thread-safe execution with proper error handling
/// - Integration with tokio async runtime
/// - Professional resource management
/// - Consistent timing with tokio::time::interval
///
/// ## Parameters:
/// - `callback`: Async function to execute at each interval
/// - `duration`: Time between executions
///
/// ## Behavior:
/// - The interval starts immediately when the hook is called
/// - Each callback execution waits for the previous one to complete
/// - If the duration changes, the interval is restarted with the new duration
/// - The interval is automatically cancelled when the component unmounts
/// - All spawned tasks are properly cleaned up to prevent memory leaks
///
/// ## Thread Safety:
/// The callback must be `Send + 'static` and return a future that is `Send + 'static`.
/// State updates should use thread-safe mechanisms like the state hooks.
///
/// ## Performance:
/// Uses tokio's optimized interval timer for accurate timing with minimal overhead.
/// The implementation properly handles async execution without blocking the runtime.
///
/// ## Runtime Requirements:
/// This function requires an active tokio runtime. If no runtime is available,
/// it will panic. Use `use_interval` for synchronous callbacks that don't require tokio.
///
/// ## Example:
/// ```rust,no_run
/// use reratui_hooks::interval::use_async_interval;
/// use reratui_hooks::state::use_state;
/// use std::time::Duration;
///
/// // Async interval example
/// let (data, set_data) = use_state(|| String::new());
/// use_async_interval({
/// let set_data = set_data.clone();
/// move || {
/// let set_data = set_data.clone();
/// async move {
/// // Simulate async API call
/// let response = "API data".to_string();
/// set_data.set(response);
/// }
/// }
/// }, Duration::from_secs(5));
/// ```