comet/core/
utils.rs

1use crate::prelude::*;
2
3pub fn document() -> web_sys::Document {
4    web_sys::window()
5        .expect("no global `window` exists")
6        .document()
7        .expect("should have a document on window")
8}
9
10#[derive(Clone)]
11pub enum HtmlNode {
12    Text(web_sys::Text),
13    Element(web_sys::Element),
14}
15
16impl HtmlNode {
17    pub fn into_element(self) -> web_sys::Element {
18        match self {
19            HtmlNode::Text(text) => panic!("Expected element, got text: {:?}", text),
20            HtmlNode::Element(elem) => elem,
21        }
22    }
23
24    pub fn append_to(self, parent: &web_sys::Element) {
25        match self {
26            HtmlNode::Text(text) => parent.append_child(&text).unwrap(),
27            HtmlNode::Element(elem) => parent.append_child(&elem).unwrap(),
28        };
29    }
30}
31
32#[derive(Serialize, Deserialize, Debug)]
33pub enum Event<T> {
34    Insert(T),
35    Update(T),
36    Delete(i32),
37}
38
39impl<T> Event<T> {
40    pub fn is_insert(&self) -> bool {
41        match self {
42            Event::Insert(_) => true,
43            _ => false,
44        }
45    }
46    pub fn is_update(&self) -> bool {
47        match self {
48            Event::Update(_) => true,
49            _ => false,
50        }
51    }
52    pub fn is_delete(&self) -> bool {
53        match self {
54            Event::Delete(_) => true,
55            _ => false,
56        }
57    }
58}
59
60#[cfg(not(target_arch = "wasm32"))]
61use reactive_pg::Event as PgEvent;
62
63#[cfg(not(target_arch = "wasm32"))]
64impl<T, M> From<PgEvent<T>> for Event<M>
65where
66    T: Into<M>,
67{
68    fn from(event: PgEvent<T>) -> Self {
69        match event {
70            PgEvent::Insert(t) => Event::Insert(t.into()),
71            PgEvent::Update(t) => Event::Update(t.into()),
72            PgEvent::Delete(id) => Event::Delete(id),
73        }
74    }
75}