From 3517fa18fd0b4328847114df321c3c9e6e71c826 Mon Sep 17 00:00:00 2001 From: One <43485962+c-git@users.noreply.github.com> Date: Tue, 16 Jul 2024 15:34:53 -0400 Subject: [PATCH] fix: rip out wait_for because it still blocks --- src/lib.rs | 2 -- src/wrappers.rs | 50 ------------------------------------------------- 2 files changed, 52 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f82c979..4d30d77 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,6 +76,4 @@ pub use wrappers::fetch; #[cfg(feature = "yield_now")] pub use yield_::yield_now; -pub use wrappers::wait_for; - pub use reqwest::Client; // Exported to make it easier to use without a second import and maintain semver diff --git a/src/wrappers.rs b/src/wrappers.rs index 0192cc0..c128f05 100644 --- a/src/wrappers.rs +++ b/src/wrappers.rs @@ -1,8 +1,6 @@ //! Stores the wrapper functions that can be called from either native or wasm //! code -use futures::Future; - /// Performs a HTTP requests and calls the given callback when done. NB: Needs /// to use a callback to prevent blocking on the thread that initiates the /// fetch. Note: Instead of calling get like in the example you can use post, @@ -63,52 +61,4 @@ where crate::wasm::spawn(future); } -/// Sometimes needed in callbacks to call async code from the sync context -/// Provides a cross platform compatible way to run an async function in a -/// blocking fashion, but without actually blocking because then the function -/// cannot complete and results in a deadlock. -/// -/// # Warning -/// Until a better solution can be found this will result in a busy loop so use -/// sparingly. -#[cfg(not(target_arch = "wasm32"))] -pub fn wait_for(future: F) -> F::Output -where - F: Future + 'static + Send, - F::Output: Send, -{ - let (tx, rx) = futures::channel::oneshot::channel(); - spawn(async move { - tx.send(future.await) - .unwrap_or_else(|_| eprintln!("failed to send but expected rx to still be available")); - }); - wait_for_receiver(rx) -} - -#[cfg(target_arch = "wasm32")] -/// dox -pub fn wait_for(future: F) -> F::Output -where - F: Future + 'static, -{ - let (tx, rx) = futures::channel::oneshot::channel(); - spawn(async move { - tx.send(future.await) - .unwrap_or_else(|_| eprintln!("failed to send but expected rx to still be available")); - }); - wait_for_receiver(rx) -} - -fn wait_for_receiver(mut rx: futures::channel::oneshot::Receiver) -> T { - loop { - // TODO 4: Is there a better way to do this than a busy loop? - if let Some(x) = rx - .try_recv() - .expect("failed to receive. maybe sender panicked") - { - return x; - } - } -} - // TODO 3: Test link in documentation after pushing to main