-
-
Notifications
You must be signed in to change notification settings - Fork 711
/
Copy pathnode_ref.rs
168 lines (151 loc) · 3.58 KB
/
node_ref.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use crate::html::{element::ElementType, node_ref::NodeRefContainer};
use reactive_graph::{
effect::Effect,
signal::{
guards::{Derefable, ReadGuard},
RwSignal,
},
traits::{
DefinedAt, Get, Notify, ReadUntracked, Set, Track, UntrackableGuard,
Write,
},
};
use send_wrapper::SendWrapper;
use std::{cell::Cell, ops::DerefMut};
use wasm_bindgen::JsCast;
/// A reactive reference to a DOM node that can be used with the `node_ref` attribute.
#[derive(Debug)]
pub struct NodeRef<E>(RwSignal<Option<SendWrapper<E::Output>>>)
where
E: ElementType,
E::Output: 'static;
impl<E> NodeRef<E>
where
E: ElementType,
E::Output: 'static,
{
/// Creates a new node reference.
#[track_caller]
pub fn new() -> Self {
Self(RwSignal::new(None))
}
/// Runs the provided closure when the `NodeRef` has been connected
/// with its element.
#[inline(always)]
pub fn on_load<F>(self, f: F)
where
E: 'static,
F: FnOnce(E::Output) + 'static,
E: ElementType,
E::Output: JsCast + Clone + 'static,
{
let f = Cell::new(Some(f));
Effect::new(move |_| {
if let Some(node_ref) = self.get() {
f.take().unwrap()(node_ref);
}
});
}
}
impl<E> Default for NodeRef<E>
where
E: ElementType,
E::Output: 'static,
{
fn default() -> Self {
Self::new()
}
}
impl<E> Clone for NodeRef<E>
where
E: ElementType,
E::Output: 'static,
{
fn clone(&self) -> Self {
*self
}
}
impl<E> Copy for NodeRef<E>
where
E: ElementType,
E::Output: 'static,
{
}
impl<E> NodeRefContainer<E> for NodeRef<E>
where
E: ElementType,
E::Output: JsCast + 'static,
{
fn load(self, el: &crate::renderer::types::Element) {
// safe to construct SendWrapper here, because it will only run in the browser
// so it will always be accessed or dropped from the main thread
self.0
.set(Some(SendWrapper::new(el.clone().unchecked_into())));
}
}
impl<E> DefinedAt for NodeRef<E>
where
E: ElementType,
E::Output: JsCast + 'static,
{
fn defined_at(&self) -> Option<&'static std::panic::Location<'static>> {
self.0.defined_at()
}
}
impl<E> Notify for NodeRef<E>
where
E: ElementType,
E::Output: JsCast + Clone + 'static,
{
fn notify(&self) {
self.0.notify();
}
}
impl<E> Write for NodeRef<E>
where
E: ElementType,
E::Output: JsCast + Clone + 'static,
{
type Value = Option<SendWrapper<E::Output>>;
fn try_write(&self) -> Option<impl UntrackableGuard<Target = Self::Value>> {
self.0.try_write()
}
fn try_write_untracked(
&self,
) -> Option<impl DerefMut<Target = Self::Value>> {
self.0.try_write_untracked()
}
}
impl<E> ReadUntracked for NodeRef<E>
where
E: ElementType,
E::Output: JsCast + Clone + 'static,
{
type Value = ReadGuard<Option<E::Output>, Derefable<Option<E::Output>>>;
fn try_read_untracked(&self) -> Option<Self::Value> {
Some(ReadGuard::new(Derefable(
self.0.try_read_untracked()?.as_deref().cloned(),
)))
}
}
impl<E> Track for NodeRef<E>
where
E: ElementType,
E::Output: JsCast + 'static,
{
fn track(&self) {
self.0.track();
}
}
/// Create a [NodeRef].
#[inline(always)]
#[track_caller]
#[deprecated = "This function is being removed to conform to Rust idioms. \
Please use `NodeRef::new()` instead."]
pub fn create_node_ref<E>() -> NodeRef<E>
where
E: ElementType,
E::Output: 'static,
{
NodeRef::new()
}