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