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
#![feature(proc_macro_hygiene)]
#![feature(exclusive_range_pattern)]
#![warn(anonymous_parameters)]
#![warn(bare_trait_objects)]
#![warn(elided_lifetimes_in_paths)]
#![warn(missing_debug_implementations)]
#![warn(single_use_lifetimes)]
#![warn(trivial_casts)]
#![warn(unreachable_pub)]
#![warn(unsafe_code)]
#![warn(unused_extern_crates)]
#![warn(unused_import_braces)]
#![warn(unused_qualifications)]
#![warn(unused_results)]
#![warn(variant_size_differences)]

use wasm_bindgen::prelude::wasm_bindgen;

#[macro_use]
pub(crate) mod logging;

mod ctx;
mod event_handler;
mod html;
mod message_handler;
mod tabs;
pub(crate) mod templates {
  pub(crate) mod card;
  pub(crate) mod query;
  pub(crate) mod results;
  pub(crate) mod schema;
  pub(crate) mod table;
}
mod ws;
mod ws_events;

// Imports
#[wasm_bindgen]
extern "C" {
  #[allow(unsafe_code)]
  #[wasm_bindgen(js_namespace = console)]
  fn log(s: &str, style: &str);

  #[allow(unsafe_code)]
  #[wasm_bindgen(js_namespace = UIkit)]
  fn tab(el: web_sys::HtmlElement);
}

#[wasm_bindgen]
#[derive(Debug)]
pub struct ClientTest {
  ctx: std::rc::Rc<std::sync::RwLock<crate::ctx::ClientContext>>
}

impl Default for ClientTest {
  fn default() -> Self {
    Self::new()
  }
}

#[wasm_bindgen]
impl ClientTest {
  #[wasm_bindgen(constructor)]
  pub fn new() -> Self {
    std::panic::set_hook(Box::new(console_error_panic_hook::hook));

    let ctx = match crate::ctx::ClientContext::new() {
      Ok(c) => c,
      Err(e) => panic!("Cannot construct ClientContext: {:?}", e)
    };

    tab(ctx.read().unwrap().get_element_by_id("tab-container").unwrap());

    ClientTest { ctx }
  }

  pub fn on_event(&self, t: &str, v: &str) {
    let mut c = self.ctx.write().unwrap();
    c.on_event(t, v);
  }
}