Skip to content

Commit

Permalink
make AsyncLoader thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
dakom committed Mar 21, 2024
1 parent 69df7c8 commit 35fd588
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
20 changes: 10 additions & 10 deletions crate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "awsm_web"
edition = "2018"
version = "0.44.0"
version = "0.45.0"
authors = ["David Komer <david.komer@gmail.com>"]
license = "MIT OR Apache-2.0"
readme = "../README.md"
Expand Down Expand Up @@ -185,17 +185,17 @@ unwrap_verbose = []
disable_webgl_opt = []

[dependencies]
web-sys = "0.3.66"
js-sys = "0.3.66"
wasm-bindgen = "0.2.89"
log = "0.4.20"
web-sys = "0.3.69"
js-sys = "0.3.69"
wasm-bindgen = "0.2.92"
log = "0.4.21"
rustc-hash = { version = "1.1.0", optional = true }
serde = { version = "1.0.193", features = ["derive"], optional = true }
serde_json = { version = "1.0.108", optional = true }
serde = { version = "1.0.197", features = ["derive"], optional = true }
serde_json = { version = "1.0.114", optional = true }
beach_map = { version = "0.2.1", optional = true }
wasm-bindgen-futures= { version = "0.4.39", optional = true }
serde-wasm-bindgen = { version = "0.6.1", optional = true }
futures = { version = "0.3.29", optional = true }
wasm-bindgen-futures= { version = "0.4.42", optional = true }
serde-wasm-bindgen = { version = "0.6.5", optional = true }
futures = { version = "0.3.30", optional = true }
gloo-timers = { version = "0.3.0", features = ["futures"], optional = true }
discard = { version = "1.0.4", optional = true }
cfg-if = "1.0.0"
Expand Down
22 changes: 9 additions & 13 deletions crate/src/loaders/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::ops::Deref;
use std::{ops::Deref, sync::{Arc, Mutex}};
use wasm_bindgen_futures::spawn_local;
use std::{
future::Future,
sync::atomic::{AtomicUsize, Ordering},
rc::Rc,
cell::RefCell
};
use wasm_bindgen::prelude::*;
use futures::{
Expand Down Expand Up @@ -99,7 +97,7 @@ pub async fn future_until<F, A>(ms: u32, f: F) -> Option<A>
///
/// Hold onto the AsyncLoader somewhere and call load(async move {...}) or cancel()
pub struct AsyncLoader {
loading: Rc<RefCell<Option<AsyncState>>>,
loading: Arc<Mutex<Option<AsyncState>>>,
}
impl Drop for AsyncLoader {
fn drop(&mut self) {
Expand All @@ -124,22 +122,20 @@ impl AsyncState {
impl AsyncLoader {
pub fn new() -> Self {
Self {
loading: Rc::new(RefCell::new(None)),
loading: Arc::new(Mutex::new(None)),
}
}

pub fn cancel(&self) {
self.replace(None);
}

fn replace(&self, value: Option<AsyncState>) {
let mut loading = self.loading.borrow_mut();
fn replace(&self, state: Option<AsyncState>) {
let mut loading = self.loading.lock().unwrap();

if let Some(state) = loading.as_mut() {
state.handle.abort();
if let Some(old_state) = std::mem::replace(&mut *loading, state) {
old_state.handle.abort();
}

*loading = value;
}

pub fn load<F>(&self, fut: F) where F: Future<Output = ()> + 'static {
Expand All @@ -155,7 +151,7 @@ impl AsyncLoader {
spawn_local(async move {
match fut.await {
Ok(()) => {
let mut loading = loading.borrow_mut();
let mut loading = loading.lock().unwrap();

if let Some(current_id) = loading.as_ref().map(|x| x.id) {
// If it hasn't been overwritten with a new state...
Expand All @@ -173,7 +169,7 @@ impl AsyncLoader {
}

pub fn is_loading(&self) -> bool {
self.loading.borrow().is_some()
self.loading.lock().unwrap().is_some()
}
}

0 comments on commit 35fd588

Please sign in to comment.