crabshell_reactive/
signal.rs1use std::cell::RefCell;
4use std::marker::PhantomData;
5use std::rc::Rc;
6
7use crate::handle::{Marker, handle_impls};
8use crate::runtime::{self, NodeId, drop_isolated};
9
10pub struct Signal<T> {
16 id: NodeId,
17 _marker: Marker<T>,
18}
19
20handle_impls!(Signal<T>);
21
22impl<T: 'static> Signal<T> {
23 #[track_caller]
24 pub(crate) fn new(owner: NodeId, value: T) -> Self {
25 let value: Rc<RefCell<Option<T>>> = Rc::new(RefCell::new(Some(value)));
26 Signal {
27 id: runtime::create_signal(owner, value),
28 _marker: PhantomData,
29 }
30 }
31
32 #[cfg(test)]
34 pub(crate) fn subscriber_count(&self) -> usize {
35 runtime::subscriber_count(self.id)
36 }
37
38 #[cfg(test)]
40 pub(crate) fn slot(&self) -> (u32, u32) {
41 runtime::slot_of(self.id)
42 }
43
44 pub fn read_only(&self) -> ReadSignal<T> {
46 ReadSignal {
47 id: self.id,
48 _marker: PhantomData,
49 }
50 }
51
52 #[track_caller]
60 pub fn get(&self) -> T
61 where
62 T: Clone,
63 {
64 self.with(T::clone)
65 }
66
67 #[track_caller]
74 pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
75 runtime::read_or_panic(self.id, true, "Signal", f)
76 }
77
78 #[track_caller]
80 pub fn get_untracked(&self) -> T
81 where
82 T: Clone,
83 {
84 self.with_untracked(T::clone)
85 }
86
87 #[track_caller]
89 pub fn with_untracked<R>(&self, f: impl FnOnce(&T) -> R) -> R {
90 runtime::read_or_panic(self.id, false, "Signal", f)
91 }
92
93 #[track_caller]
95 pub fn try_get(&self) -> Option<T>
96 where
97 T: Clone,
98 {
99 self.try_with(T::clone)
100 }
101
102 #[track_caller]
105 pub fn try_with<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R> {
106 runtime::read(self.id, true, "Signal", f).ok()
107 }
108
109 #[track_caller]
120 pub fn set(&self, value: T) {
121 if let Some(old) = runtime::write_or_panic(self.id, |slot: &mut Option<T>| slot.replace(value))
122 {
123 drop_isolated(old);
124 }
125 }
126
127 #[track_caller]
133 pub fn update(&self, f: impl FnOnce(&mut T)) {
134 runtime::write_or_panic(self.id, |slot: &mut Option<T>| update_slot(slot, f));
135 }
136
137 #[track_caller]
140 pub fn try_set(&self, value: T) -> bool {
141 match runtime::write(self.id, |slot: &mut Option<T>| slot.replace(value)) {
142 Ok(old) => {
143 drop_isolated(old);
144 true
145 }
146 Err(_) => false,
147 }
148 }
149
150 #[track_caller]
153 pub fn try_update(&self, f: impl FnOnce(&mut T)) -> bool {
154 runtime::write(self.id, |slot: &mut Option<T>| update_slot(slot, f)).is_ok()
155 }
156
157 pub fn named(self, name: impl Into<String>) -> Self {
159 runtime::set_name(self.id, name.into());
160 self
161 }
162}
163
164fn update_slot<T>(slot: &mut Option<T>, f: impl FnOnce(&mut T)) {
165 f(slot
166 .as_mut()
167 .expect("invariant: a signal always holds a value"));
168}
169
170pub struct ReadSignal<T> {
173 id: NodeId,
174 _marker: Marker<T>,
175}
176
177handle_impls!(ReadSignal<T>);
178
179impl<T: 'static> ReadSignal<T> {
180 #[track_caller]
182 pub fn get(&self) -> T
183 where
184 T: Clone,
185 {
186 self.with(T::clone)
187 }
188
189 #[track_caller]
191 pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
192 runtime::read_or_panic(self.id, true, "ReadSignal", f)
193 }
194
195 #[track_caller]
197 pub fn get_untracked(&self) -> T
198 where
199 T: Clone,
200 {
201 self.with_untracked(T::clone)
202 }
203
204 #[track_caller]
206 pub fn with_untracked<R>(&self, f: impl FnOnce(&T) -> R) -> R {
207 runtime::read_or_panic(self.id, false, "ReadSignal", f)
208 }
209
210 #[track_caller]
212 pub fn try_get(&self) -> Option<T>
213 where
214 T: Clone,
215 {
216 self.try_with(T::clone)
217 }
218
219 #[track_caller]
221 pub fn try_with<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R> {
222 runtime::read(self.id, true, "ReadSignal", f).ok()
223 }
224}