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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! Template refs — `pp-ref="name"` pins the decorated element under
//! its enclosing scope so Rust handlers can reach it imperatively.
//!
//! Two resolution flavours are available on the same `pp-ref` name:
//!
//! 1. **DOM element** ([`get`] / [`get_as`]). The element decorated
//! with `pp-ref="search"` returns from `refs::get("search")` as an
//! `Element`; `refs::get_as::<HtmlInputElement>("search")` downcasts.
//! 2. **Typed child-component handle** ([`get_component`]). When the
//! decorated element happens to be a child *component*'s host
//! (RFC 081), `refs::get_component::<Child>("body")` resolves a
//! [`Handle<Child>`](crate::handle::Handle) for the child's Rust
//! state — same primitive used by `Parent<T>` / `this::<T>()`,
//! mirror direction.
//!
//! ```ignore
//! use pocopine::prelude::*;
//! use pocopine::refs;
//! use web_sys::HtmlInputElement;
//!
//! #[handlers]
//! impl SearchBar {
//! pub fn init(&mut self) {
//! // DOM element flavour
//! if let Some(input) = refs::get_as::<HtmlInputElement>("search") {
//! let _ = input.focus();
//! }
//! }
//!
//! pub fn save(&mut self) {
//! // Child-component handle flavour
//! if let Some(body) = refs::get_component::<KeepNoteBody>("body") {
//! let md = body.with(|b| b.editor()?.get::<Markdown>().ok())?;
//! }
//! }
//! }
//! ```
//!
//! Refs resolve against the current handler's scope (the one on the
//! call stack via [`crate::scope::current_scope_id`]) so the same
//! name used in two sibling components doesn't collide. Scope eviction
//! ([`crate::scope::Scope::remove`]) also clears the scope's refs.
use RefCell;
use HashMap;
use JsCast;
use Element;
use crateHandle;
use crateScopeId;
use cratecurrent_scope_id;
thread_local!
/// Register `el` as the ref named `name` on `scope_id`. Called by the
/// `pp-ref` directive during walk.
/// Look up a ref on the current handler's scope. Returns `None`
/// outside of a handler invocation or when no ref with that name is
/// registered.
/// Typed convenience — downcasts the looked-up `Element` to `T`. Fails
/// silently (returns `None`) on downcast mismatch.
/// Look up a ref on an explicit scope. Useful for code that knows the
/// scope id (e.g. stored inside an async task).
/// Drop every ref registered on `scope_id`. Called by scope teardown
/// so evicted components don't leak their element handles.
/// Resolve a typed [`Handle<T>`] for the child component whose host
/// element was tagged `pp-ref="name"` in the current handler's scope.
///
/// Returns `None` when:
/// - no `pp-ref` of that name exists in the current scope (also
/// covered by [`get`] returning `None`),
/// - the tagged element is a plain DOM element rather than a child
/// component host,
/// - the registered child component's Rust type doesn't match `T`,
/// - the call site has no live scope context (e.g. fired from a
/// `tick::next` continuation after the parent's handler returned).
///
/// Implementation: the host element of every mounted child component
/// carries the child's `SCOPE_ID_KEY` (set in
/// [`crate::mount::mount_component`]); this helper reads it via
/// [`crate::mount::scope_id_of_element`] and looks up the typed
/// component state via [`crate::scope::Scope::typed`]. No DOM walk.
/// Explicit-scope variant of [`get_component`]. Useful when the
/// calling code already holds a [`ScopeId`] (e.g. cached at on_ready
/// time for use inside a `tick::next` continuation, mirroring
/// [`get_on`]).
/// Compile-time-named ref accessor (RFC 081 Layer 1). Carries the
/// scope id + the ref name baked in by `#[component]` codegen, so
/// callers reach all three resolution flavours through one entry
/// point without restating the name:
///
/// ```ignore
/// fn save(&self, refs: KeepNoteFormRefs) {
/// let el = refs.body().element(); // Option<Element>
/// let input = refs.title_input().as_::<HtmlInputElement>(); // Option<HtmlInputElement>
/// let body = refs.body().component::<KeepNoteBody>(); // Option<Handle<KeepNoteBody>>
/// }
/// ```
///
/// Constructed by macro-emitted accessors — there's no reason for
/// authors to build one by hand. The `name` field is a
/// `&'static str` so a typo in a generated method body would
/// surface at macro-expand time (before user code compiles).
/// Build a plain JS object snapshot of every ref registered on
/// `scope_id`. Used to resolve the `$refs` magic — templates can read
/// `$refs.search` without importing anything.