Skip to content

Commit f29a3b2

Browse files
Convert async_sleep into SleepBackend enum
1 parent 5dd4375 commit f29a3b2

File tree

2 files changed

+65
-28
lines changed

2 files changed

+65
-28
lines changed

src/client.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
search::*,
1313
task_info::TaskInfo,
1414
tasks::{Task, TasksCancelQuery, TasksDeleteQuery, TasksResults, TasksSearchQuery},
15-
utils::async_sleep,
15+
utils::SleepBackend,
1616
DefaultHttpClient,
1717
};
1818

@@ -933,7 +933,7 @@ impl<Http: HttpClient> Client<Http> {
933933
}
934934
Task::Enqueued { .. } | Task::Processing { .. } => {
935935
elapsed_time += interval;
936-
async_sleep(interval).await;
936+
self.sleep_backend().sleep(interval).await;
937937
}
938938
},
939939
Err(error) => return Err(error),
@@ -1144,6 +1144,10 @@ impl<Http: HttpClient> Client<Http> {
11441144

11451145
crate::tenant_tokens::generate_tenant_token(api_key_uid, search_rules, api_key, expires_at)
11461146
}
1147+
1148+
fn sleep_backend(&self) -> SleepBackend {
1149+
SleepBackend::infer()
1150+
}
11471151
}
11481152

11491153
#[derive(Debug, Clone, Deserialize)]

src/utils.rs

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,77 @@
11
use std::time::Duration;
22

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(not(target_arch = "wasm32"))]
6+
Thread,
7+
#[cfg(target_arch = "wasm32")]
8+
Javascript,
119
}
1210

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();
11+
impl SleepBackend {
12+
pub(crate) fn infer() -> Self {
13+
#[cfg(not(target_arch = "wasm32"))]
14+
return Self::Thread;
15+
16+
#[cfg(target_arch = "wasm32")]
17+
return Self::Javascript;
18+
}
19+
20+
pub(crate) async fn sleep(self, interval: Duration) {
21+
match self {
22+
#[cfg(not(target_arch = "wasm32"))]
23+
Self::Thread => {
24+
let (sender, receiver) = futures_channel::oneshot::channel::<()>();
25+
std::thread::spawn(move || {
26+
std::thread::sleep(interval);
27+
let _ = sender.send(());
28+
});
29+
let _ = receiver.await;
30+
}
31+
#[cfg(target_arch = "wasm32")]
32+
Self::Javascript => {
33+
use std::convert::TryInto;
34+
use wasm_bindgen_futures::JsFuture;
35+
36+
JsFuture::from(web_sys::js_sys::Promise::new(&mut |yes, _| {
37+
web_sys::window()
38+
.unwrap()
39+
.set_timeout_with_callback_and_timeout_and_arguments_0(
40+
&yes,
41+
interval.as_millis().try_into().unwrap(),
42+
)
43+
.unwrap();
44+
}))
45+
.await
46+
.unwrap();
47+
}
48+
}
49+
}
2950
}
3051

3152
#[cfg(test)]
3253
mod test {
3354
use super::*;
3455
use meilisearch_test_macro::meilisearch_test;
3556

57+
#[cfg(not(target_arch = "wasm32"))]
58+
#[meilisearch_test]
59+
async fn sleep_thread() {
60+
let sleep_duration = Duration::from_millis(10);
61+
let now = std::time::Instant::now();
62+
63+
SleepBackend::Thread.sleep(sleep_duration).await;
64+
65+
assert!(now.elapsed() >= sleep_duration);
66+
}
67+
68+
#[cfg(target_arch = "wasm32")]
3669
#[meilisearch_test]
37-
async fn test_async_sleep() {
70+
async fn sleep_javascript() {
3871
let sleep_duration = Duration::from_millis(10);
3972
let now = std::time::Instant::now();
4073

41-
async_sleep(sleep_duration).await;
74+
SleepBackend::Javascript.sleep(sleep_duration).await;
4275

4376
assert!(now.elapsed() >= sleep_duration);
4477
}

0 commit comments

Comments
 (0)