threadloom-dom 0.1.12

A meticulously crafted Rust full-stack framework.
Documentation
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#![allow(warnings)]
pub use js_sys;
pub use reqwasm;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use threadloom_core::{
    create_effect, run_effects, take_pending_boundaries, AttributeValue, Boundary, NodeId, View,
};
pub use wasm_bindgen;
use wasm_bindgen::prelude::*;
pub use wasm_bindgen_futures;
pub use web_sys;
use web_sys::{Document, Element, Node};

thread_local! {
    static BOUNDARIES: RefCell<HashMap<NodeId, (Node, Rc<RefCell<dyn FnMut() -> View>>)>> = RefCell::new(HashMap::new());
    pub static ROUTER_SETTER: std::cell::RefCell<Option<threadloom_core::WriteSignal<String>>> = std::cell::RefCell::new(None);
}

pub mod storage;
pub mod ws;

pub fn mount(view: View, container: &Element) -> Result<(), JsValue> {
    let window = web_sys::window().expect("no global `window` exists");
    let document = window.document().expect("should have a document on window");

    let node = render_view(&document, view)?;
    container.append_child(&node)?;
    Ok(())
}

fn render_view(document: &Document, view: View) -> Result<Node, JsValue> {
    match view {
        View::Text(text) => Ok(document.create_text_node(&text).into()),
        View::Element {
            tag,
            attrs,
            children,
        } => {
            let el = if tag == "svg"
                || tag == "path"
                || tag == "circle"
                || tag == "rect"
                || tag == "g"
                || tag == "line"
            {
                document.create_element_ns(Some("http://www.w3.org/2000/svg"), &tag)?
            } else {
                document.create_element(&tag)?
            };
            for (k, v) in attrs {
                match v {
                    AttributeValue::String(s) => el.set_attribute(&k, &s)?,
                    AttributeValue::Bool(b) => {
                        if b {
                            el.set_attribute(&k, "")?;
                        }
                    }
                    AttributeValue::Dynamic(f) => {
                        // Evaluate once initially to set the attr, then register a reactive effect
                        // that re-runs (and calls set_attribute again) whenever any signal read
                        // inside the closure changes.
                        let el_clone = el.clone();
                        let k_clone = k.clone();
                        let val = f();
                        if let AttributeValue::String(s) = &val {
                            let _ = el.set_attribute(&k, s);
                        }
                        // Reactive update effect
                        create_effect(move || {
                            let val = f();
                            if let AttributeValue::String(s) = val {
                                let _ = el_clone.set_attribute(&k_clone, &s);
                            }
                        });
                    }
                    AttributeValue::Event(cb) => {
                        use wasm_bindgen::JsCast;
                        let closure = wasm_bindgen::closure::Closure::wrap(Box::new(move || {
                            cb();
                            let _ = crate::tick();
                        })
                            as Box<dyn FnMut()>);
                        el.add_event_listener_with_callback(&k, closure.as_ref().unchecked_ref())?;
                        closure.forget();
                    }
                }
            }
            for child in children {
                let child_node = render_view(document, child)?;
                el.append_child(&child_node)?;
            }
            Ok(el.into())
        }
        View::DynamicNode(boundary) => {
            let view = boundary.id.track(|| {
                let mut compute = boundary.compute.borrow_mut();
                compute()
            });
            let node = render_view(document, view)?;

            let compute_rc = boundary.compute.clone();
            BOUNDARIES.with(|b| {
                b.borrow_mut()
                    .insert(boundary.id, (node.clone(), compute_rc));
            });

            Ok(node)
        }
        View::Fragment(children) => {
            // Very simple stub: return a div wrapping the fragment to avoid complex multi-node tracking
            let el = document.create_element("div")?;
            for child in children {
                let child_node = render_view(document, child)?;
                el.append_child(&child_node)?;
            }
            Ok(el.into())
        }
        View::None => Ok(document.create_text_node("").into()),
    }
}

pub fn tick() -> Result<(), JsValue> {
    let window = web_sys::window().expect("no global `window` exists");
    let document = window.document().expect("should have a document on window");

    // run_effects() re-runs any create_effect closures whose signals changed,
    // including dynamic attribute effects registered during render.
    run_effects();

    let pending = take_pending_boundaries();

    let mut boundary_updates = Vec::new();

    for id in pending {
        let entry = BOUNDARIES.with(|b| b.borrow().get(&id).cloned());
        if let Some((old_node, compute)) = entry {
            let view = id.track(|| {
                let mut comp = compute.borrow_mut();
                comp()
            });
            let new_node = render_view(&document, view)?;
            if let Some(parent) = old_node.parent_node() {
                parent.replace_child(&new_node, &old_node)?;
                boundary_updates.push((id, new_node, compute));
            }
        }
    }

    BOUNDARIES.with(|b| {
        let mut boundaries = b.borrow_mut();
        for (id, new_node, compute) in boundary_updates {
            boundaries.insert(id, (new_node, compute));
        }
    });

    Ok(())
}

#[macro_export]
macro_rules! get_value {
    ($id:expr) => {{
        let mut val = String::new();
        if let Some(w) = $crate::web_sys::window() {
            if let Some(d) = w.document() {
                if let Some(el) = d.get_element_by_id($id) {
                    use $crate::wasm_bindgen::JsCast;
                    if let Ok(input_el) = el.clone().dyn_into::<$crate::web_sys::HtmlInputElement>()
                    {
                        val = input_el.value();
                    } else if let Ok(textarea_el) = el
                        .clone()
                        .dyn_into::<$crate::web_sys::HtmlTextAreaElement>()
                    {
                        val = textarea_el.value();
                    } else if let Ok(select_el) =
                        el.dyn_into::<$crate::web_sys::HtmlSelectElement>()
                    {
                        val = select_el.value();
                    }
                }
            }
        }
        val
    }};
}

#[macro_export]
macro_rules! spawn {
    ($fut:expr) => {
        $crate::wasm_bindgen_futures::spawn_local(async move {
            $fut.await;
            let _ = $crate::tick();
        });
    };
}

#[macro_export]
macro_rules! fetch {
    // With body
    ($method:ident $url:expr, $body:expr => |$text:ident| $success:block) => {
        $crate::wasm_bindgen_futures::spawn_local(async move {
            if let Ok(resp) = $crate::reqwasm::http::Request::$method($url).header("Content-Type", "application/json").body($body).send().await {
                if let Ok($text) = resp.text().await {
                    $success
                    let _ = $crate::tick();
                }
            }
        });
    };
    ($method:ident $url:expr, $body:expr => |$text:ident| $success:block, |$err:ident| $error:block) => {
        $crate::wasm_bindgen_futures::spawn_local(async move {
            match $crate::reqwasm::http::Request::$method($url).header("Content-Type", "application/json").body($body).send().await {
                Ok(resp) => {
                    match resp.text().await {
                        Ok($text) => {
                            $success
                            let _ = $crate::tick();
                        }
                        Err(e) => {
                            let $err = format!("Parse error: {:?}", e);
                            $error
                            let _ = $crate::tick();
                        }
                    }
                }
                Err(e) => {
                    let $err = format!("Fetch error: {:?}", e);
                    $error
                    let _ = $crate::tick();
                }
            }
        });
    };

    // Without body
    ($method:ident $url:expr => |$text:ident| $success:block) => {
        $crate::wasm_bindgen_futures::spawn_local(async move {
            if let Ok(resp) = $crate::reqwasm::http::Request::$method($url).send().await {
                if let Ok($text) = resp.text().await {
                    $success
                    let _ = $crate::tick();
                }
            }
        });
    };
    ($method:ident $url:expr => |$text:ident| $success:block, |$err:ident| $error:block) => {
        $crate::wasm_bindgen_futures::spawn_local(async move {
            match $crate::reqwasm::http::Request::$method($url).send().await {
                Ok(resp) => {
                    match resp.text().await {
                        Ok($text) => {
                            $success
                            let _ = $crate::tick();
                        }
                        Err(e) => {
                            let $err = format!("Parse error: {:?}", e);
                            $error
                            let _ = $crate::tick();
                        }
                    }
                }
                Err(e) => {
                    let $err = format!("Fetch error: {:?}", e);
                    $error
                    let _ = $crate::tick();
                }
            }
        });
    };

    // Default GET
    ($url:expr => |$text:ident| $success:block) => {
        $crate::fetch!(get $url => |$text| $success)
    };
    ($url:expr => |$text:ident| $success:block, |$err:ident| $error:block) => {
        $crate::fetch!(get $url => |$text| $success, |$err| $error)
    };
}

#[macro_export]
macro_rules! rpc {
    ($call:expr => |$ok:ident| $success:block) => {
        $crate::spawn!(async move {
            if let Ok($ok) = $call.await {
                $success
            }
        });
    };
    ($call:expr => |$ok:ident| $success:block, |$err:ident| $error:block) => {
        $crate::spawn!(async move {
            match $call.await {
                Ok($ok) => $success,
                Err($err) => $error,
            }
        });
    };
}

#[macro_export]
macro_rules! alert {
    ($msg:expr) => {
        if let Some(window) = $crate::web_sys::window() {
            let _ = window.alert_with_message($msg);
        }
    };
}

#[macro_export]
macro_rules! log {
    ($($t:tt)*) => {
        $crate::web_sys::console::log_1(&format!($($t)*).into());
    }
}

pub fn toggle_html_class(class: &str, active: bool) {
    if let Some(window) = web_sys::window() {
        if let Some(document) = window.document() {
            if let Some(html) = document.document_element() {
                if active {
                    let _ = html.set_attribute("class", class);
                } else {
                    let _ = html.remove_attribute("class");
                }
            }
        }
    }
}

// ponytail: keep it simple. use max-age for expiration, no complex date parsing.
#[macro_export]
macro_rules! get_cookie {
    () => {{
        let mut cookie_string = String::new();
        if let Some(window) = $crate::web_sys::window() {
            if let Some(document) = window.document() {
                use $crate::wasm_bindgen::JsCast;
                if let Ok(html_doc) = document.dyn_into::<$crate::web_sys::HtmlDocument>() {
                    if let Ok(c) = html_doc.cookie() {
                        cookie_string = c;
                    }
                }
            }
        }
        cookie_string
    }};
    ($name:expr) => {{
        let cookies = $crate::get_cookie!();
        let name = $name;
        let mut result = None;
        for c in cookies.split(';') {
            let c = c.trim();
            if c.starts_with(name) && c[name.len()..].starts_with('=') {
                result = Some(c[name.len() + 1..].to_string());
                break;
            }
        }
        result
    }};
}

#[macro_export]
macro_rules! set_cookie {
    ($name:expr, $value:expr) => {
        if let Some(window) = $crate::web_sys::window() {
            if let Some(document) = window.document() {
                use $crate::wasm_bindgen::JsCast;
                if let Ok(html_doc) = document.dyn_into::<$crate::web_sys::HtmlDocument>() {
                    let cookie_str = format!("{}={}; path=/", $name, $value);
                    let _ = html_doc.set_cookie(&cookie_str);
                }
            }
        }
    };
    ($name:expr, $value:expr, $max_age:expr) => {
        if let Some(window) = $crate::web_sys::window() {
            if let Some(document) = window.document() {
                use $crate::wasm_bindgen::JsCast;
                if let Ok(html_doc) = document.dyn_into::<$crate::web_sys::HtmlDocument>() {
                    let cookie_str = format!("{}={}; max-age={}; path=/", $name, $value, $max_age);
                    let _ = html_doc.set_cookie(&cookie_str);
                }
            }
        }
    };
}

#[macro_export]
macro_rules! navigate {
    ($path:expr) => {
        if let Some(window) = $crate::web_sys::window() {
            let _ = window.history().unwrap().push_state_with_url(
                &$crate::wasm_bindgen::JsValue::NULL,
                "",
                Some($path),
            );
            let path_str = $path;
            let route = path_str.split(['?', '#']).next().unwrap_or(path_str);
            $crate::ROUTER_SETTER.with(|s| {
                if let Some(setter) = *s.borrow() {
                    setter.set(route.to_string());
                }
            });
            let _ = $crate::tick();
            window.scroll_to_with_x_and_y(0.0, 0.0);
        }
    };
}

#[macro_export]
macro_rules! animate {
    ($selector:expr, $config:expr) => {
        if let Some(_) = $crate::web_sys::window() {
            let script = format!("if (window.gsap) {{ gsap.to('{}', {}) }}", $selector, $config);
            if let Err(e) = $crate::js_sys::eval(&script) {
                $crate::web_sys::console::error_2(&"GSAP animate! error:".into(), &e);
            }
        }
    };
    (from $selector:expr, $config:expr) => {
        if let Some(_) = $crate::web_sys::window() {
            let script = format!("if (window.gsap) {{ gsap.from('{}', {}) }}", $selector, $config);
            if let Err(e) = $crate::js_sys::eval(&script) {
                $crate::web_sys::console::error_2(&"GSAP animate! from error:".into(), &e);
            }
        }
    };
    (fromTo $selector:expr, $from:expr, $to:expr) => {
        if let Some(_) = $crate::web_sys::window() {
            let script = format!("if (window.gsap) {{ gsap.fromTo('{}', {}, {}) }}", $selector, $from, $to);
            if let Err(e) = $crate::js_sys::eval(&script) {
                $crate::web_sys::console::error_2(&"GSAP animate! fromTo error:".into(), &e);
            }
        }
    };
    (timeline $script:expr) => {
        if let Some(_) = $crate::web_sys::window() {
            let script = format!("if (window.gsap) {{ let tl = gsap.timeline(); {} }}", $script);
            if let Err(e) = $crate::js_sys::eval(&script) {
                $crate::web_sys::console::error_2(&"GSAP animate! timeline error:".into(), &e);
            }
        }
    };
}

#[macro_export]
macro_rules! redirect {
    ($url:expr) => {
        if let Some(w) = $crate::web_sys::window() {
            let _ = w.location().assign($url);
        }
    };
}

#[macro_export]
macro_rules! back {
    () => {
        if let Some(w) = $crate::web_sys::window() {
            if let Ok(h) = w.history() {
                let _ = h.back();
            }
        }
    };
}