use serde::{Deserialize, Serialize};
use std::any::Any;
#[cfg(any(client, doc))]
use sycamore::prelude::Scope;
pub trait MakeRx {
type Rx: MakeUnrx;
#[cfg(debug_assertions)]
const HSR_IGNORE: bool = false;
fn make_rx(self) -> Self::Rx;
}
pub trait MakeUnrx {
type Unrx: Serialize + for<'de> Deserialize<'de> + MakeRx;
fn make_unrx(self) -> Self::Unrx;
#[cfg(any(client, doc))]
fn compute_suspense(&self, cx: Scope<'_>);
}
pub trait Freeze {
fn freeze(&self) -> String;
}
pub trait AnyFreeze: Freeze + Any {
fn as_any(&self) -> &dyn Any;
}
impl<T: Any + Freeze> AnyFreeze for T {
fn as_any(&self) -> &dyn Any {
self
}
}
impl std::fmt::Debug for (dyn AnyFreeze + 'static) {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AnyFreeze").finish_non_exhaustive()
}
}
pub trait UnreactiveState {}
#[derive(Clone, Debug)]
pub struct UnreactiveStateWrapper<
T: Serialize + for<'de> Deserialize<'de> + UnreactiveState + Clone,
>(pub T);
impl<T: Serialize + for<'de> Deserialize<'de> + UnreactiveState + Clone> MakeRx for T {
type Rx = UnreactiveStateWrapper<T>;
#[cfg(debug_assertions)]
const HSR_IGNORE: bool = true;
fn make_rx(self) -> Self::Rx {
UnreactiveStateWrapper(self)
}
}
impl<T: Serialize + for<'de> Deserialize<'de> + UnreactiveState + Clone> MakeUnrx
for UnreactiveStateWrapper<T>
{
type Unrx = T;
fn make_unrx(self) -> Self::Unrx {
self.0
}
#[cfg(any(client, doc))]
fn compute_suspense(&self, _cx: Scope) {}
}
impl<T: Serialize + for<'de> Deserialize<'de> + UnreactiveState + Clone> Freeze
for UnreactiveStateWrapper<T>
{
fn freeze(&self) -> String {
serde_json::to_string(&self.0).unwrap()
}
}