use crate::runtime::RuntimeInner;
use crate::{Effect, Memo, Signal};
use std::cell::RefCell;
use std::rc::Rc;
pub struct Scope {
runtime: Rc<RefCell<RuntimeInner>>,
}
impl Scope {
pub fn new() -> Self {
Self {
runtime: Rc::new(RefCell::new(RuntimeInner::new())),
}
}
pub fn signal<T>(&self, value: T) -> Signal<T>
where
T: Clone + 'static,
{
let id = self.runtime.borrow().allocate_id();
Signal::new_in_scope(value, id, Rc::clone(&self.runtime))
}
pub fn memo<T, F>(&self, compute: F) -> Memo<T>
where
T: Clone + 'static,
F: Fn() -> T + 'static,
{
let id = self.runtime.borrow().allocate_id();
Memo::new_in_scope(compute, id, Rc::clone(&self.runtime))
}
pub fn effect<F>(&self, effect: F) -> Effect
where
F: Fn() + 'static,
{
let id = self.runtime.borrow().allocate_id();
Effect::new_in_scope(effect, id, Rc::clone(&self.runtime))
}
pub fn clear(&self) {
self.runtime.borrow().clear();
}
}
impl Default for Scope {
fn default() -> Self {
Self::new()
}
}