Skip to content

Commit

Permalink
backout of commit 7e3e8ff
Browse files Browse the repository at this point in the history
Signed-off-by: Manmeet Singh <[email protected]>
  • Loading branch information
maan2003 committed Apr 15, 2024
1 parent f7329c7 commit a0a6e00
Showing 1 changed file with 8 additions and 31 deletions.
39 changes: 8 additions & 31 deletions crates/matrix-sdk-common/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
//! Abstraction over an executor so we can spawn tasks under WASM the same way
//! we do usually.
#[cfg(target_arch = "wasm32")]
pub use std::convert::Infallible as JoinError;
#[cfg(target_arch = "wasm32")]
use std::{
future::Future,
Expand All @@ -23,12 +25,7 @@ use std::{
};

#[cfg(target_arch = "wasm32")]
pub use futures_util::future::Aborted as JoinError;
#[cfg(target_arch = "wasm32")]
use futures_util::{
future::{AbortHandle, Abortable, RemoteHandle},
FutureExt,
};
use futures_util::{future::RemoteHandle, FutureExt};
#[cfg(not(target_arch = "wasm32"))]
pub use tokio::task::{spawn, JoinError, JoinHandle};

Expand All @@ -37,44 +34,24 @@ pub fn spawn<F, T>(future: F) -> JoinHandle<T>
where
F: Future<Output = T> + 'static,
{
let (future, remote_handle) = future.remote_handle();
let (abort_handle, abort_registration) = AbortHandle::new_pair();
let future = Abortable::new(future, abort_registration);

wasm_bindgen_futures::spawn_local(async {
// Poll the future, and ignore the result (either it's `Ok(())`, or it's
// `Err(Aborted)`).
let _ = future.await;
});
let (fut, handle) = future.remote_handle();
wasm_bindgen_futures::spawn_local(fut);

JoinHandle { remote_handle, abort_handle }
JoinHandle { handle }
}

#[cfg(target_arch = "wasm32")]
#[derive(Debug)]
pub struct JoinHandle<T> {
remote_handle: RemoteHandle<T>,
abort_handle: AbortHandle,
}

#[cfg(target_arch = "wasm32")]
impl<T> JoinHandle<T> {
pub fn abort(&self) {
self.abort_handle.abort();
}
handle: RemoteHandle<T>,
}

#[cfg(target_arch = "wasm32")]
impl<T: 'static> Future for JoinHandle<T> {
type Output = Result<T, JoinError>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.abort_handle.is_aborted() {
// The future has been aborted. It is not possible to poll it again.
Poll::Ready(Err(JoinError))
} else {
Pin::new(&mut self.remote_handle).poll(cx).map(Ok)
}
Pin::new(&mut self.handle).poll(cx).map(Ok)
}
}

Expand Down

0 comments on commit a0a6e00

Please sign in to comment.