use std::cell::RefCell;
use std::marker::PhantomData;
use std::rc::Rc;
use crate::handle::{Marker, handle_impls};
use crate::runtime::{self, NodeId, drop_isolated};
pub struct Signal<T> {
id: NodeId,
_marker: Marker<T>,
}
handle_impls!(Signal<T>);
impl<T: 'static> Signal<T> {
#[track_caller]
pub(crate) fn new(owner: NodeId, value: T) -> Self {
let value: Rc<RefCell<Option<T>>> = Rc::new(RefCell::new(Some(value)));
Signal {
id: runtime::create_signal(owner, value),
_marker: PhantomData,
}
}
#[cfg(test)]
pub(crate) fn subscriber_count(&self) -> usize {
runtime::subscriber_count(self.id)
}
#[cfg(test)]
pub(crate) fn slot(&self) -> (u32, u32) {
runtime::slot_of(self.id)
}
pub fn read_only(&self) -> ReadSignal<T> {
ReadSignal {
id: self.id,
_marker: PhantomData,
}
}
#[track_caller]
pub fn get(&self) -> T
where
T: Clone,
{
self.with(T::clone)
}
#[track_caller]
pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
runtime::read_or_panic(self.id, true, "Signal", f)
}
#[track_caller]
pub fn get_untracked(&self) -> T
where
T: Clone,
{
self.with_untracked(T::clone)
}
#[track_caller]
pub fn with_untracked<R>(&self, f: impl FnOnce(&T) -> R) -> R {
runtime::read_or_panic(self.id, false, "Signal", f)
}
#[track_caller]
pub fn try_get(&self) -> Option<T>
where
T: Clone,
{
self.try_with(T::clone)
}
#[track_caller]
pub fn try_with<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R> {
runtime::read(self.id, true, "Signal", f).ok()
}
#[track_caller]
pub fn set(&self, value: T) {
if let Some(old) = runtime::write_or_panic(self.id, |slot: &mut Option<T>| slot.replace(value))
{
drop_isolated(old);
}
}
#[track_caller]
pub fn update(&self, f: impl FnOnce(&mut T)) {
runtime::write_or_panic(self.id, |slot: &mut Option<T>| update_slot(slot, f));
}
#[track_caller]
pub fn try_set(&self, value: T) -> bool {
match runtime::write(self.id, |slot: &mut Option<T>| slot.replace(value)) {
Ok(old) => {
drop_isolated(old);
true
}
Err(_) => false,
}
}
#[track_caller]
pub fn try_update(&self, f: impl FnOnce(&mut T)) -> bool {
runtime::write(self.id, |slot: &mut Option<T>| update_slot(slot, f)).is_ok()
}
pub fn named(self, name: impl Into<String>) -> Self {
runtime::set_name(self.id, name.into());
self
}
}
fn update_slot<T>(slot: &mut Option<T>, f: impl FnOnce(&mut T)) {
f(slot
.as_mut()
.expect("invariant: a signal always holds a value"));
}
pub struct ReadSignal<T> {
id: NodeId,
_marker: Marker<T>,
}
handle_impls!(ReadSignal<T>);
impl<T: 'static> ReadSignal<T> {
#[track_caller]
pub fn get(&self) -> T
where
T: Clone,
{
self.with(T::clone)
}
#[track_caller]
pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
runtime::read_or_panic(self.id, true, "ReadSignal", f)
}
#[track_caller]
pub fn get_untracked(&self) -> T
where
T: Clone,
{
self.with_untracked(T::clone)
}
#[track_caller]
pub fn with_untracked<R>(&self, f: impl FnOnce(&T) -> R) -> R {
runtime::read_or_panic(self.id, false, "ReadSignal", f)
}
#[track_caller]
pub fn try_get(&self) -> Option<T>
where
T: Clone,
{
self.try_with(T::clone)
}
#[track_caller]
pub fn try_with<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R> {
runtime::read(self.id, true, "ReadSignal", f).ok()
}
}