reratui 1.1.0

A modern, reactive TUI framework for Rust with React-inspired hooks and components, powered by ratatui
Documentation
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! Effect hook with post-commit execution.
//!
//! This module provides `use_effect`, a React-style effect hook that:
//! - Queues effects to run after the commit phase (not during render)
//! - Properly handles cleanup functions
//! - Supports dependency tracking for conditional execution

use std::future::Future;
use std::pin::Pin;

use crate::fiber::{AsyncCleanupFn, AsyncPendingEffect, PendingEffect};
use crate::fiber_tree::with_current_fiber;
use crate::scheduler::effect_queue::{
    queue_async_cleanup, queue_async_effect, queue_cleanup, queue_effect,
};

/// React-style useEffect with proper post-commit execution
///
/// # Differences from use_effect (deprecated)
/// - Effects are queued, not executed immediately
/// - Cleanup runs before new effect, not during render
/// - Proper fiber-scoped state
///
/// # Arguments
/// - `effect`: A function that performs the side effect and optionally returns a cleanup function
/// - `deps`: Dependencies that determine when the effect should re-run
///   - `Some(())` or use `use_effect_once`: Run only once on mount
///   - `None`: Run after every render
///   - `Some((a, b, ...))`: Run when any dependency changes
///
/// # Example
/// ```ignore
/// // Run once on mount
/// use_effect_once(|| {
///     println!("Mounted!");
///     Some(|| println!("Unmounting!"))
/// });
///
/// // Run when count changes
/// use_effect(|| {
///     println!("Count changed to: {}", count);
///     Option::<fn()>::None
/// }, Some((count,)));
///
/// // Run after every render
/// use_effect(|| {
///     println!("Rendered!");
///     Option::<fn()>::None
/// }, None::<()>);
/// ```
pub fn use_effect<Deps, F, C>(effect: F, deps: Option<Deps>)
where
    Deps: PartialEq + Clone + Send + 'static,
    F: FnOnce() -> Option<C> + 'static,
    C: FnOnce() + Send + 'static,
{
    with_current_fiber(|fiber| {
        fiber.track_hook_call("use_effect");
        let hook_index = fiber.next_hook_index();

        // Get previous deps from fiber's hook state
        let prev_deps: Option<Option<Deps>> = fiber.get_hook(hook_index);

        // Determine if effect should run
        let should_run = match (&deps, &prev_deps) {
            // No deps (None) = run every render
            (None, _) => true,
            // First render (no previous state)
            (Some(_), None) => true,
            // Check if deps changed
            (Some(current_deps), Some(Some(prev_deps))) => current_deps != prev_deps,
            // Previous was None, now has deps - run
            (Some(_), Some(None)) => true,
        };

        if should_run {
            // Queue cleanup from previous effect if it exists
            if let Some(cleanup) = fiber.cleanup_by_hook.remove(&hook_index) {
                queue_cleanup(cleanup);
            }

            // Wrap the effect to store cleanup in fiber after execution
            let fiber_id = fiber.id;
            let wrapped_effect = Box::new(move || {
                let cleanup_opt = effect();
                cleanup_opt.map(|c| Box::new(c) as crate::fiber::CleanupFn)
            });

            // Queue new effect for post-commit execution
            queue_effect(
                fiber_id,
                PendingEffect {
                    effect: wrapped_effect,
                    hook_index,
                },
            );
        }

        // Store deps for next render comparison
        fiber.set_hook(hook_index, deps);
    });
}

/// Convenience function for effects that run only once on mount
///
/// Equivalent to `use_effect(effect, Some(()))`
pub fn use_effect_once<F, C>(effect: F)
where
    F: FnOnce() -> Option<C> + 'static,
    C: FnOnce() + Send + 'static,
{
    use_effect(effect, Some(()));
}

/// React-style useAsyncEffect with proper post-commit execution for async operations
///
/// This hook is similar to `use_effect` but supports async effect functions
/// and async cleanup functions. It's useful for effects that need to perform
/// async operations like data fetching, subscriptions, or other I/O.
///
/// # Differences from use_effect
/// - Effect function returns a Future instead of executing synchronously
/// - Cleanup function can also be async
/// - Integrates with tokio for async execution
///
/// # Arguments
/// - `effect`: An async function that performs the side effect and optionally returns an async cleanup function
/// - `deps`: Dependencies that determine when the effect should re-run
///   - `Some(())` or use `use_async_effect_once`: Run only once on mount
///   - `None`: Run after every render
///   - `Some((a, b, ...))`: Run when any dependency changes
///
/// # Example
/// ```ignore
/// // Async effect that fetches data
/// use_async_effect(|| {
///     let set_data = set_data.clone();
///     async move {
///         let data = fetch_data().await;
///         set_data.set(data);
///         
///         // Optional async cleanup
///         Some(|| async move {
///             println!("Cleaning up...");
///         })
///     }
/// }, Some((user_id,)));
///
/// // Run once on mount
/// use_async_effect_once(|| async {
///     println!("Mounted!");
///     Some(|| async { println!("Unmounting!") })
/// });
/// ```
pub fn use_async_effect<Deps, F, Fut, C, CFut>(effect: F, deps: Option<Deps>)
where
    Deps: PartialEq + Clone + Send + 'static,
    F: FnOnce() -> Fut + Send + 'static,
    Fut: Future<Output = Option<C>> + Send + 'static,
    C: FnOnce() -> CFut + Send + 'static,
    CFut: Future<Output = ()> + Send + 'static,
{
    with_current_fiber(|fiber| {
        fiber.track_hook_call("use_async_effect");
        let hook_index = fiber.next_hook_index();

        // Get previous deps from fiber's hook state
        let prev_deps: Option<Option<Deps>> = fiber.get_hook(hook_index);

        // Determine if effect should run
        let should_run = match (&deps, &prev_deps) {
            // No deps (None) = run every render
            (None, _) => true,
            // First render (no previous state)
            (Some(_), None) => true,
            // Check if deps changed
            (Some(current_deps), Some(Some(prev_deps))) => current_deps != prev_deps,
            // Previous was None, now has deps - run
            (Some(_), Some(None)) => true,
        };

        if should_run {
            // Queue cleanup from previous async effect if it exists
            if let Some(async_cleanup) = fiber.async_cleanup_by_hook.remove(&hook_index) {
                queue_async_cleanup(async_cleanup);
            }

            // Wrap the async effect to return the proper type
            let fiber_id = fiber.id;
            let wrapped_effect: crate::fiber::AsyncEffectFn = Box::new(move || {
                Box::pin(async move {
                    let cleanup_opt = effect().await;
                    cleanup_opt.map(|c| {
                        Box::new(move || Box::pin(c()) as Pin<Box<dyn Future<Output = ()> + Send>>)
                            as AsyncCleanupFn
                    })
                })
            });

            // Queue new async effect for post-commit execution
            queue_async_effect(
                fiber_id,
                AsyncPendingEffect {
                    effect: wrapped_effect,
                    hook_index,
                },
            );
        }

        // Store deps for next render comparison
        fiber.set_hook(hook_index, deps);
    });
}

/// Convenience function for async effects that run only once on mount
///
/// Equivalent to `use_async_effect(effect, Some(()))`
pub fn use_async_effect_once<F, Fut, C, CFut>(effect: F)
where
    F: FnOnce() -> Fut + Send + 'static,
    Fut: Future<Output = Option<C>> + Send + 'static,
    C: FnOnce() -> CFut + Send + 'static,
    CFut: Future<Output = ()> + Send + 'static,
{
    use_async_effect(effect, Some(()));
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fiber_tree::{FiberTree, clear_fiber_tree, set_fiber_tree};
    use crate::scheduler::effect_queue::{
        clear_effect_queue, flush_effects_with_tree, has_pending_effects,
    };
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    fn setup_test_fiber() -> crate::fiber::FiberId {
        clear_effect_queue();
        let mut tree = FiberTree::new();
        let fiber_id = tree.mount(None, None);
        tree.begin_render(fiber_id);
        set_fiber_tree(tree);
        fiber_id
    }

    fn cleanup_test() {
        clear_fiber_tree();
        clear_effect_queue();
    }

    #[test]
    fn test_effect_queued_not_executed_immediately() {
        let _fiber_id = setup_test_fiber();

        let executed = Arc::new(AtomicUsize::new(0));
        let executed_clone = executed.clone();

        use_effect(
            move || {
                executed_clone.fetch_add(1, Ordering::SeqCst);
                Option::<fn()>::None
            },
            Some(()),
        );

        // Effect should be queued but not executed
        assert_eq!(executed.load(Ordering::SeqCst), 0);
        assert!(has_pending_effects());

        cleanup_test();
    }

    #[test]
    fn test_effect_runs_on_flush() {
        let _fiber_id = setup_test_fiber();

        let executed = Arc::new(AtomicUsize::new(0));
        let executed_clone = executed.clone();

        use_effect(
            move || {
                executed_clone.fetch_add(1, Ordering::SeqCst);
                Option::<fn()>::None
            },
            Some(()),
        );

        // Flush effects
        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.end_render();
            flush_effects_with_tree(tree);
        });

        // Effect should have executed
        assert_eq!(executed.load(Ordering::SeqCst), 1);

        cleanup_test();
    }

    #[test]
    fn test_effect_with_empty_deps_runs_once() {
        let fiber_id = setup_test_fiber();

        let executed = Arc::new(AtomicUsize::new(0));

        // First render
        {
            let executed_clone = executed.clone();
            use_effect(
                move || {
                    executed_clone.fetch_add(1, Ordering::SeqCst);
                    Option::<fn()>::None
                },
                Some(()), // Empty deps - run once
            );
        }

        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.end_render();
            flush_effects_with_tree(tree);
        });

        assert_eq!(executed.load(Ordering::SeqCst), 1);

        // Second render - effect should NOT run again
        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.begin_render(fiber_id);
        });

        {
            let executed_clone = executed.clone();
            use_effect(
                move || {
                    executed_clone.fetch_add(1, Ordering::SeqCst);
                    Option::<fn()>::None
                },
                Some(()), // Same empty deps
            );
        }

        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.end_render();
            flush_effects_with_tree(tree);
        });

        // Should still be 1 - effect didn't run again
        assert_eq!(executed.load(Ordering::SeqCst), 1);

        cleanup_test();
    }

    #[test]
    fn test_effect_with_none_deps_runs_every_render() {
        let fiber_id = setup_test_fiber();

        let executed = Arc::new(AtomicUsize::new(0));

        // First render
        {
            let executed_clone = executed.clone();
            use_effect(
                move || {
                    executed_clone.fetch_add(1, Ordering::SeqCst);
                    Option::<fn()>::None
                },
                None::<()>, // None deps - run every render
            );
        }

        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.end_render();
            flush_effects_with_tree(tree);
        });

        assert_eq!(executed.load(Ordering::SeqCst), 1);

        // Second render - effect SHOULD run again
        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.begin_render(fiber_id);
        });

        {
            let executed_clone = executed.clone();
            use_effect(
                move || {
                    executed_clone.fetch_add(1, Ordering::SeqCst);
                    Option::<fn()>::None
                },
                None::<()>,
            );
        }

        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.end_render();
            flush_effects_with_tree(tree);
        });

        // Should be 2 - effect ran again
        assert_eq!(executed.load(Ordering::SeqCst), 2);

        cleanup_test();
    }

    #[test]
    fn test_effect_with_changing_deps() {
        let fiber_id = setup_test_fiber();

        let executed = Arc::new(AtomicUsize::new(0));

        // First render with deps = (1,)
        {
            let executed_clone = executed.clone();
            use_effect(
                move || {
                    executed_clone.fetch_add(1, Ordering::SeqCst);
                    Option::<fn()>::None
                },
                Some((1i32,)),
            );
        }

        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.end_render();
            flush_effects_with_tree(tree);
        });

        assert_eq!(executed.load(Ordering::SeqCst), 1);

        // Second render with same deps = (1,) - should NOT run
        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.begin_render(fiber_id);
        });

        {
            let executed_clone = executed.clone();
            use_effect(
                move || {
                    executed_clone.fetch_add(1, Ordering::SeqCst);
                    Option::<fn()>::None
                },
                Some((1i32,)), // Same deps
            );
        }

        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.end_render();
            flush_effects_with_tree(tree);
        });

        assert_eq!(executed.load(Ordering::SeqCst), 1); // Still 1

        // Third render with different deps = (2,) - SHOULD run
        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.begin_render(fiber_id);
        });

        {
            let executed_clone = executed.clone();
            use_effect(
                move || {
                    executed_clone.fetch_add(1, Ordering::SeqCst);
                    Option::<fn()>::None
                },
                Some((2i32,)), // Different deps
            );
        }

        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.end_render();
            flush_effects_with_tree(tree);
        });

        assert_eq!(executed.load(Ordering::SeqCst), 2); // Now 2

        cleanup_test();
    }

    #[test]
    fn test_use_effect_once() {
        let fiber_id = setup_test_fiber();

        let executed = Arc::new(AtomicUsize::new(0));

        // First render
        {
            let executed_clone = executed.clone();
            use_effect_once(move || {
                executed_clone.fetch_add(1, Ordering::SeqCst);
                Option::<fn()>::None
            });
        }

        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.end_render();
            flush_effects_with_tree(tree);
        });

        assert_eq!(executed.load(Ordering::SeqCst), 1);

        // Second render - should NOT run again
        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.begin_render(fiber_id);
        });

        {
            let executed_clone = executed.clone();
            use_effect_once(move || {
                executed_clone.fetch_add(1, Ordering::SeqCst);
                Option::<fn()>::None
            });
        }

        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.end_render();
            flush_effects_with_tree(tree);
        });

        assert_eq!(executed.load(Ordering::SeqCst), 1);

        cleanup_test();
    }

    #[tokio::test]
    async fn test_async_effect_queued_not_executed_immediately() {
        let _fiber_id = setup_test_fiber();

        let executed = Arc::new(AtomicUsize::new(0));
        let executed_clone = executed.clone();

        use_async_effect(
            move || {
                let executed = executed_clone.clone();
                async move {
                    executed.fetch_add(1, Ordering::SeqCst);
                    Option::<fn() -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>>::None
                }
            },
            Some(()),
        );

        // Effect should be queued but not executed
        assert_eq!(executed.load(Ordering::SeqCst), 0);
        assert!(crate::scheduler::effect_queue::has_pending_async_effects());

        cleanup_test();
    }

    #[tokio::test]
    async fn test_async_effect_with_empty_deps_runs_once() {
        let fiber_id = setup_test_fiber();

        let executed = Arc::new(AtomicUsize::new(0));

        // First render
        {
            let executed_clone = executed.clone();
            use_async_effect(
                move || {
                    let executed = executed_clone.clone();
                    async move {
                        executed.fetch_add(1, Ordering::SeqCst);
                        Option::<
                            fn() -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>,
                        >::None
                    }
                },
                Some(()), // Empty deps - run once
            );
        }

        // Verify effect was queued
        assert!(crate::scheduler::effect_queue::has_pending_async_effects());

        // Flush async effects
        let pending =
            crate::scheduler::effect_queue::with_effect_queue(|queue| queue.drain_async_effects());

        // Execute the async effects
        for (_fiber_id, pending_effect) in pending {
            let future = (pending_effect.effect)();
            future.await;
        }

        assert_eq!(executed.load(Ordering::SeqCst), 1);

        // Second render - effect should NOT run again
        crate::fiber_tree::with_fiber_tree_mut(|tree| {
            tree.end_render();
            tree.begin_render(fiber_id);
        });

        {
            let executed_clone = executed.clone();
            use_async_effect(
                move || {
                    let executed = executed_clone.clone();
                    async move {
                        executed.fetch_add(1, Ordering::SeqCst);
                        Option::<
                            fn() -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>,
                        >::None
                    }
                },
                Some(()), // Same empty deps
            );
        }

        // Should not have queued a new async effect since deps didn't change
        assert!(!crate::scheduler::effect_queue::has_pending_async_effects());

        // Should still be 1 - effect didn't run again
        assert_eq!(executed.load(Ordering::SeqCst), 1);

        cleanup_test();
    }

    #[test]
    fn test_use_async_effect_once() {
        let _fiber_id = setup_test_fiber();

        let executed = Arc::new(AtomicUsize::new(0));
        let executed_clone = executed.clone();

        use_async_effect_once(move || {
            let executed = executed_clone.clone();
            async move {
                executed.fetch_add(1, Ordering::SeqCst);
                Option::<fn() -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>>::None
            }
        });

        // Effect should be queued
        assert!(crate::scheduler::effect_queue::has_pending_async_effects());

        cleanup_test();
    }
}