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
#![allow(dead_code)]

pub(crate) mod events;
pub mod adapters {
    #[cfg(feature = "warp")]
    pub mod warp_adapter;

    #[cfg(feature = "axum")]
    pub mod axum_adapter;

    #[cfg(feature = "actix")]
    pub mod actix_adapter;
}

use std::net::SocketAddr;

#[cfg(feature = "warp")]
pub use adapters::warp_adapter::connect;

#[cfg(feature = "axum")]
pub use adapters::axum_adapter::connect;

#[cfg(feature = "actix")]
pub use adapters::actix_adapter::connect;
use tokio_util::task::LocalPoolHandle;

#[derive(Clone)]
pub struct Liveview {
    pool: LocalPoolHandle,
    addr: String,
}

impl Liveview {
    pub fn body(&self, header: &str) -> String {
        format!(
            r#"
<!DOCTYPE html>
<html>
  <head>
    {header}
  </head>
  <body>
    <div id="main"></div>
    <script>
      var WS_ADDR = "ws://{addr}/app";
      {interpreter}
      main();
    </script>
  </body>
</html>"#,
            addr = self.addr,
            interpreter = include_str!("../src/interpreter.js")
        )
    }
}

pub fn new(addr: impl Into<SocketAddr>) -> Liveview {
    let addr: SocketAddr = addr.into();

    Liveview {
        pool: LocalPoolHandle::new(16),
        addr: addr.to_string(),
    }
}