floem_reactive/
base.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::marker::PhantomData;

use crate::{
    id::Id, signal::Signal, ReadSignal, RwSignal, SignalGet, SignalUpdate, SignalWith, WriteSignal,
};

/// BaseSignal gives you another way to control the lifetime of a Signal
/// apart from Scope.
///
/// When BaseSignal is dropped, it will dispose the underlying Signal as well.
/// The signal isn't put in any Scope when a BaseSignal is created, so that
/// the lifetime of the signal can only be determined by BaseSignal rather than
/// Scope dependencies
pub struct BaseSignal<T> {
    id: Id,
    ty: PhantomData<T>,
}

impl<T> Eq for BaseSignal<T> {}

impl<T> PartialEq for BaseSignal<T> {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl<T> Drop for BaseSignal<T> {
    fn drop(&mut self) {
        self.id.dispose();
    }
}

pub fn create_base_signal<T: 'static>(value: T) -> BaseSignal<T> {
    let id = Signal::create(value);
    BaseSignal {
        id,
        ty: PhantomData,
    }
}

impl<T> BaseSignal<T> {
    /// Create a RwSignal of this Signal
    pub fn rw(&self) -> RwSignal<T> {
        RwSignal {
            id: self.id,
            ty: PhantomData,
        }
    }

    /// Create a Getter of this Signal
    pub fn read_only(&self) -> ReadSignal<T> {
        ReadSignal {
            id: self.id,
            ty: PhantomData,
        }
    }

    /// Create a Setter of this Signal
    pub fn write_only(&self) -> WriteSignal<T> {
        WriteSignal {
            id: self.id,
            ty: PhantomData,
        }
    }
}

impl<T: Clone> SignalGet<T> for BaseSignal<T> {
    fn id(&self) -> Id {
        self.id
    }
}

impl<T> SignalWith<T> for BaseSignal<T> {
    fn id(&self) -> Id {
        self.id
    }
}

impl<T> SignalUpdate<T> for BaseSignal<T> {
    fn id(&self) -> Id {
        self.id
    }
}