reinhardt-core 0.2.0

Core components for Reinhardt framework
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! Memo - Cached Reactive Computations
//!
//! `Memo<T>` represents a memoized computation that automatically updates when its dependencies change.
//! Unlike `Effect`, which runs for side effects, `Memo` caches and returns a computed value.
//!
//! ## Key Features
//!
//! - **Automatic Caching**: Computation result is cached and reused until dependencies change
//! - **Lazy Evaluation**: Only recomputes when `get()` is called and dependencies have changed
//! - **Automatic Dependency Tracking**: Dependencies are tracked automatically, just like Effect
//! - **Can be Depended On**: Other Effects and Memos can depend on a Memo, making it act like a Signal
//!
//! ## Example
//!
//! ```no_run
//! use reinhardt_core::reactive::{Signal, Memo};
//!
//! let count = Signal::new(5);
//!
//! // Create a memo that computes count * 2
//! let count_for_memo = count.clone();
//! let doubled = Memo::new(move || count_for_memo.get() * 2);
//!
//! // First access computes the value
//! assert_eq!(doubled.get(), 10);
//!
//! // Second access uses cached value (no recomputation)
//! assert_eq!(doubled.get(), 10);
//!
//! // When dependency changes, memo is marked dirty
//! count.set(10);
//!
//! // Next access recomputes
//! assert_eq!(doubled.get(), 20);
//! ```

use core::cell::RefCell;

extern crate alloc;
use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::rc::Rc;

use super::runtime::{EffectTiming, NodeId, NodeType, Observer, try_with_runtime, with_runtime};

/// Computation function for a Memo
type MemoFn<T> = Box<dyn FnMut() -> T + 'static>;

/// Cached value and dirty flag for a Memo
#[derive(Clone)]
struct MemoState<T: Clone> {
	/// The cached computed value
	value: T,
	/// Whether the value needs to be recomputed
	dirty: bool,
}

// Global storage for Memo computation functions
thread_local! {
	static MEMO_FUNCTIONS: RefCell<BTreeMap<NodeId, Box<dyn core::any::Any>>> = RefCell::new(BTreeMap::new());
}

// Global storage for Memo cached values
thread_local! {
	static MEMO_VALUES: RefCell<BTreeMap<NodeId, Box<dyn core::any::Any>>> = RefCell::new(BTreeMap::new());
}

// Type-agnostic dirty map for `Memo::new_with_deps` (Refs #4195).
//
// `MemoState<T>::dirty` is stored type-erased so flipping it from outside
// requires `T`. The dirty notifier created by `new_with_deps` is type-erased
// (it only knows `NodeId`), so it writes here instead. `Memo::get` checks
// both `MemoState<T>::dirty` and this map.
thread_local! {
	static MEMO_DIRTY: RefCell<BTreeMap<NodeId, bool>> = const { RefCell::new(BTreeMap::new()) };
}

/// Flag the Memo identified by `memo_id` as dirty without requiring `T`,
/// and propagate the change to downstream subscribers.
///
/// Called by the hidden notifier Effect created by [`Memo::new_with_deps`]
/// whenever one of the listed deps changes. Cleared on the next
/// [`Memo::get`] / [`Memo::get_untracked`] recompute.
///
/// The `notify_signal_change` call is what makes downstream `use_effect` /
/// `use_memo` calls that take this memo as a listed dep actually re-run.
/// Without it, the dirty flag would be set but no consumer would be
/// woken up. Mirrors the propagation behavior of `Memo::mark_dirty`.
pub(crate) fn mark_memo_dirty_by_id(memo_id: NodeId) {
	MEMO_DIRTY.with(|m| {
		m.borrow_mut().insert(memo_id, true);
	});
	with_runtime(|rt| rt.notify_signal_change(memo_id));
}

/// Returns whether the type-agnostic dirty flag is set for `memo_id`.
fn is_memo_dirty_externally(memo_id: NodeId) -> bool {
	MEMO_DIRTY.with(|m| m.borrow().get(&memo_id).copied().unwrap_or(false))
}

/// Clears the type-agnostic dirty flag for `memo_id` after a recompute.
fn clear_memo_dirty(memo_id: NodeId) {
	MEMO_DIRTY.with(|m| {
		m.borrow_mut().remove(&memo_id);
	});
}

/// A memoized reactive computation that caches its result
///
/// `Memo<T>` is similar to a Signal in that it can be read with `get()` and can have dependents.
/// However, unlike a Signal, its value is computed from other reactive values, and the
/// computation is automatically cached.
///
/// ## When to Use Memo vs Effect
///
/// - Use `Memo` when you need a **derived value** that should be cached
/// - Use `Effect` when you need to perform **side effects**
///
/// ## Example
///
/// ```rust
/// use reinhardt_core::reactive::{Signal, Memo, Effect};
///
/// let first_name = Signal::new("John".to_string());
/// let last_name = Signal::new("Doe".to_string());
///
/// // Memo caches the full name computation
/// let first_clone = first_name.clone();
/// let last_clone = last_name.clone();
/// let full_name = Memo::new(move || {
///     format!("{} {}", first_clone.get(), last_clone.get())
/// });
///
/// // Effect uses the memo
/// let full_name_clone = full_name.clone();
/// Effect::new(move || {
///     println!("Full name: {}", full_name_clone.get());
/// });
/// ```
#[derive(Clone)]
pub struct Memo<T: Clone + 'static> {
	/// Unique identifier for this memo
	id: NodeId,
	/// Whether this memo has been disposed
	disposed: Rc<RefCell<bool>>,
	/// Phantom data for type parameter
	_phantom: core::marker::PhantomData<T>,
	/// Hidden Effect that subscribes to explicit deps (`new_with_deps` only).
	///
	/// Wrapped in `Rc` so `Memo<T>` remains `Clone`. The Effect's closure
	/// calls [`mark_memo_dirty_by_id`] whenever any listed dep fires, so
	/// the next [`Self::get`] recomputes. `None` for `Memo::new`
	/// (auto-track path; the Memo is its own Observer).
	#[allow(dead_code)]
	deps_notifier: Option<Rc<super::effect::Effect>>,
}

impl<T: Clone + 'static> Memo<T> {
	/// Create a new Memo with the given computation function
	///
	/// The function runs immediately to compute the initial value, and will
	/// automatically re-run (when accessed via `get()`) after any of its
	/// dependencies change.
	///
	/// # Arguments
	///
	/// * `f` - The computation function. Must be `FnMut() -> T + 'static`.
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_core::reactive::{Signal, Memo};
	///
	/// let count = Signal::new(5);
	/// let count_clone = count.clone();
	/// let doubled = Memo::new(move || count_clone.get() * 2);
	/// assert_eq!(doubled.get(), 10);
	/// ```
	pub fn new<F>(f: F) -> Self
	where
		F: FnMut() -> T + 'static,
	{
		let id = NodeId::new();
		let disposed = Rc::new(RefCell::new(false));

		// Store the computation function.
		//
		// The stored closure must NOT capture a clone of `disposed`. `Drop`
		// uses `Rc::strong_count(&self.disposed) == 1` to detect the last live
		// clone; a strong reference held by this long-lived closure would keep
		// the count above 1 forever, so `dispose()` would never run and the
		// memo node would leak in the runtime. `dispose()` removes the
		// `MEMO_FUNCTIONS` entry, so this closure is never invoked after
		// disposal and needs no disposed-flag guard.
		MEMO_FUNCTIONS.with(|storage| {
			let mut storage = storage.borrow_mut();
			let boxed: Box<dyn core::any::Any> = Box::new(Box::new(f) as MemoFn<T>);
			storage.insert(id, boxed);
		});

		// Compute initial value
		let initial_value = Self::compute_value(id);

		// Store the initial value
		MEMO_VALUES.with(|storage| {
			let mut storage = storage.borrow_mut();
			let state = MemoState {
				value: initial_value,
				dirty: false,
			};
			let boxed: Box<dyn core::any::Any> = Box::new(state);
			storage.insert(id, boxed);
		});

		Self {
			id,
			disposed,
			_phantom: core::marker::PhantomData,
			deps_notifier: None,
		}
	}

	/// Create a Memo that recomputes only when one of the listed `deps` changes.
	///
	/// Unlike [`Self::new`], the computation closure runs with **no active
	/// reactive Observer**, so `Signal::get` calls inside do not auto-
	/// subscribe. A hidden Layout-timing Effect subscribes to the listed
	/// deps and flips the memo's dirty flag synchronously when any dep
	/// changes; the next [`Self::get`] then recomputes.
	///
	/// This is the lower-layer primitive used by `use_memo(f, deps)` in
	/// `reinhardt-pages`. See the design spec at
	/// `docs/superpowers/specs/2026-05-22-issue-4195-hooks-deps-array-design.md`.
	#[allow(dead_code)]
	pub fn new_with_deps<F>(mut f: F, deps: super::deps::Deps) -> Self
	where
		F: FnMut() -> T + 'static,
	{
		let id = NodeId::new();
		let disposed = Rc::new(RefCell::new(false));

		// Wrap the user closure to detach the Observer before each compute.
		// `compute_value` pushes the memo's NodeId as Observer, but Option A
		// requires Signal reads inside f to NOT auto-subscribe. The
		// `run_without_observer` call detaches the stack for the duration
		// of f.
		// See `Memo::new`: the wrapped closure must not hold a strong clone of
		// `disposed`, or `Drop`'s `strong_count == 1` last-clone check can
		// never fire. `dispose()` removes the `MEMO_FUNCTIONS` entry, so this
		// closure is never run after disposal.
		let f_wrapped = move || super::runtime::run_without_observer(&mut f);

		MEMO_FUNCTIONS.with(|storage| {
			let boxed: Box<dyn core::any::Any> = Box::new(Box::new(f_wrapped) as MemoFn<T>);
			storage.borrow_mut().insert(id, boxed);
		});

		// Initial compute (deps not yet subscribed; auto-track no-op inside
		// run_without_observer).
		let initial_value = Self::compute_value(id);

		MEMO_VALUES.with(|storage| {
			let state = MemoState {
				value: initial_value,
				dirty: false,
			};
			let boxed: Box<dyn core::any::Any> = Box::new(state);
			storage.borrow_mut().insert(id, boxed);
		});

		// Hidden notifier Effect: subscribes to deps; on trigger, marks
		// this memo dirty. Layout timing so dep changes propagate
		// synchronously, matching React's "memo result available immediately
		// after dep change" semantics.
		let deps_notifier = if deps.as_slice().is_empty() {
			None
		} else {
			let memo_id = id;
			let notifier = super::effect::Effect::new_with_deps_and_timing::<_, fn()>(
				move || {
					mark_memo_dirty_by_id(memo_id);
					None
				},
				deps,
				EffectTiming::Layout,
			);
			Some(Rc::new(notifier))
		};

		// The notifier's initial run set MEMO_DIRTY[id] = true; clear it
		// so the first `Self::get` returns the cached `initial_value`
		// without spuriously recomputing.
		clear_memo_dirty(id);

		Self {
			id,
			disposed,
			_phantom: core::marker::PhantomData,
			deps_notifier,
		}
	}

	/// Compute the value by executing the memo function
	///
	/// This is called internally when the memo needs to recompute.
	fn compute_value(memo_id: NodeId) -> T {
		with_runtime(|rt| {
			// Clear old dependencies before recomputing
			rt.clear_dependencies(memo_id);

			// Push observer onto stack
			rt.push_observer(Observer {
				id: memo_id,
				node_type: NodeType::Memo,
				timing: EffectTiming::default(), // Memos use default (Passive) timing
				cleanup: None,
			});
		});

		// Execute the computation function using Remove-Execute-Reinsert pattern
		// to avoid RefCell reentrant borrow panics when the closure creates nested effects or memos.
		// An RAII guard ensures the function is reinserted even if the computation panics.
		struct MemoFnGuard {
			memo_id: NodeId,
			memo_fn_box: Option<Box<dyn core::any::Any>>,
		}

		impl Drop for MemoFnGuard {
			fn drop(&mut self) {
				if let Some(f) = self.memo_fn_box.take() {
					MEMO_FUNCTIONS.with(|storage| {
						storage.borrow_mut().insert(self.memo_id, f);
					});
				}
			}
		}

		let mut guard = MemoFnGuard {
			memo_id,
			memo_fn_box: MEMO_FUNCTIONS.with(|storage| storage.borrow_mut().remove(&memo_id)),
		};

		let result = if let Some(ref mut boxed) = guard.memo_fn_box
			&& let Some(memo_fn) = boxed.downcast_mut::<MemoFn<T>>()
		{
			memo_fn()
		} else {
			panic!("Memo function not found - this should never happen")
		};

		// Pop observer from stack
		with_runtime(|rt| {
			rt.pop_observer();
		});

		result
	}

	/// Get the current value of the memo
	///
	/// This automatically tracks the dependency if called from within an Effect or Memo.
	/// If the memo is dirty (dependencies have changed), it will recompute before returning.
	///
	/// # Example
	///
	/// ```no_run
	/// use reinhardt_core::reactive::{Signal, Memo};
	///
	/// let count = Signal::new(5);
	/// let count_clone = count.clone();
	/// let doubled = Memo::new(move || count_clone.get() * 2);
	///
	/// assert_eq!(doubled.get(), 10);
	///
	/// count.set(10);
	/// assert_eq!(doubled.get(), 20); // Recomputes here
	/// ```
	pub fn get(&self) -> T {
		if *self.disposed.borrow() {
			panic!("Attempted to access a disposed Memo");
		}

		// Track dependency with the runtime
		with_runtime(|rt| rt.track_dependency(self.id));

		// Check if we need to recompute (either typed dirty bit OR the
		// type-agnostic dirty flag flipped by `Memo::new_with_deps`'s
		// notifier).
		let needs_recompute = MEMO_VALUES.with(|storage| {
			let storage = storage.borrow();
			if let Some(boxed) = storage.get(&self.id)
				&& let Some(state) = boxed.downcast_ref::<MemoState<T>>()
			{
				return state.dirty;
			}
			// If not found, we need to recompute
			true
		}) || is_memo_dirty_externally(self.id);

		if needs_recompute {
			// Recompute the value
			let new_value = Self::compute_value(self.id);

			// Update the cached value
			MEMO_VALUES.with(|storage| {
				let mut storage = storage.borrow_mut();
				if let Some(boxed) = storage.get_mut(&self.id)
					&& let Some(state) = boxed.downcast_mut::<MemoState<T>>()
				{
					state.value = new_value.clone();
					state.dirty = false;
				}
			});
			clear_memo_dirty(self.id);

			new_value
		} else {
			// Return cached value
			MEMO_VALUES.with(|storage| {
				let storage = storage.borrow();
				if let Some(boxed) = storage.get(&self.id)
					&& let Some(state) = boxed.downcast_ref::<MemoState<T>>()
				{
					return state.value.clone();
				}
				panic!("Memo value not found - this should never happen");
			})
		}
	}

	/// Get the current value without tracking dependencies
	///
	/// This is useful when you want to read a memo's value without creating
	/// a dependency relationship.
	pub fn get_untracked(&self) -> T {
		if *self.disposed.borrow() {
			panic!("Attempted to access a disposed Memo");
		}

		// Check if dirty and recompute if needed (typed bit OR external).
		let needs_recompute = MEMO_VALUES.with(|storage| {
			let storage = storage.borrow();
			if let Some(boxed) = storage.get(&self.id)
				&& let Some(state) = boxed.downcast_ref::<MemoState<T>>()
			{
				return state.dirty;
			}
			true
		}) || is_memo_dirty_externally(self.id);

		if needs_recompute {
			let new_value = Self::compute_value(self.id);
			MEMO_VALUES.with(|storage| {
				let mut storage = storage.borrow_mut();
				if let Some(boxed) = storage.get_mut(&self.id)
					&& let Some(state) = boxed.downcast_mut::<MemoState<T>>()
				{
					state.value = new_value.clone();
					state.dirty = false;
				}
			});
			clear_memo_dirty(self.id);
			new_value
		} else {
			MEMO_VALUES.with(|storage| {
				let storage = storage.borrow();
				if let Some(boxed) = storage.get(&self.id)
					&& let Some(state) = boxed.downcast_ref::<MemoState<T>>()
				{
					return state.value.clone();
				}
				panic!("Memo value not found - this should never happen");
			})
		}
	}

	/// Mark this memo as dirty (needs recomputation)
	///
	/// This is called internally by the runtime when a dependency changes.
	/// It's also exposed for testing purposes.
	pub fn mark_dirty(&self) {
		MEMO_VALUES.with(|storage| {
			let mut storage = storage.borrow_mut();
			if let Some(boxed) = storage.get_mut(&self.id)
				&& let Some(state) = boxed.downcast_mut::<MemoState<T>>()
			{
				state.dirty = true;
			}
		});

		// Notify dependents that this memo has changed
		with_runtime(|rt| rt.notify_signal_change(self.id));
	}

	/// Get the NodeId of this memo (for testing)
	pub fn id(&self) -> NodeId {
		self.id
	}

	/// Dispose this memo
	///
	/// After calling this, the memo will no longer work and its resources will be cleaned up.
	pub fn dispose(&self) {
		*self.disposed.borrow_mut() = true;

		// Remove from runtime's dependency graph (ignore if TLS is destroyed)
		let _ = try_with_runtime(|rt| rt.remove_node(self.id));

		// Remove from storage (ignore if TLS is destroyed)
		let _ = MEMO_FUNCTIONS.try_with(|storage| {
			storage.borrow_mut().remove(&self.id);
		});
		let _ = MEMO_VALUES.try_with(|storage| {
			storage.borrow_mut().remove(&self.id);
		});
		let _ = MEMO_DIRTY.try_with(|storage| {
			storage.borrow_mut().remove(&self.id);
		});
	}
}

impl<T: Clone + 'static> Drop for Memo<T> {
	fn drop(&mut self) {
		// Only dispose when this is the last live clone. `Memo<T>` is `Clone`
		// and every clone shares the same `disposed` Rc (and the same `id` /
		// global storage entry). Disposing on every clone drop would set the
		// shared `disposed` flag and clear `MEMO_FUNCTIONS` / `MEMO_VALUES`
		// while sibling clones are still in use, causing a spurious
		// "Attempted to access a disposed Memo" panic on the next `get()`.
		//
		// `strong_count == 1` means this is the only remaining reference, so
		// cleanup is safe. Mirrors the last-clone-cleanup guard in
		// `Signal<T>`'s `Drop` impl.
		if Rc::strong_count(&self.disposed) == 1 {
			self.dispose();
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::reactive::Signal;
	use serial_test::serial;

	#[test]
	#[serial]
	fn test_memo_creation() {
		let memo = Memo::new(|| 42);
		assert_eq!(memo.get(), 42);
	}

	#[test]
	#[serial]
	fn test_memo_caching() {
		let compute_count = Rc::new(RefCell::new(0));
		let compute_count_clone = compute_count.clone();

		let memo = Memo::new(move || {
			*compute_count_clone.borrow_mut() += 1;
			42
		});

		// First access computes
		assert_eq!(memo.get(), 42);
		assert_eq!(*compute_count.borrow(), 1);

		// Second access uses cache
		assert_eq!(memo.get(), 42);
		assert_eq!(*compute_count.borrow(), 1); // Still 1, not 2
	}

	#[test]
	#[serial]
	fn test_memo_with_signal_dependency() {
		let signal = Signal::new(5);
		let signal_clone = signal.clone();

		let memo = Memo::new(move || signal_clone.get() * 2);

		// Initial value
		assert_eq!(memo.get(), 10);

		// Change signal and mark memo dirty manually (in real system, runtime does this)
		signal.set(10);
		memo.mark_dirty();

		// Memo should recompute
		assert_eq!(memo.get(), 20);
	}

	#[test]
	#[serial]
	fn test_memo_clone() {
		let memo1 = Memo::new(|| 42);
		let memo2 = memo1.clone();

		assert_eq!(memo1.get(), 42);
		assert_eq!(memo2.get(), 42);
	}

	#[test]
	#[serial]
	fn test_memo_dependency_tracking() {
		let signal = Signal::new(1);
		let signal_clone = signal.clone();

		let memo = Memo::new(move || signal_clone.get() + 10);

		// Access the memo inside an effect-like observer
		with_runtime(|rt| {
			let observer_id = NodeId::new();
			rt.push_observer(Observer {
				id: observer_id,
				node_type: NodeType::Effect,
				timing: EffectTiming::default(), // Test observer uses default timing
				cleanup: None,
			});

			// This should track the dependency
			let _ = memo.get();

			rt.pop_observer();

			// Verify dependency was tracked
			let graph = rt.dependency_graph.borrow();
			let memo_node = graph.get(&memo.id()).unwrap();
			assert!(memo_node.subscribers.contains(&observer_id));
		});
	}

	// Note: Memo chain test removed due to Drop ordering issues with thread-local storage.
	// While chained memos are a valid pattern, the test creates Drop ordering complexities
	// with TLS. In production code, memo chains work correctly during normal execution;
	// the issue only manifests during test cleanup.

	#[rstest::rstest]
	#[serial]
	fn test_memo_creates_effect_during_computation() {
		// Arrange
		let effect_ran = Rc::new(RefCell::new(false));
		let effect_ran_clone = effect_ran.clone();

		// Act - create a memo whose computation creates an effect
		let memo = Memo::new(move || {
			use crate::reactive::Effect;
			let ran = effect_ran_clone.clone();
			let _effect = Effect::new(move || {
				*ran.borrow_mut() = true;
			});
			42
		});

		// Assert - memo returns the correct value and nested effect executed
		assert_eq!(memo.get(), 42);
		assert!(*effect_ran.borrow());
	}

	#[test]
	#[serial]
	fn test_memo_get_untracked() {
		let signal = Signal::new(5);
		let signal_clone = signal.clone();

		let memo = Memo::new(move || signal_clone.get() * 2);

		with_runtime(|rt| {
			let observer_id = NodeId::new();
			rt.push_observer(Observer {
				id: observer_id,
				node_type: NodeType::Effect,
				timing: EffectTiming::default(), // Test observer uses default timing
				cleanup: None,
			});

			// get_untracked should not create dependency
			let _ = memo.get_untracked();

			rt.pop_observer();

			// Verify NO dependency was tracked
			let graph = rt.dependency_graph.borrow();
			if let Some(memo_node) = graph.get(&memo.id()) {
				assert!(!memo_node.subscribers.contains(&observer_id));
			}
		});
	}

	#[test]
	#[serial]
	fn new_with_deps_recomputes_only_on_listed_dep() {
		use core::cell::Cell;

		// Arrange
		let listed = Signal::new(2_i32);
		let unlisted = Signal::new(100_i32);
		let computations = Rc::new(Cell::new(0_i32));
		let computations_for_memo = computations.clone();
		let listed_for_memo = listed.clone();
		let unlisted_for_memo = unlisted.clone();
		let deps = crate::reactive::deps::Deps::from_signals(&[listed.id()]);

		// Act
		let memo = Memo::new_with_deps(
			move || {
				computations_for_memo.set(computations_for_memo.get() + 1);
				listed_for_memo.get() * 10 + unlisted_for_memo.get()
			},
			deps,
		);
		// Force initial value materialization (also exercises the
		// no-spurious-recompute path through the cleared MEMO_DIRTY entry).
		let _ = memo.get();
		let before_changes = computations.get();

		unlisted.set(200);
		let _ = memo.get();
		let after_unlisted = computations.get();

		listed.set(3);
		let _ = memo.get();
		let after_listed = computations.get();

		// Assert
		assert_eq!(
			after_unlisted, before_changes,
			"memo MUST NOT recompute when unlisted Signal changes (Option A)"
		);
		assert_eq!(
			after_listed,
			before_changes + 1,
			"memo MUST recompute exactly once when listed dep changes"
		);
	}
}