[dependencies]
anyhow = "1.0"
derivative = "2.2.0"
+futures = { version = "0.3", default-features = false, features = ["std"] }
# If you need to use the development version of Yew
# yew = { git = "https://github.com/yewstack/yew/", features = ["csr"] }
# yew-router = { git = "https://github.com/yewstack/yew/" }
--- /dev/null
+use futures::future::{abortable, AbortHandle, Future};
+
+/// Abort guard for async functions.
+pub struct AsyncAbortGuard {
+ abort_handle: AbortHandle,
+}
+
+impl AsyncAbortGuard {
+ /// Runs a Rust Future on the current thread.
+ ///
+ /// The future is aborted as soon as the returned guard gets dropped.
+ pub fn spawn<F>(future: F) -> Self
+ where
+ F: Future<Output = ()> + 'static,
+ {
+ let (future, abort_handle) = abortable(future);
+
+ wasm_bindgen_futures::spawn_local(async move {
+ match future.await {
+ Ok(()) => { /* do nothing */ }
+ Err(futures::future::Aborted) => { /* do nothing (maybe we want to log this?) */ }
+ }
+ });
+ AsyncAbortGuard { abort_handle }
+ }
+}
+
+impl Drop for AsyncAbortGuard {
+ fn drop(&mut self) {
+ self.abort_handle.abort();
+ }
+}
use serde::{de::DeserializeOwned, Serialize};
+mod async_abort_guard;
+pub use async_abort_guard::AsyncAbortGuard;
+
mod data_store;
pub use data_store::{DataNode, DataNodeDerefGuard, DataStore};