use crate::{read::Readable, ReadableRef};
use dioxus_core::prelude::{IntoAttributeValue, ScopeId};
use generational_box::UnsyncStorage;
use std::{mem::MaybeUninit, ops::Deref};
use crate::{ReadOnlySignal, Signal};
use super::get_global_context;
pub struct GlobalMemo<T: 'static> {
selector: fn() -> T,
}
impl<T: PartialEq + 'static> GlobalMemo<T> {
pub const fn new(selector: fn() -> T) -> GlobalMemo<T>
where
T: PartialEq,
{
GlobalMemo { selector }
}
pub fn signal(&self) -> ReadOnlySignal<T> {
let key = self as *const _ as *const ();
let context = get_global_context();
let read = context.signal.borrow();
match read.get(&key) {
Some(signal) => *signal.downcast_ref::<ReadOnlySignal<T>>().unwrap(),
None => {
drop(read);
let signal = ScopeId::ROOT.in_runtime(|| Signal::memo(self.selector));
context.signal.borrow_mut().insert(key, Box::new(signal));
signal
}
}
}
pub fn origin_scope(&self) -> ScopeId {
ScopeId::ROOT
}
pub fn id(&self) -> generational_box::GenerationalBoxId {
self.signal().id()
}
}
impl<T: PartialEq + 'static> Readable for GlobalMemo<T> {
type Target = T;
type Storage = UnsyncStorage;
#[track_caller]
fn try_read(&self) -> Result<ReadableRef<Self>, generational_box::BorrowError> {
self.signal().try_read()
}
#[track_caller]
fn peek(&self) -> ReadableRef<Self> {
self.signal().peek()
}
}
impl<T: PartialEq + 'static> IntoAttributeValue for GlobalMemo<T>
where
T: Clone + IntoAttributeValue,
{
fn into_value(self) -> dioxus_core::AttributeValue {
self.signal().into_value()
}
}
impl<T: PartialEq + 'static> PartialEq for GlobalMemo<T> {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self, other)
}
}
impl<T: PartialEq + Clone + 'static> Deref for GlobalMemo<T> {
type Target = dyn Fn() -> T;
fn deref(&self) -> &Self::Target {
let uninit_callable = MaybeUninit::<Self>::uninit();
let uninit_closure = move || Self::read(unsafe { &*uninit_callable.as_ptr() }).clone();
let size_of_closure = std::mem::size_of_val(&uninit_closure);
assert_eq!(size_of_closure, std::mem::size_of::<Self>());
fn cast_lifetime<'a, T>(_a: &T, b: &'a T) -> &'a T {
b
}
let reference_to_closure = cast_lifetime(
{
&uninit_closure
},
unsafe { std::mem::transmute(self) },
);
reference_to_closure as &Self::Target
}
}