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
//! Macro for ring-core
//!
//! for impl recursion, we need:
//! `poll` macro help to implement await operation finish when feature using `wasm`
//!
//! # Example
//!
//! ```rust,ignore
//! # extern crate async_trait;
//! # extern crate futures;
//! # extern crate ring_core;
//! # extern crate log;
//!
//! use std::sync::Arc;
//! use std::time::Duration;
//!
//! # use async_trait::async_trait;
//! # use futures::future::FutureExt;
//! # use futures::pin_mut;
//! # use futures::select;
//! # use futures_timer::Delay;
//! # use ring_core::dht::Stabilization;
//! # use ring_core::dht::TStabilize;
//!
//! #[async_trait]
//! impl TStabilize for Stabilization {
//!     async fn wait(self: Arc<Self>) {
//!         loop {
//!             let timeout = Delay::new(Duration::from_secs(self.timeout as u64)).fuse();
//!             pin_mut!(timeout);
//!             select! {
//!                 _ = timeout => self
//!                     .stabilize()
//!                     .await
//!                     .unwrap_or_else(|e| log::error!("failed to stabilize {:?}", e)),
//!             }
//!         }
//!     }
//! }
//! ```
//!
//! Stabilize function using `futures::select` to await task is finish, but feature wasm not support
//! Using `poll` can fix this problem.
//!
//! ```rust,ignore
//! # extern crate async_trait;
//! # extern crate ring_core;
//! # extern crate wasm_bindgen_futures;
//! # extern crate log;
//!
//! # use std::sync::Arc;
//!
//! # use async_trait::async_trait;
//! # use ring_core::dht::Stabilization;
//! # use ring_core::dht::TStabilize;
//! # use ring_core::macros::poll;
//! # use wasm_bindgen_futures::spawn_local;
//!  #[async_trait(?Send)]
//!  impl TStabilize for Stabilization {
//!      async fn wait(self: Arc<Self>) {
//!          let caller = Arc::clone(&self);
//!          let func = move || {
//!             let caller = caller.clone();
//!             spawn_local(Box::pin(async move {
//!                 caller
//!                     .stabilize()
//!                     .await
//!                     .unwrap_or_else(|e| log::error!("failed to stabilize {:?}", e));
//!             }))
//!         };
//!         poll!(func, 25000);
//!     }
//!  }
//!  ```

/// poll macro use for wasm futures and wait, act like `async-await`.
#[macro_export]
macro_rules! poll {
    ( $func:expr, $ttl:expr ) => {{
        use wasm_bindgen::JsCast;
        use $crate::utils::js_utils::global;
        use $crate::utils::js_utils::Global;
        let func = wasm_bindgen::prelude::Closure::wrap(
            (Box::new(move |func: js_sys::Function| {
                $func();
                match global().unwrap() {
                    Global::Window(window) => {
                        window
                            .set_timeout_with_callback_and_timeout_and_arguments(
                                func.unchecked_ref(),
                                $ttl,
                                &js_sys::Array::of1(&func),
                            )
                            .unwrap();
                    }
                    Global::WorkerGlobal(window) => {
                        window
                            .set_timeout_with_callback_and_timeout_and_arguments(
                                func.unchecked_ref(),
                                $ttl,
                                &js_sys::Array::of1(&func),
                            )
                            .unwrap();
                    }
                    Global::ServiceWorkerGlobal(window) => {
                        window
                            .set_timeout_with_callback_and_timeout_and_arguments(
                                func.unchecked_ref(),
                                $ttl,
                                &js_sys::Array::of1(&func),
                            )
                            .unwrap();
                    }
                }
            })) as Box<dyn FnMut(js_sys::Function)>,
        );
        match global().unwrap() {
            Global::Window(window) => {
                window
                    .set_timeout_with_callback_and_timeout_and_arguments(
                        &func.as_ref().unchecked_ref(),
                        $ttl,
                        &js_sys::Array::of1(&func.as_ref().unchecked_ref()),
                    )
                    .unwrap();
                func.forget();
            }
            Global::WorkerGlobal(window) => {
                window
                    .set_timeout_with_callback_and_timeout_and_arguments(
                        &func.as_ref().unchecked_ref(),
                        $ttl,
                        &js_sys::Array::of1(&func.as_ref().unchecked_ref()),
                    )
                    .unwrap();
                func.forget();
            }
            Global::ServiceWorkerGlobal(window) => {
                window
                    .set_timeout_with_callback_and_timeout_and_arguments(
                        &func.as_ref().unchecked_ref(),
                        $ttl,
                        &js_sys::Array::of1(&func.as_ref().unchecked_ref()),
                    )
                    .unwrap();
                func.forget();
            }
        }
    }};
}