Skip to content

Commit cd248f4

Browse files
sanders41ppamorim
authored andcommitted
Making sleep non-blocking
1 parent 0eac1cc commit cd248f4

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ log = "0.4"
1414
serde = { version = "1.0", features = ["derive"] }
1515

1616
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
17+
futures = "0.3"
1718
isahc = { version = "1.0", features = ["http2", "text-decoding"], default_features = false }
1819

1920
[target.'cfg(target_arch = "wasm32")'.dependencies]
21+
js-sys = "0.3.47"
2022
web-sys = { version = "0.3", features = ["RequestInit", "Headers", "Window", "Response", "console"] }
2123
wasm-bindgen = "0.2"
2224
wasm-bindgen-futures = "0.4"

src/progress.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use crate::{errors::Error, indexes::Index, request::*};
44
use serde::Deserialize;
55
use std::collections::{BTreeMap, BTreeSet};
6-
use std::thread::sleep;
76
use std::time::Duration;
87

98
#[derive(Deserialize)]
@@ -64,7 +63,7 @@ impl<'a> Progress<'a> {
6463
/// ```
6564
/// # use meilisearch_sdk::{client::*, document, indexes::*, progress::*};
6665
/// # use serde::{Serialize, Deserialize};
67-
///
66+
/// #
6867
/// # #[derive(Debug, Serialize, Deserialize, PartialEq)]
6968
/// # struct Document {
7069
/// # id: usize,
@@ -79,7 +78,7 @@ impl<'a> Progress<'a> {
7978
/// # &self.id
8079
/// # }
8180
/// # }
82-
///
81+
/// #
8382
/// # futures::executor::block_on(async move {
8483
/// let client = Client::new("http://localhost:7700", "masterKey");
8584
/// let movies = client.create_index("movies_wait_for_pending", None).await.unwrap();
@@ -125,7 +124,7 @@ impl<'a> Progress<'a> {
125124
},
126125
UpdateStatus::Enqueued { .. } => {
127126
elapsed_time += interval;
128-
sleep(interval);
127+
async_sleep(interval).await;
129128
},
130129
};
131130
}
@@ -141,10 +140,30 @@ impl<'a> Progress<'a> {
141140
}
142141
}
143142

144-
/*#[cfg(not(target_arch = "wasm32"))]
145-
pub(crate) async fn async_sleep() {
143+
#[cfg(not(target_arch = "wasm32"))]
144+
pub(crate) async fn async_sleep(interval: Duration) {
145+
let (sender, receiver) = futures::channel::oneshot::channel::<()>();
146+
std::thread::spawn(move || {
147+
std::thread::sleep(interval);
148+
let _ = sender.send(());
149+
});
150+
let _ = receiver.await;
151+
}
152+
153+
#[cfg(target_arch = "wasm32")]
154+
pub(crate) async fn async_sleep(interval: Duration) {
155+
use wasm_bindgen_futures::JsFuture;
146156

147-
}*/
157+
JsFuture::from(js_sys::Promise::new(&mut |yes, _| {
158+
web_sys::window()
159+
.unwrap()
160+
.set_timeout_with_callback_and_timeout_and_arguments_0(
161+
&yes,
162+
interval.as_millis() as i32,
163+
)
164+
.unwrap();
165+
})).await.unwrap();
166+
}
148167

149168
#[derive(Debug, Clone, Deserialize)]
150169
pub enum RankingRule {
@@ -233,6 +252,7 @@ mod test {
233252
use crate::{client::*, document, progress::*};
234253
use serde::{Serialize, Deserialize};
235254
use futures_await_test::async_test;
255+
use std::time;
236256

237257
#[derive(Debug, Serialize, Deserialize, PartialEq)]
238258
struct Document {
@@ -272,4 +292,14 @@ mod test {
272292
client.delete_index("movies_wait_for_pending_args").await.unwrap();
273293
assert!(matches!(status, UpdateStatus::Processed { .. }));
274294
}
295+
296+
#[async_test]
297+
async fn test_async_sleep() {
298+
let sleep_duration = time::Duration::from_millis(10);
299+
let now = time::Instant::now();
300+
301+
async_sleep(sleep_duration).await;
302+
303+
assert!(now.elapsed() >= sleep_duration);
304+
}
275305
}

0 commit comments

Comments
 (0)