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
//! Thread Safe Reactive Data Structure. Made with ❤️ for 🦀
//!
//! # Reactive
//!
//! `Reactive` is a thread-safe reactive data structure for managing state changes
//! in single-threaded or multi-threaded environments. It allows users to create reactive values that automatically
//! notifies observers when their underlying values change.
//!
//! ## Update
//!
//! ```
//! use reactivate::Reactive;
//!
//! let r = Reactive::new(10);
//! let d = r.derive(|val| val + 5);
//!
//! r.update(|val| val * 2);
//! r.update_inplace(|val| *val += 1);
//! r.update_unchecked(|val| val * 2);
//! r.update_inplace_unchecked(|val| *val += 1);
//!
//! println!("{:?}", r); // Reactive(43)
//! println!("{:?}", d); // Reactive(48)
//! ```
//!
//! ## Observers
//!
//! ```
//! use reactivate::Reactive;
//! # use std::rc::Rc;
//! # use std::cell::RefCell;
//! # use std::sync::{Arc, Mutex};
//!
//! let r = Reactive::new(0);
//!
//! // normal observer
//! r.add_observer(|val| println!("{}", val));
//!
//! // non-threadsafe observer
//! let changes: Rc<RefCell<Vec<usize>>> = Default::default();
//! r.add_observer({
//! let changes = changes.clone();
//! move |val| changes.borrow_mut().push(*val)
//! });
//!
//! // threadsafe observer
//! // must use Arc<Mutex<Vec<_>>> when threadsafety is enabled (features = ["threadsafe"])
//! // because Reactive as a whole must be thread safe
//! let tsafe_changes: Arc<Mutex<Vec<usize>>> = Default::default();
//! r.add_observer({
//! let changes = tsafe_changes.clone();
//! move |val| changes.lock().expect("unable to acq lock").push(*val)
//! });
//! ```
//!
//! ## Merge
//!
//! ```
//! use reactivate::{Merge, Reactive};
//!
//! let a = Reactive::new(String::from("hazash"));
//! let b = Reactive::new(0);
//! let d = (&a, &b).merge().derive(|(s, n)| s.len() + n);
//!
//! println!("{:?}", a); // Reactive("hazash")
//! println!("{:?}", b); // Reactive(0)
//! println!("{:?}", d); // Reactive(6)
//!
//! b.update(|_| 5);
//!
//! println!("{:?}", a); // Reactive("hazash")
//! println!("{:?}", b); // Reactive(5)
//! println!("{:?}", d); // Reactive(11)
//!
//!
//! a.update(|_| String::from("mouse"));
//!
//! println!("{:?}", a); // Reactive("mouse")
//! println!("{:?}", b); // Reactive(5)
//! println!("{:?}", d); // Reactive(10)
//! ```
//!
//! ## With Threads (features = ["threadsafe"])
//!
//! ```
//! # #[cfg(feature = "threadsafe")]
//! # {
//! use reactivate::Reactive;
//! use std::{thread, time::Duration};
//!
//! let r: Reactive<String> = Reactive::default();
//! let d = r.derive(|s| s.len());
//!
//! let handle = thread::spawn({
//! let r = r.clone();
//!
//! move || {
//! for _ in 0..10 {
//! r.update_inplace(|s| s.push('a'));
//! thread::sleep(Duration::from_millis(1));
//! }
//! }
//! });
//!
//! for _ in 0..10 {
//! r.update_inplace(|s| s.push('b'));
//! thread::sleep(Duration::from_millis(1));
//! }
//!
//! handle.join().unwrap();
//!
//! println!("{:?}", r); // Reactive("babababababababababa")
//! println!("{:?}", d); // Reactive(20)
//! # }
//! ```
//!
//! ## Concurrency
//!
//! `Reactive` provides thread-safe implementations using `Arc` and `Mutex` for multi-threaded environments. Ensure to enable the `threadsafe` feature to use the thread-safe version.
//!
//! ## Performance
//!
//! For performance-critical scenarios, `Reactive` provides methods like `update_unchecked` and `update_inplace_unchecked` for efficient updates without checking for value changes, optimizing performance especially in cases where frequent updates occur.
//!
//! For more details and usage examples, refer to the individual method documentations.
//!
pub use Merge;
pub use Reactive;