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
//! Effect Event Hook - Stable callbacks that always call the latest handler
//!
//! This module provides React's experimental `useEffectEvent` functionality for Rust TUI applications.
//! It creates a stable callback reference that doesn't change between renders, but always calls
//! the latest version of the provided handler. This is crucial for:
//! - Event handlers in effects with empty dependency arrays
//! - Callbacks passed to memoized child components
//! - Avoiding unnecessary re-renders while maintaining fresh state access
//! - Breaking the dependency cycle in effects
//!
use crate;
use crateuse_effect_always;
use crateuse_ref;
use Arc;
/// A stable callback handle that always invokes the latest handler
///
/// This is the return type of `use_effect_event`. The callback identity
/// remains stable across renders, but it always calls the most recent
/// version of the handler function.
pub type EffectEvent<IN, OUT> = ;
/// React-style `useEffectEvent` hook that creates a stable callback with fresh handler
///
/// This hook follows the exact React pattern using existing hooks:
/// 1. Store the handler in a ref (using `use_ref`)
/// 2. Update the ref on every render (using `use_effect_always`)
/// 3. Return a stable callback that calls the current ref value (using `use_callback`)
///
/// # Examples
///
/// ## Basic Usage
/// ```rust,no_run
/// # use reratui_hooks::effect_event::use_effect_event;
/// # use reratui_hooks::state::use_state;
/// # use reratui_hooks::hook_context::{HookContext, set_hook_context};
/// # use std::rc::Rc;
/// # let context = Rc::new(HookContext::new());
/// # set_hook_context(context);
/// let (count, set_count) = use_state(|| 0);
///
/// // This callback has a stable identity but always sees the latest count
/// let log_count = use_effect_event(move |_: ()| {
/// println!("Current count: {}", count.get());
/// });
///
/// log_count.emit(());
/// ```