dioxus_signals/global/
signal.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use super::{Global, InitializeFromFunction};
use crate::read::Readable;
use crate::read_impls;
use crate::Signal;

impl<T> InitializeFromFunction<T> for Signal<T> {
    fn initialize_from_function(f: fn() -> T) -> Self {
        Signal::new(f())
    }
}

/// A signal that can be accessed from anywhere in the application and created in a static
pub type GlobalSignal<T> = Global<Signal<T>, T>;

impl<T: 'static> GlobalSignal<T> {
    /// Get the generational id of the signal.
    pub fn id(&self) -> generational_box::GenerationalBoxId {
        self.resolve().id()
    }

    /// 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.
    pub fn signal(&self) -> Signal<T> {
        self.resolve()
    }
}

read_impls!(GlobalSignal<T>);