dioxus_signals/global/
signal.rs

1use super::{Global, InitializeFromFunction};
2use crate::read::Readable;
3use crate::read_impls;
4use crate::Signal;
5
6impl<T> InitializeFromFunction<T> for Signal<T> {
7    fn initialize_from_function(f: fn() -> T) -> Self {
8        Signal::new(f())
9    }
10}
11
12/// A signal that can be accessed from anywhere in the application and created in a static
13pub type GlobalSignal<T> = Global<Signal<T>, T>;
14
15impl<T: 'static> GlobalSignal<T> {
16    /// Get the generational id of the signal.
17    pub fn id(&self) -> generational_box::GenerationalBoxId {
18        self.resolve().id()
19    }
20
21    /// Resolve the global signal. This will try to get the existing value from the current virtual dom, and if it doesn't exist, it will create a new one.
22    pub fn signal(&self) -> Signal<T> {
23        self.resolve()
24    }
25}
26
27read_impls!(GlobalSignal<T>);