Skip to content

Commit

Permalink
Fix doc tests
Browse files Browse the repository at this point in the history
Includes a workaround for rust-lang/rust#67295

Global context is only available for wasm, but tests are run natively.
Because #[cfg(doctest)] does not work as expected, we include a feature
to fill that role. We should eventually switch back over when the fix is
merged into cargo.
  • Loading branch information
intendednull committed Dec 12, 2023
1 parent 19773d2 commit 227bf31
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
3 changes: 3 additions & 0 deletions crates/yewdux/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ categories = ["wasm", "web-programming", "rust-patterns"]
default = ["future"]
future = []

# INTERNAL USE ONLY
doctests = []

[dependencies]
anymap = "1.0.0-beta.2"
async-trait = "0.1.58"
Expand Down
2 changes: 1 addition & 1 deletion crates/yewdux/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Context {
Default::default()
}

#[cfg(target_arch = "wasm32")]
#[cfg(any(doc, feature = "doctests", target_arch = "wasm32"))]
pub fn global() -> Self {
thread_local! {
static CONTEXT: Context = Default::default();
Expand Down
14 changes: 8 additions & 6 deletions crates/yewdux/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<S: Store> Dispatch<S> {
/// Create a new dispatch with the global context (thread local).
///
/// This is only available for wasm. For SSR, see the YewduxRoot pattern.
#[cfg(target_arch = "wasm32")]
#[cfg(any(doc, feature = "doctests", target_arch = "wasm32"))]
pub fn global() -> Self {
Self::new(&Context::global())
}
Expand Down Expand Up @@ -103,7 +103,7 @@ impl<S: Store> Dispatch<S> {
///
/// fn create(ctx: &Context<Self>) -> Self {
/// let on_change = ctx.link().callback(Msg::State);
/// let dispatch = Dispatch::subscribe_global(on_change);
/// let dispatch = Dispatch::global().subscribe(on_change);
/// Self {
/// state: dispatch.get(),
/// dispatch,
Expand Down Expand Up @@ -341,8 +341,9 @@ impl<S: Store> Dispatch<S> {
/// # struct State {
/// # count: u32,
/// # }
/// # fn main() {
/// let dispatch = Dispatch::<State>::global();
/// # #[hook]
/// # fn use_foo() {
/// let dispatch = use_dispatch::<State>();
/// let onchange = dispatch.set_callback(|event: Event| {
/// let value = event.target_unchecked_into::<web_sys::HtmlInputElement>().value();
/// State { count: value.parse().unwrap() }
Expand Down Expand Up @@ -760,8 +761,9 @@ impl<S: Store> Dispatch<S> {
/// # async fn get_incr() -> u32 {
/// # 1
/// # }
/// # fn main() {
/// let dispatch = Dispatch::<State>::global();
/// # #[hook]
/// # fn use_foo() {
/// let dispatch = use_dispatch::<State>();
/// let onchange = dispatch.reduce_mut_future_callback_with(|state, event: Event| {
/// Box::pin(async move {
/// let value = event
Expand Down
9 changes: 8 additions & 1 deletion crates/yewdux/src/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ fn use_cx() -> Context {
}
}

#[hook]
pub fn use_dispatch<S: Store>() -> Dispatch<S> {
let cx = use_cx();
Dispatch::new(&cx)
}

/// This hook allows accessing the state of a store. When the store is modified, a re-render is
/// automatically triggered.
///
Expand Down Expand Up @@ -78,8 +84,9 @@ pub fn use_store_value<S: Store>() -> Rc<S> {
///
/// #[function_component]
/// fn App() -> Html {
/// let dispatch = use_dispatch::<State>();
/// let count = use_selector(|state: &State| state.count);
/// let onclick = Dispatch::<State>::new().reduce_mut_callback(|state| state.count += 1);
/// let onclick = dispatch.reduce_mut_callback(|state| state.count += 1);
///
/// html! {
/// <>
Expand Down
4 changes: 2 additions & 2 deletions crates/yewdux/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ pub mod prelude {
context_provider::YewduxRoot,
dispatch::Dispatch,
functional::{
use_selector, use_selector_eq, use_selector_eq_with_deps, use_selector_with_deps,
use_store, use_store_value,
use_dispatch, use_selector, use_selector_eq, use_selector_eq_with_deps,
use_selector_with_deps, use_store, use_store_value,
},
listener::{init_listener, Listener},
store::{Reducer, Store},
Expand Down

0 comments on commit 227bf31

Please sign in to comment.