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
pub mod element;

use std::{fmt::Display, ops::Deref};

use wasm_bindgen::{prelude::*, JsCast, JsValue};

pub use element::{elem, Element, WebElement, WebElementBuilder};
pub use we_derive::{we_builder, WebElement};
use web_sys::{KeyboardEvent, MessageEvent};

#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
    JsError(JsValue),
    Cast(&'static str),
    Window,
    Document,
    Body,
    Value,
}

impl From<JsValue> for Error {
    fn from(from: JsValue) -> Self {
        Error::JsError(from)
    }
}

impl From<Error> for JsValue {
    fn from(e: Error) -> Self {
        e.as_jsvalue()
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::JsError(s) => {
                if let Some(s) = s.as_string() {
                    write!(f, "{}", s)
                } else {
                    Err(std::fmt::Error)
                }
            }
            Error::Cast(t) => writeln!(f, "unable to cast value to type `{}`", t),
            n => writeln!(f, "{:?}", n),
        }
    }
}

impl Error {
    pub fn as_jsvalue(&self) -> JsValue {
        if let Self::JsError(jsvalue) = self {
            jsvalue.clone()
        } else {
            JsValue::from_str(&self.to_string())
        }
    }

    pub fn js_str(value: impl AsRef<str>) -> Error {
        Error::JsError(JsValue::from_str(value.as_ref()))
    }
}

impl std::error::Error for Error {}

pub type Result<T> = std::result::Result<T, Error>;

pub struct Window {
    window: web_sys::Window,
}

impl Deref for Window {
    type Target = web_sys::Window;

    fn deref(&self) -> &Self::Target {
        &self.window
    }
}

impl Window {
    pub fn on_animation(&self, callback: impl FnMut() + 'static) -> Result<()> {
        let closure = Closure::wrap(Box::new(callback) as Box<dyn FnMut()>);
        self.request_animation_frame(closure.as_ref().unchecked_ref())
            .map_err(Error::JsError)?;
        closure.forget();
        Ok(())
    }
}

pub fn window() -> Result<Window> {
    Ok(Window {
        window: web_sys::window().ok_or(Error::Window)?,
    })
}

pub struct Document {
    document: web_sys::Document,
}

impl Document {
    pub fn on_key(&self, mut callback: impl FnMut(KeyboardEvent) + 'static) -> Result<()> {
        let closure =
            Closure::wrap(Box::new(move |e| callback(e)) as Box<dyn FnMut(KeyboardEvent)>);
        self.document
            .add_event_listener_with_callback("keydown", closure.as_ref().unchecked_ref())
            .map_err(Error::JsError)?;
        closure.forget();
        Ok(())
    }

    pub fn body(&self) -> Result<Element<crate::elem::Base>> {
        let element = self.document.body().ok_or(Error::Body)?;
        Ok(Element::from_element(element))
    }
}

impl Deref for Document {
    type Target = web_sys::Document;

    fn deref(&self) -> &Self::Target {
        &self.document
    }
}

pub fn document() -> Result<Document> {
    Ok(Document {
        document: window()?.document().ok_or(Error::Document)?,
    })
}

pub trait Loggable {
    fn log(self);
}

impl<T> Loggable for Result<T> {
    fn log(self) {
        if let Err(err) = self {
            log(format!("{}", err))
        }
    }
}

#[allow(unused_unsafe)]
pub fn log<S: AsRef<str>>(str: S) {
    unsafe {
        web_sys::console::log_1(&JsValue::from_str(str.as_ref()));
    }
}

#[derive(Debug, Clone)]
pub struct Worker {
    worker: web_sys::Worker,
}

impl Worker {
    pub fn new(ctor: impl AsRef<JsValue>) -> Result<Self> {
        let ctor = ctor
            .as_ref()
            .dyn_ref::<js_sys::Function>()
            .ok_or(Error::Value)?;
        let worker = ctor
            .call0(&JsValue::null())?
            .dyn_into::<web_sys::Worker>()?;
        Ok(Self { worker })
    }

    pub fn set_onmessage(&self, mut callback: impl FnMut(JsValue) + 'static) -> Result<()> {
        let closure = Closure::wrap(Box::new(move |event| {
            let event: MessageEvent = event;
            callback(event.data())
        }) as Box<dyn FnMut(web_sys::MessageEvent)>);
        self.worker
            .set_onmessage(Some(closure.into_js_value().unchecked_ref()));
        Ok(())
    }

    pub fn post_message(&self, value: impl AsRef<JsValue>) -> Result<()> {
        self.worker.post_message(value.as_ref())?;
        Ok(())
    }
}

#[derive(Debug, Clone)]
pub struct Scope {
    scope: web_sys::DedicatedWorkerGlobalScope,
}

impl Scope {
    pub fn new(scope: impl AsRef<JsValue>) -> Result<Self> {
        Ok(Self {
            scope: scope.as_ref().clone().dyn_into()?,
        })
    }
    pub fn set_onmessage(&self, mut callback: impl FnMut(JsValue) + 'static) -> Result<()> {
        let closure = Closure::wrap(Box::new(move |event| {
            let event: MessageEvent = event;
            callback(event.data());
        }) as Box<dyn FnMut(MessageEvent)>);
        self.scope.set_onmessage(Some(closure.into_js_value().unchecked_ref()));
        Ok(())
    }

    pub fn post_message(&self, message: JsValue) -> Result<()> {
        self.scope.post_message(&message)?;
        Ok(())
    }
}