Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 227bf31

Browse files
committedDec 12, 2023
Fix doc tests
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.
1 parent 19773d2 commit 227bf31

File tree

5 files changed

+22
-10
lines changed

5 files changed

+22
-10
lines changed
 

‎crates/yewdux/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ categories = ["wasm", "web-programming", "rust-patterns"]
1414
default = ["future"]
1515
future = []
1616

17+
# INTERNAL USE ONLY
18+
doctests = []
19+
1720
[dependencies]
1821
anymap = "1.0.0-beta.2"
1922
async-trait = "0.1.58"

‎crates/yewdux/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl Context {
7070
Default::default()
7171
}
7272

73-
#[cfg(target_arch = "wasm32")]
73+
#[cfg(any(doc, feature = "doctests", target_arch = "wasm32"))]
7474
pub fn global() -> Self {
7575
thread_local! {
7676
static CONTEXT: Context = Default::default();

‎crates/yewdux/src/dispatch.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<S: Store> Dispatch<S> {
5454
/// Create a new dispatch with the global context (thread local).
5555
///
5656
/// This is only available for wasm. For SSR, see the YewduxRoot pattern.
57-
#[cfg(target_arch = "wasm32")]
57+
#[cfg(any(doc, feature = "doctests", target_arch = "wasm32"))]
5858
pub fn global() -> Self {
5959
Self::new(&Context::global())
6060
}
@@ -103,7 +103,7 @@ impl<S: Store> Dispatch<S> {
103103
///
104104
/// fn create(ctx: &Context<Self>) -> Self {
105105
/// let on_change = ctx.link().callback(Msg::State);
106-
/// let dispatch = Dispatch::subscribe_global(on_change);
106+
/// let dispatch = Dispatch::global().subscribe(on_change);
107107
/// Self {
108108
/// state: dispatch.get(),
109109
/// dispatch,
@@ -341,8 +341,9 @@ impl<S: Store> Dispatch<S> {
341341
/// # struct State {
342342
/// # count: u32,
343343
/// # }
344-
/// # fn main() {
345-
/// let dispatch = Dispatch::<State>::global();
344+
/// # #[hook]
345+
/// # fn use_foo() {
346+
/// let dispatch = use_dispatch::<State>();
346347
/// let onchange = dispatch.set_callback(|event: Event| {
347348
/// let value = event.target_unchecked_into::<web_sys::HtmlInputElement>().value();
348349
/// State { count: value.parse().unwrap() }
@@ -760,8 +761,9 @@ impl<S: Store> Dispatch<S> {
760761
/// # async fn get_incr() -> u32 {
761762
/// # 1
762763
/// # }
763-
/// # fn main() {
764-
/// let dispatch = Dispatch::<State>::global();
764+
/// # #[hook]
765+
/// # fn use_foo() {
766+
/// let dispatch = use_dispatch::<State>();
765767
/// let onchange = dispatch.reduce_mut_future_callback_with(|state, event: Event| {
766768
/// Box::pin(async move {
767769
/// let value = event

‎crates/yewdux/src/functional.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ fn use_cx() -> Context {
1717
}
1818
}
1919

20+
#[hook]
21+
pub fn use_dispatch<S: Store>() -> Dispatch<S> {
22+
let cx = use_cx();
23+
Dispatch::new(&cx)
24+
}
25+
2026
/// This hook allows accessing the state of a store. When the store is modified, a re-render is
2127
/// automatically triggered.
2228
///
@@ -78,8 +84,9 @@ pub fn use_store_value<S: Store>() -> Rc<S> {
7884
///
7985
/// #[function_component]
8086
/// fn App() -> Html {
87+
/// let dispatch = use_dispatch::<State>();
8188
/// let count = use_selector(|state: &State| state.count);
82-
/// let onclick = Dispatch::<State>::new().reduce_mut_callback(|state| state.count += 1);
89+
/// let onclick = dispatch.reduce_mut_callback(|state| state.count += 1);
8390
///
8491
/// html! {
8592
/// <>

‎crates/yewdux/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ pub mod prelude {
6060
context_provider::YewduxRoot,
6161
dispatch::Dispatch,
6262
functional::{
63-
use_selector, use_selector_eq, use_selector_eq_with_deps, use_selector_with_deps,
64-
use_store, use_store_value,
63+
use_dispatch, use_selector, use_selector_eq, use_selector_eq_with_deps,
64+
use_selector_with_deps, use_store, use_store_value,
6565
},
6666
listener::{init_listener, Listener},
6767
store::{Reducer, Store},

0 commit comments

Comments
 (0)
Please sign in to comment.