33use crate :: { errors:: Error , indexes:: Index , request:: * } ;
44use serde:: Deserialize ;
55use std:: collections:: { BTreeMap , BTreeSet } ;
6- use std:: thread:: sleep;
76use 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 ) ]
150169pub 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