|
| 1 | +use futures::task::{self, ArcWake}; |
| 2 | +use parking_lot::Mutex; |
| 3 | +use std::convert::TryFrom; |
| 4 | +use std::future::Future; |
| 5 | +use std::pin::Pin; |
| 6 | +use std::sync::Arc; |
| 7 | +use std::task::Context; |
| 8 | +use std::time::Duration; |
| 9 | +use wasm_bindgen::{JsCast, closure::Closure}; |
| 10 | + |
| 11 | +use crate::{Instant, Timer, TimerHandle}; |
| 12 | + |
| 13 | +/// Starts a background task, creates a `Timer`, and returns a handle to it. |
| 14 | +/// |
| 15 | +/// > **Note**: Contrary to the original `futures-timer` crate, we don't have |
| 16 | +/// > any `forget()` method, as the task is automatically considered |
| 17 | +/// > as "forgotten". |
| 18 | +pub(crate) fn run() -> TimerHandle { |
| 19 | + let timer = Timer::new(); |
| 20 | + let handle = timer.handle(); |
| 21 | + schedule_callback(Arc::new(Mutex::new(timer)), Duration::new(0, 0)); |
| 22 | + handle |
| 23 | +} |
| 24 | + |
| 25 | +/// Calls `Window::setTimeout` with the given `Duration`. The callback wakes up the timer and |
| 26 | +/// processes everything. |
| 27 | +fn schedule_callback(timer: Arc<Mutex<Timer>>, when: Duration) { |
| 28 | + let window = web_sys::window().expect("Unable to access Window"); |
| 29 | + let _ = window.set_timeout_with_callback_and_timeout_and_arguments_0( |
| 30 | + &Closure::once_into_js(move || { |
| 31 | + let mut timer_lock = timer.lock(); |
| 32 | + |
| 33 | + // We start by polling the timer. If any new `Delay` is created, the waker will be used |
| 34 | + // to wake up this task pre-emptively. As such, we pass a `Waker` that calls |
| 35 | + // `schedule_callback` with a delay of `0`. |
| 36 | + let waker = task::waker(Arc::new(Waker { timer: timer.clone() })); |
| 37 | + let _ = Future::poll(Pin::new(&mut *timer_lock), &mut Context::from_waker(&waker)); |
| 38 | + |
| 39 | + // Notify the timers that are ready. |
| 40 | + let now = Instant::now(); |
| 41 | + timer_lock.advance_to(now); |
| 42 | + |
| 43 | + // Each call to `schedule_callback` calls `schedule_callback` again, but also leaves |
| 44 | + // the possibility for `schedule_callback` to be called in parallel. Since we don't |
| 45 | + // want too many useless callbacks, we... |
| 46 | + // TODO: ugh, that's a hack |
| 47 | + if Arc::strong_count(&timer) > 20 { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // We call `schedule_callback` again for the next event. |
| 52 | + let sleep_dur = timer_lock.next_event() |
| 53 | + .map(|next_event| { |
| 54 | + if next_event > now { |
| 55 | + next_event - now |
| 56 | + } else { |
| 57 | + Duration::new(0, 0) |
| 58 | + } |
| 59 | + }) |
| 60 | + .unwrap_or(Duration::from_secs(5)); |
| 61 | + drop(timer_lock); |
| 62 | + schedule_callback(timer, sleep_dur); |
| 63 | + |
| 64 | + }).unchecked_ref(), |
| 65 | + i32::try_from(when.as_millis()).unwrap_or(0) |
| 66 | + ).unwrap(); |
| 67 | +} |
| 68 | + |
| 69 | +struct Waker { |
| 70 | + timer: Arc<Mutex<Timer>>, |
| 71 | +} |
| 72 | + |
| 73 | +impl ArcWake for Waker { |
| 74 | + fn wake_by_ref(arc_self: &Arc<Self>) { |
| 75 | + schedule_callback(arc_self.timer.clone(), Duration::new(0, 0)); |
| 76 | + } |
| 77 | +} |
0 commit comments