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
//! Side effects.
use std::collections::HashSet;
use crate::*;
thread_local! {
/// While the [`EffectState`] is inside the Vec, it is owned by [`EFFECTS`].
/// Because this is a global variable, the lifetime is necessarily `'static`. However, that does not mean
/// that it can last forever. The `EffectState` should only be used the time it is inside [`EFFECTS`].
pub(crate) static EFFECTS: RefCell<Vec<*mut EffectState<'static>>> = Default::default();
}
/// The internal state of an effect. The effect callback and the effect dependencies are stored in
/// this struct.
pub(crate) struct EffectState<'a> {
/// The callback when the effect is re-executed.
cb: Rc<RefCell<dyn FnMut() + 'a>>,
/// A list of dependencies that can trigger this effect.
dependencies: HashSet<EffectDependency>,
}
/// Implements reference equality for [`WeakSignalEmitter`]s.
pub(crate) struct EffectDependency(WeakSignalEmitter);
impl std::cmp::PartialEq for EffectDependency {
fn eq(&self, other: &Self) -> bool {
Weak::as_ptr(&self.0 .0) == Weak::as_ptr(&other.0 .0)
}
}
impl std::cmp::Eq for EffectDependency {}
impl std::hash::Hash for EffectDependency {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
Weak::as_ptr(&self.0 .0).hash(state);
}
}
impl<'a> EffectState<'a> {
// Clears the dependencies (both links and backlinks).
/// Should be called when re-executing an effect to recreate all dependencies.
pub fn clear_dependencies(&mut self) {
for dependency in &self.dependencies {
if let Some(dep) = dependency.0.upgrade() {
// SAFETY: We only access the pointer, not the pointed data.
dep.unsubscribe(unsafe { std::mem::transmute(Rc::as_ptr(&self.cb)) })
};
}
self.dependencies.clear();
}
/// Add a dependency to the effect.
pub fn add_dependency(&mut self, signal: WeakSignalEmitter) {
self.dependencies.insert(EffectDependency(signal));
}
}
impl<'a> Scope<'a> {
/// Creates an effect on signals used inside the effect closure.
///
/// # Example
/// ```
/// # use sycamore_reactive::*;
/// # create_scope_immediate(|ctx| {
/// let state = ctx.create_signal(0);
///
/// ctx.create_effect(|| {
/// println!("State changed. New state value = {}", state.get());
/// }); // Prints "State changed. New state value = 0"
///
/// state.set(1); // Prints "State changed. New state value = 1"
/// # });
/// ```
pub fn create_effect(&self, f: impl FnMut() + 'a) {
self._create_effect(Box::new(f))
}
/// Internal implementation for `create_effect`. Use dynamic dispatch to reduce code-bloat.
fn _create_effect(&self, mut f: Box<dyn FnMut() + 'a>) {
let effect = Rc::new(RefCell::new(None::<EffectState<'a>>));
let cb = Rc::new(RefCell::new({
let effect = Rc::downgrade(&effect);
move || {
EFFECTS.with(|effects| {
// Record initial effect stack length to verify that it is the same after.
let initial_effect_stack_len = effects.borrow().len();
// Upgrade the effect to an Rc now so that it is valid for the rest of the
// callback.
let effect_ref = effect.upgrade().unwrap();
// Take effect out.
let mut effect = effect_ref.take().unwrap();
effect.clear_dependencies();
// Push the effect onto the effect stack.
let boxed = Box::new(effect);
let ptr: *mut EffectState<'a> = Box::into_raw(boxed);
// Push the effect onto the effect stack so that it is visible by signals.
effects
.borrow_mut()
.push(ptr as *mut () as *mut EffectState<'static>);
// Now we can call the user-provided function.
f();
// Pop the effect from the effect stack.
effects.borrow_mut().pop().unwrap();
// SAFETY: Now that the effect has been popped from EFFECTS,
// get a boxed EffectState with the correct lifetime back.
let boxed = unsafe { Box::from_raw(ptr) };
// For all the signals collected by the EffectState,
// we need to add backlinks from the signal to the effect, so that
// updating the signal will trigger the effect.
for emitter in &boxed.dependencies {
// The SignalEmitter might have been destroyed between when the signal was
// accessed and now.
if let Some(emitter) = emitter.0.upgrade() {
// SAFETY: When the effect is destroyed or when the emitter is dropped,
// this link will be destroyed to prevent
// dangling references.
emitter.subscribe(Rc::downgrade(unsafe {
std::mem::transmute(&boxed.cb)
}));
}
}
// Get the effect state back into the Rc
*effect_ref.borrow_mut() = Some(*boxed);
debug_assert_eq!(effects.borrow().len(), initial_effect_stack_len);
});
}
}));
// Initialize initial effect state.
*effect.borrow_mut() = Some(EffectState {
cb: cb.clone(),
dependencies: HashSet::new(),
});
// Initial callback call to get everything started.
cb.borrow_mut()();
// Push Rc to self.effects so that it is not dropped immediately.
self.effects.borrow_mut().push(effect);
}
/// Creates an effect on signals used inside the effect closure.
///
/// Instead of [`create_effect`](Self::create_effect), this function also provides a new
/// reactive scope instead the effect closure. This scope is created for each new run of the
/// effect.
///
/// Items created within the scope cannot escape outside the effect because that can result in
/// an use-after-free.
///
/// # Example
/// ```
/// # use sycamore_reactive::*;
/// # create_scope_immediate(|ctx| {
/// ctx.create_effect_scoped(|ctx| {
/// // Use the scoped ctx inside here.
/// let _nested_signal = ctx.create_signal(0);
/// // _nested_signal cannot escape out of the effect closure.
/// });
/// # });
/// ```
pub fn create_effect_scoped<F>(&'a self, mut f: F)
where
F: for<'child_lifetime> FnMut(BoundedScopeRef<'child_lifetime, 'a>) + 'a,
{
let mut disposer: Option<Box<ScopeDisposer<'a>>> = None;
self.create_effect(move || {
// We run the disposer inside the effect, after effect dependencies have been cleared.
// This is to make sure that if the effect subscribes to its own signal, there is no
// use-after-free during the clear dependencies phase.
if let Some(disposer) = disposer.take() {
// SAFETY: we are not accessing the scope after the effect has been dropped.
unsafe {
disposer.dispose();
}
}
// Create a new nested scope and save the disposer.
let new_disposer: Option<Box<ScopeDisposer<'a>>> =
Some(Box::new(self.create_child_scope(|ctx| {
// SAFETY: f takes the same parameter as the argument to
// self.create_child_scope(_).
f(unsafe { std::mem::transmute(ctx) })
})));
// SAFETY: transmute the lifetime. This is safe because disposer is only used within the
// effect which is necessarily within the lifetime of self (the Scope).
// disposer = unsafe { std::mem::transmute(new_disposer) };
disposer = new_disposer;
});
}
}
/// Run the passed closure inside an untracked dependency scope.
///
/// See also [`ReadSignal::get_untracked()`].
///
/// # Example
///
/// ```
/// # use sycamore_reactive::*;
/// # create_scope_immediate(|ctx| {
/// let state = ctx.create_signal(1);
/// let double = ctx.create_memo(|| untrack(|| *state.get() * 2));
/// // ^^^^^^^
/// assert_eq!(*double.get(), 2);
///
/// state.set(2);
/// // double value should still be old value because state was untracked
/// assert_eq!(*double.get(), 2);
/// # });
/// ```
pub fn untrack<T>(f: impl FnOnce() -> T) -> T {
EFFECTS.with(|effects| {
let tmp = effects.take();
let ret = f();
*effects.borrow_mut() = tmp;
ret
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn effect() {
create_scope_immediate(|ctx| {
let state = ctx.create_signal(0);
let double = ctx.create_signal(-1);
ctx.create_effect(|| {
double.set(*state.get() * 2);
});
assert_eq!(*double.get(), 0); // calling create_effect should call the effect at least once
state.set(1);
assert_eq!(*double.get(), 2);
state.set(2);
assert_eq!(*double.get(), 4);
});
}
#[test]
fn effect_with_explicit_dependencies() {
create_scope_immediate(|ctx| {
let state = ctx.create_signal(0);
let double = ctx.create_signal(-1);
ctx.create_effect(on([state], || {
double.set(*state.get() * 2);
}));
assert_eq!(*double.get(), 0); // calling create_effect should call the effect at least once
state.set(1);
assert_eq!(*double.get(), 2);
state.set(2);
assert_eq!(*double.get(), 4);
});
}
#[test]
fn effect_cannot_create_infinite_loop() {
create_scope_immediate(|ctx| {
let state = ctx.create_signal(0);
ctx.create_effect(|| {
state.track();
state.set(0);
});
state.set(0);
});
}
#[test]
fn effect_should_only_subscribe_once_to_same_signal() {
create_scope_immediate(|ctx| {
let state = ctx.create_signal(0);
let counter = ctx.create_signal(0);
ctx.create_effect(|| {
counter.set(*counter.get_untracked() + 1);
// call state.track() twice but should subscribe once
state.track();
state.track();
});
assert_eq!(*counter.get(), 1);
state.set(1);
assert_eq!(*counter.get(), 2);
});
}
#[test]
fn effect_should_recreate_dependencies_each_time() {
create_scope_immediate(|ctx| {
let condition = ctx.create_signal(true);
let state1 = ctx.create_signal(0);
let state2 = ctx.create_signal(1);
let counter = ctx.create_signal(0);
ctx.create_effect(|| {
counter.set(*counter.get_untracked() + 1);
if *condition.get() {
state1.track();
} else {
state2.track();
}
});
assert_eq!(*counter.get(), 1);
state1.set(1);
assert_eq!(*counter.get(), 2);
state2.set(1);
assert_eq!(*counter.get(), 2); // not tracked
condition.set(false);
assert_eq!(*counter.get(), 3);
state1.set(2);
assert_eq!(*counter.get(), 3); // not tracked
state2.set(2);
assert_eq!(*counter.get(), 4); // tracked after condition.set
});
}
#[test]
fn outer_effects_run_first() {
create_scope_immediate(|ctx| {
let trigger = ctx.create_signal(());
let outer_counter = ctx.create_signal(0);
let inner_counter = ctx.create_signal(0);
ctx.create_effect_scoped(|ctx| {
trigger.track();
outer_counter.set(*outer_counter.get_untracked() + 1);
ctx.create_effect(|| {
trigger.track();
inner_counter.set(*inner_counter.get_untracked() + 1);
});
});
assert_eq!(*outer_counter.get(), 1);
assert_eq!(*inner_counter.get(), 1);
trigger.set(());
assert_eq!(*outer_counter.get(), 2);
assert_eq!(*inner_counter.get(), 2);
});
}
#[test]
fn destroy_effects_on_scope_dispose() {
create_scope_immediate(|ctx| {
let counter = ctx.create_signal(0);
let trigger = ctx.create_signal(());
let disposer = ctx.create_child_scope(|ctx| {
ctx.create_effect(|| {
trigger.track();
counter.set(*counter.get_untracked() + 1);
});
});
assert_eq!(*counter.get(), 1);
trigger.set(());
assert_eq!(*counter.get(), 2);
unsafe {
disposer.dispose();
}
trigger.set(());
assert_eq!(*counter.get(), 2); // inner effect should be destroyed and thus not executed
});
}
#[test]
fn effect_preserves_scope_hierarchy() {
create_scope_immediate(|ctx| {
let trigger = ctx.create_signal(());
let parent: &Signal<Option<*const ()>> = ctx.create_signal(None);
ctx.create_effect_scoped(|ctx| {
trigger.track();
let p = ctx.parent.unwrap();
parent.set(Some(p as *const ()));
});
assert_eq!(
parent.get().unwrap(),
ctx as *const _ as *const (),
"the parent scope of the effect should be `ctx`"
);
trigger.set(());
assert_eq!(
parent.get().unwrap(),
ctx as *const _ as *const (),
"the parent should still be `ctx` after effect is re-executed"
);
});
}
#[test]
fn effect_scoped_subscribing_to_own_signal() {
create_scope_immediate(|ctx| {
let trigger = ctx.create_signal(());
ctx.create_effect_scoped(|ctx| {
trigger.track();
let signal = ctx.create_signal(());
// Track own signal:
signal.track();
});
trigger.set(());
});
}
#[test]
fn effect_do_not_subscribe_to_destroyed_signal() {
create_scope_immediate(|ctx| {
let trigger = ctx.create_signal(());
let mut signal = Some(create_rc_signal(()));
ctx.create_effect(move || {
trigger.track();
if let Some(signal) = signal.take() {
signal.track();
drop(signal);
}
});
trigger.set(());
});
}
}