create global selectors

This commit is contained in:
Evan Almloff committed 2024-01-19 15:26:28 -06:00
1 parent e4c84f91e1
commit c859ed3b12
4 files changed
+197 -19

No files matched your search

+23 -1
View File
@@ -1,4 +1,4 @@
use crate::{GlobalSignal, MappedSignal};
use crate::{GlobalSelector, GlobalSignal, MappedSignal};
use std::{
cell::RefCell,
marker::PhantomData,
@@ -225,6 +225,14 @@ impl<T: 'static> Signal<T> {
pub const fn global(constructor: fn() -> T) -> GlobalSignal<T> {
GlobalSignal::new(constructor)
}
/// Creates a new global Signal that can be used in a global static.
pub const fn global_selector(constructor: fn() -> T) -> GlobalSelector<T>
where
T: PartialEq,
{
GlobalSelector::new(constructor)
}
}
impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
@@ -626,6 +634,20 @@ impl<T: 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
pub fn with<O>(&self, f: impl FnOnce(&T) -> O) -> O {
self.inner.with(f)
}
/// Get the id of the signal.
pub fn id(&self) -> generational_box::GenerationalBoxId {
self.inner.id()
}
}
impl<T> IntoAttributeValue for ReadOnlySignal<T>
where
T: Clone + IntoAttributeValue,
{
fn into_value(self) -> dioxus_core::AttributeValue {
self.with(|f| f.clone().into_value())
}
}
impl<T: Clone + 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {