Skip to content

Commit

Permalink
fix: spawn correctly on wasm
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 a0a6e00 commit 928d00b
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions crates/matrix-sdk-common/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
//! 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,
pin::Pin,
task::{Context, Poll},
};

#[cfg(target_arch = "wasm32")]
pub use futures_util::future::Aborted as JoinError;
#[cfg(target_arch = "wasm32")]
use futures_util::{future::RemoteHandle, FutureExt};
#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -37,21 +37,42 @@ where
let (fut, handle) = future.remote_handle();
wasm_bindgen_futures::spawn_local(fut);

JoinHandle { handle }
JoinHandle { handle: Some(handle) }
}

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

#[cfg(target_arch = "wasm32")]
impl<T> JoinHandle<T> {
pub fn abort(&mut self) {
drop(self.handle.take());
}
}

#[cfg(target_arch = "wasm32")]
impl<T> Drop for JoinHandle<T> {
fn drop(&mut self) {
// don't abort the spawned future
if let Some(h) = self.handle.take() {
h.forget();
}
}
}

#[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> {
Pin::new(&mut self.handle).poll(cx).map(Ok)
if let Some(handle) = self.handle.as_mut() {
Pin::new(handle).poll(cx).map(Ok)
} else {
Poll::Ready(Err(JoinError))
}
}
}

Expand All @@ -73,7 +94,8 @@ mod tests {
#[async_test]
async fn test_abort() {
let future = async { 42 };
let join_handle = spawn(future);
#[allow(unused_mut)]
let mut join_handle = spawn(future);

join_handle.abort();

Expand Down

0 comments on commit 928d00b

Please sign in to comment.