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
#![forbid(unsafe_code, missing_debug_implementations)]
#![feature(futures_api)]

use futures::prelude::*;
use futures::sync::oneshot;
use wasm_bindgen::prelude::Closure;
use wasm_bindgen::JsCast;

struct DocumentReady {
  receiver: oneshot::Receiver<()>,
  _cb: Closure<FnMut()>,
}

/// A function to call once the DOM has loaded. If the DOM is already loaded,
/// it will return on next tick.
impl Future for DocumentReady {
  type Item = ();
  type Error = ();

  fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
    match self.receiver.poll() {
      Ok(Async::Ready(_)) => Ok(Async::Ready(())),
      Ok(Async::NotReady) => Ok(Async::NotReady),
      Err(_) => {
        if cfg!(debug_assertions) {
          unreachable!("This future keeps the closure alive. The closure keeps the sender alive. Therefor it can't be cancelled.")
        }
        Err(())
      }
    }
  }
}

/// Wait for the DOM to be loaded.
pub fn ready() -> impl Future<Item = (), Error = ()> {
  let doc = window()
    .document()
    .expect("should have a document on window");

  let (sender, receiver) = oneshot::channel();

  let mut sender = Some(sender);
  let cb = move || {
    sender.take().unwrap().send(()).unwrap();
  };

  let cb = Closure::wrap(Box::new(cb) as Box<FnMut()>);
  match doc.ready_state().as_str() {
    "complete" | "interactive" => {
      window()
        .set_timeout_with_callback(cb.as_ref().unchecked_ref())
        .unwrap();
    }
    _ => {
      doc
        .add_event_listener_with_callback(
          "DOMContentLoaded",
          cb.as_ref().unchecked_ref(),
        )
        .unwrap();
    }
  };

  DocumentReady { _cb: cb, receiver }
}

/// Access the window.
// todo: expect_throw
fn window() -> web_sys::Window {
  web_sys::window().expect("no global `window` exists")
}