|
1 | 1 | use std::time::Duration;
|
2 | 2 |
|
3 |
| -#[cfg(not(target_arch = "wasm32"))] |
4 |
| -pub(crate) async fn async_sleep(interval: Duration) { |
5 |
| - let (sender, receiver) = futures_channel::oneshot::channel::<()>(); |
6 |
| - std::thread::spawn(move || { |
7 |
| - std::thread::sleep(interval); |
8 |
| - let _ = sender.send(()); |
9 |
| - }); |
10 |
| - let _ = receiver.await; |
| 3 | +#[derive(Debug, Copy, Clone)] |
| 4 | +pub(crate) enum SleepBackend { |
| 5 | + #[cfg(all(not(target_arch = "wasm32"), feature = "reqwest"))] |
| 6 | + Tokio, |
| 7 | + #[cfg(not(target_arch = "wasm32"))] |
| 8 | + Thread, |
| 9 | + #[cfg(target_arch = "wasm32")] |
| 10 | + Javascript, |
11 | 11 | }
|
12 | 12 |
|
13 |
| -#[cfg(target_arch = "wasm32")] |
14 |
| -pub(crate) async fn async_sleep(interval: Duration) { |
15 |
| - use std::convert::TryInto; |
16 |
| - use wasm_bindgen_futures::JsFuture; |
17 |
| - |
18 |
| - JsFuture::from(web_sys::js_sys::Promise::new(&mut |yes, _| { |
19 |
| - web_sys::window() |
20 |
| - .unwrap() |
21 |
| - .set_timeout_with_callback_and_timeout_and_arguments_0( |
22 |
| - &yes, |
23 |
| - interval.as_millis().try_into().unwrap(), |
24 |
| - ) |
25 |
| - .unwrap(); |
26 |
| - })) |
27 |
| - .await |
28 |
| - .unwrap(); |
| 13 | +impl SleepBackend { |
| 14 | + pub(crate) fn infer(is_tokio: bool) -> Self { |
| 15 | + #[cfg(all(not(target_arch = "wasm32"), feature = "reqwest"))] |
| 16 | + if is_tokio { |
| 17 | + return Self::Tokio; |
| 18 | + } |
| 19 | + #[cfg(any(target_arch = "wasm32", not(feature = "reqwest")))] |
| 20 | + let _ = is_tokio; |
| 21 | + |
| 22 | + #[cfg(not(target_arch = "wasm32"))] |
| 23 | + return Self::Thread; |
| 24 | + |
| 25 | + #[cfg(target_arch = "wasm32")] |
| 26 | + return Self::Javascript; |
| 27 | + } |
| 28 | + |
| 29 | + pub(crate) async fn sleep(self, interval: Duration) { |
| 30 | + match self { |
| 31 | + #[cfg(all(not(target_arch = "wasm32"), feature = "reqwest"))] |
| 32 | + Self::Tokio => { |
| 33 | + tokio::time::sleep(interval).await; |
| 34 | + } |
| 35 | + #[cfg(not(target_arch = "wasm32"))] |
| 36 | + Self::Thread => { |
| 37 | + let (sender, receiver) = futures_channel::oneshot::channel::<()>(); |
| 38 | + std::thread::spawn(move || { |
| 39 | + std::thread::sleep(interval); |
| 40 | + let _ = sender.send(()); |
| 41 | + }); |
| 42 | + let _ = receiver.await; |
| 43 | + } |
| 44 | + #[cfg(target_arch = "wasm32")] |
| 45 | + Self::Javascript => { |
| 46 | + use std::convert::TryInto; |
| 47 | + use wasm_bindgen_futures::JsFuture; |
| 48 | + |
| 49 | + JsFuture::from(web_sys::js_sys::Promise::new(&mut |yes, _| { |
| 50 | + web_sys::window() |
| 51 | + .unwrap() |
| 52 | + .set_timeout_with_callback_and_timeout_and_arguments_0( |
| 53 | + &yes, |
| 54 | + interval.as_millis().try_into().unwrap(), |
| 55 | + ) |
| 56 | + .unwrap(); |
| 57 | + })) |
| 58 | + .await |
| 59 | + .unwrap(); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
29 | 63 | }
|
30 | 64 |
|
31 | 65 | #[cfg(test)]
|
32 | 66 | mod test {
|
33 | 67 | use super::*;
|
34 | 68 | use meilisearch_test_macro::meilisearch_test;
|
35 | 69 |
|
| 70 | + #[cfg(all(not(target_arch = "wasm32"), feature = "reqwest"))] |
| 71 | + #[meilisearch_test] |
| 72 | + async fn sleep_tokio() { |
| 73 | + let sleep_duration = Duration::from_millis(10); |
| 74 | + let now = std::time::Instant::now(); |
| 75 | + |
| 76 | + SleepBackend::Tokio.sleep(sleep_duration).await; |
| 77 | + |
| 78 | + assert!(now.elapsed() >= sleep_duration); |
| 79 | + } |
| 80 | + |
| 81 | + #[cfg(not(target_arch = "wasm32"))] |
| 82 | + #[meilisearch_test] |
| 83 | + async fn sleep_thread() { |
| 84 | + let sleep_duration = Duration::from_millis(10); |
| 85 | + let now = std::time::Instant::now(); |
| 86 | + |
| 87 | + SleepBackend::Thread.sleep(sleep_duration).await; |
| 88 | + |
| 89 | + assert!(now.elapsed() >= sleep_duration); |
| 90 | + } |
| 91 | + |
| 92 | + #[cfg(target_arch = "wasm32")] |
36 | 93 | #[meilisearch_test]
|
37 |
| - async fn test_async_sleep() { |
| 94 | + async fn sleep_javascript() { |
38 | 95 | let sleep_duration = Duration::from_millis(10);
|
39 | 96 | let now = std::time::Instant::now();
|
40 | 97 |
|
41 |
| - async_sleep(sleep_duration).await; |
| 98 | + SleepBackend::Javascript.sleep(sleep_duration).await; |
42 | 99 |
|
43 | 100 | assert!(now.elapsed() >= sleep_duration);
|
44 | 101 | }
|
|
0 commit comments