use crate::{AtomId, AtomRoot, Writable};
use dioxus_core::{ScopeId, ScopeState};
use std::{
cell::RefMut,
fmt::{Debug, Display},
ops::{Add, Div, Mul, Not, Sub},
rc::Rc,
};
#[must_use]
pub fn use_atom_state<T: 'static>(cx: &ScopeState, f: impl Writable<T>) -> &AtomState<T> {
let root = crate::use_atom_root(cx);
let inner = cx.use_hook(|| AtomState {
value: None,
root: root.clone(),
scope_id: cx.scope_id(),
id: f.unique_id(),
});
inner.value = Some(inner.root.register(f, cx.scope_id()));
inner
}
pub struct AtomState<V: 'static> {
root: Rc<AtomRoot>,
id: AtomId,
scope_id: ScopeId,
value: Option<Rc<V>>,
}
impl<V> Drop for AtomState<V> {
fn drop(&mut self) {
self.root.unsubscribe(self.id, self.scope_id)
}
}
impl<T: 'static> AtomState<T> {
pub fn set(&self, new: T) {
self.root.set(self.id, new)
}
#[must_use]
pub fn current(&self) -> Rc<T> {
let atoms = self.root.atoms.borrow();
let slot = atoms.get(&self.id).unwrap();
slot.value.clone().downcast().unwrap()
}
#[must_use]
pub fn setter(&self) -> Rc<dyn Fn(T)> {
let root = self.root.clone();
let id = self.id;
Rc::new(move |new_val| root.set(id, new_val))
}
pub fn modify(&self, f: impl FnOnce(&T) -> T) {
self.root.clone().set(self.id, {
let current = self.value.as_ref().unwrap();
f(current.as_ref())
});
}
#[must_use]
pub fn get(&self) -> &T {
self.value.as_ref().unwrap()
}
#[must_use]
pub fn get_rc(&self) -> &Rc<T> {
self.value.as_ref().unwrap()
}
pub fn needs_update(&self) {
self.root.force_update(self.id)
}
}
impl<T: Clone> AtomState<T> {
pub fn with_mut(&self, apply: impl FnOnce(&mut T)) {
let mut new_val = self.value.as_ref().unwrap().as_ref().to_owned();
apply(&mut new_val);
self.set(new_val);
}
#[must_use]
pub fn make_mut(&self) -> RefMut<T> {
todo!("make mut not support for atom values yet")
}
#[must_use]
pub fn split(&self) -> (&T, &Self) {
(self.value.as_ref().unwrap(), self)
}
}
impl<T: 'static> Clone for AtomState<T> {
fn clone(&self) -> Self {
AtomState {
root: self.root.clone(),
id: self.id,
scope_id: self.scope_id,
value: self.value.clone(),
}
}
}
impl<T: 'static + Display> std::fmt::Display for AtomState<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.value.as_ref().unwrap())
}
}
impl<T: std::fmt::Binary> std::fmt::Binary for AtomState<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:b}", self.value.as_ref().unwrap().as_ref())
}
}
impl<T: PartialEq> PartialEq<T> for AtomState<T> {
fn eq(&self, other: &T) -> bool {
self.value.as_ref().unwrap().as_ref() == other
}
}
impl PartialEq<bool> for &AtomState<bool> {
fn eq(&self, other: &bool) -> bool {
self.value.as_ref().unwrap().as_ref() == other
}
}
impl<T: PartialEq> PartialEq<AtomState<T>> for AtomState<T> {
fn eq(&self, other: &AtomState<T>) -> bool {
Rc::ptr_eq(self.value.as_ref().unwrap(), other.value.as_ref().unwrap())
}
}
impl<T: Debug> Debug for AtomState<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.value.as_ref().unwrap())
}
}
impl<T> std::ops::Deref for AtomState<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.value.as_ref().unwrap().as_ref()
}
}
impl<T: Not + Copy> std::ops::Not for &AtomState<T> {
type Output = <T as std::ops::Not>::Output;
fn not(self) -> Self::Output {
self.value.as_ref().unwrap().not()
}
}
impl<T: Not + Copy> std::ops::Not for AtomState<T> {
type Output = <T as std::ops::Not>::Output;
fn not(self) -> Self::Output {
self.value.as_ref().unwrap().not()
}
}
impl<T: std::ops::Add + Copy> std::ops::Add<T> for &AtomState<T> {
type Output = <T as std::ops::Add>::Output;
fn add(self, other: T) -> Self::Output {
*self.value.as_ref().unwrap().as_ref() + other
}
}
impl<T: std::ops::Sub + Copy> std::ops::Sub<T> for &AtomState<T> {
type Output = <T as std::ops::Sub>::Output;
fn sub(self, other: T) -> Self::Output {
*self.value.as_ref().unwrap().as_ref() - other
}
}
impl<T: std::ops::Div + Copy> std::ops::Div<T> for &AtomState<T> {
type Output = <T as std::ops::Div>::Output;
fn div(self, other: T) -> Self::Output {
*self.value.as_ref().unwrap().as_ref() / other
}
}
impl<T: std::ops::Mul + Copy> std::ops::Mul<T> for &AtomState<T> {
type Output = <T as std::ops::Mul>::Output;
fn mul(self, other: T) -> Self::Output {
*self.value.as_ref().unwrap().as_ref() * other
}
}
impl<T: Add<Output = T> + Copy> std::ops::AddAssign<T> for &AtomState<T> {
fn add_assign(&mut self, rhs: T) {
self.set((*self.current()) + rhs);
}
}
impl<T: Sub<Output = T> + Copy> std::ops::SubAssign<T> for &AtomState<T> {
fn sub_assign(&mut self, rhs: T) {
self.set((*self.current()) - rhs);
}
}
impl<T: Mul<Output = T> + Copy> std::ops::MulAssign<T> for &AtomState<T> {
fn mul_assign(&mut self, rhs: T) {
self.set((*self.current()) * rhs);
}
}
impl<T: Div<Output = T> + Copy> std::ops::DivAssign<T> for &AtomState<T> {
fn div_assign(&mut self, rhs: T) {
self.set((*self.current()) / rhs);
}
}
impl<T: Add<Output = T> + Copy> std::ops::AddAssign<T> for AtomState<T> {
fn add_assign(&mut self, rhs: T) {
self.set((*self.current()) + rhs);
}
}
impl<T: Sub<Output = T> + Copy> std::ops::SubAssign<T> for AtomState<T> {
fn sub_assign(&mut self, rhs: T) {
self.set((*self.current()) - rhs);
}
}
impl<T: Mul<Output = T> + Copy> std::ops::MulAssign<T> for AtomState<T> {
fn mul_assign(&mut self, rhs: T) {
self.set((*self.current()) * rhs);
}
}
impl<T: Div<Output = T> + Copy> std::ops::DivAssign<T> for AtomState<T> {
fn div_assign(&mut self, rhs: T) {
self.set((*self.current()) / rhs);
}
}