Skip to content

Commit

Permalink
Remove async variants (#71)
Browse files Browse the repository at this point in the history
* Remove async variants

Rplace with a couple helper functions

* Remove unecessary allocation

* Add some docs
  • Loading branch information
intendednull authored Dec 19, 2023
1 parent 730d418 commit 1a2f1bb
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 736 deletions.
11 changes: 0 additions & 11 deletions crates/yewdux-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use proc_macro::TokenStream;
use proc_macro_error::proc_macro_error;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

mod store;
Expand All @@ -11,13 +10,3 @@ pub fn store(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
store::derive(input).into()
}

#[proc_macro_attribute]
pub fn async_reducer(_attr: TokenStream, item: TokenStream) -> TokenStream {
let item = proc_macro2::TokenStream::from(item);
quote! {
#[::yewdux::async_trait(?Send)]
#item
}
.into()
}
3 changes: 0 additions & 3 deletions crates/yewdux/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ doctests = []

[dependencies]
anymap = "1.0.0-beta.2"
async-trait = "0.1.58"
log = "0.4.16"
serde = { version = "1.0.114", features = ["rc"] }
serde_json = "1.0.64"
Expand All @@ -32,6 +31,4 @@ yewdux-macros = { path = "../yewdux-macros" }
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2"

[dev-dependencies]
async-std = { version = "1.11", features = ["attributes"] }

44 changes: 1 addition & 43 deletions crates/yewdux/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::rc::Rc;
#[cfg(feature = "future")]
use std::{future::Future, pin::Pin};

use anymap::AnyMap;

use crate::{
mrc::Mrc,
store::{AsyncReducer, Reducer, Store},
store::{Reducer, Store},
subscriber::{Callable, SubscriberId, Subscribers},
};

Expand All @@ -33,18 +31,6 @@ impl<S: Store> Entry<S> {
// Return whether or not subscribers should be notified.
self.store.borrow().should_notify(&old)
}

/// Apply a future reduction to state, returning if it should notify subscribers or not.
#[cfg(feature = "future")]
pub(crate) async fn reduce_future<R: AsyncReducer<S>>(&self, reducer: R) -> bool {
let old = Rc::clone(&self.store.borrow());
// Apply the reducer.
let new = reducer.apply(Rc::clone(&old)).await;
// Update the new state.
*self.store.borrow_mut() = new;
// Return whether or not subscribers should be notified.
self.store.borrow().should_notify(&old)
}
}

/// Execution context for a dispatch
Expand Down Expand Up @@ -127,41 +113,13 @@ impl Context {
}
}

#[cfg(feature = "future")]
pub async fn reduce_future<S, R>(&self, r: R)
where
S: Store,
R: AsyncReducer<S>,
{
let entry = self.get_or_init::<S>();
let should_notify = entry.reduce_future(r).await;

if should_notify {
let state = Rc::clone(&entry.store.borrow());
self.notify_subscribers(state)
}
}

pub fn reduce_mut<S: Store + Clone, F: FnOnce(&mut S)>(&self, f: F) {
self.reduce(|mut state| {
f(Rc::make_mut(&mut state));
state
});
}

#[cfg(feature = "future")]
pub async fn reduce_mut_future<S, R, F>(&self, f: F)
where
S: Store + Clone,
F: FnOnce(&mut S) -> Pin<Box<dyn Future<Output = R> + '_>>,
{
self.reduce_future(|mut state| async move {
f(Rc::make_mut(&mut state)).await;
state
})
.await;
}

/// Set state to given value.
pub fn set<S: Store>(&self, value: S) {
self.reduce(move |_| value.into());
Expand Down
Loading

0 comments on commit 1a2f1bb

Please sign in to comment.