use std::marker::PhantomData;
use crate::handle::{NotSend, handle_impls};
use crate::runtime::{self, NodeId};
use crate::{Effect, Memo, Signal};
pub struct Owner {
id: NodeId,
_not_send: NotSend,
}
handle_impls!(Owner);
impl Owner {
#[track_caller]
pub(crate) fn new(parent: Option<NodeId>) -> Self {
Owner {
id: runtime::create_owner(parent),
_not_send: PhantomData,
}
}
#[track_caller]
pub fn child(&self) -> Owner {
Owner::new(Some(self.id))
}
#[track_caller]
pub fn signal<T: 'static>(&self, value: T) -> Signal<T> {
Signal::new(self.id, value)
}
#[track_caller]
pub fn memo<T: PartialEq + 'static>(&self, f: impl FnMut() -> T + 'static) -> Memo<T> {
Memo::new(self.id, f)
}
#[track_caller]
pub fn effect(&self, f: impl FnMut() + 'static) -> Effect {
Effect::new(self.id, f)
}
#[track_caller]
pub fn effect_scoped(&self, mut f: impl FnMut(Owner) + 'static) -> Effect {
self.effect(move || {
f(Owner {
id: runtime::new_scope(),
_not_send: PhantomData,
});
})
}
pub fn is_disposed(&self) -> bool {
runtime::is_disposed(self.id)
}
pub fn dispose(self) {
runtime::dispose(self.id);
}
pub fn named(self, name: impl Into<String>) -> Self {
runtime::set_name(self.id, name.into());
self
}
}