Change context api to panic with nice error message
This commit is contained in:
19 files changed
+96
-69
No files matched your search
@@ -58,8 +58,8 @@ macro_rules! to_owned {
|
||||
mod use_on_destroy;
|
||||
pub use use_on_destroy::*;
|
||||
|
||||
// mod use_context;
|
||||
// pub use use_context::*;
|
||||
mod use_context;
|
||||
pub use use_context::*;
|
||||
|
||||
mod use_coroutine;
|
||||
pub use use_coroutine::*;
|
||||
|
||||
@@ -1,20 +1,31 @@
|
||||
use dioxus_core::ScopeState;
|
||||
use dioxus_core::{
|
||||
prelude::{consume_context, provide_context, try_consume_context},
|
||||
use_hook,
|
||||
};
|
||||
|
||||
/// Consume some context in the tree, providing a sharable handle to the value
|
||||
///
|
||||
/// Does not regenerate the value if the value is changed at the parent.
|
||||
#[must_use]
|
||||
pub fn use_context<T: 'static + Clone>() -> Option<&T> {
|
||||
cx.use_hook(|| cx.consume_context::<T>()).as_ref()
|
||||
pub fn try_use_context<T: 'static + Clone>() -> Option<T> {
|
||||
use_hook(|| try_consume_context::<T>())
|
||||
}
|
||||
|
||||
/// Consume some context in the tree, providing a sharable handle to the value
|
||||
///
|
||||
/// Does not regenerate the value if the value is changed at the parent.
|
||||
#[must_use]
|
||||
pub fn use_context<T: 'static + Clone>() -> T {
|
||||
use_hook(|| consume_context::<T>())
|
||||
}
|
||||
|
||||
/// Provide some context via the tree and return a reference to it
|
||||
///
|
||||
/// Once the context has been provided, it is immutable. Mutations should be done via interior mutability.
|
||||
pub fn use_context_provider<T: 'static + Clone>(f: impl FnOnce() -> T) -> &T {
|
||||
cx.use_hook(|| {
|
||||
pub fn use_context_provider<T: 'static + Clone>(f: impl FnOnce() -> T) -> T {
|
||||
use_hook(|| {
|
||||
let val = f();
|
||||
cx.provide_context(val.clone());
|
||||
provide_context(val.clone());
|
||||
val
|
||||
})
|
||||
}
|
||||
@@ -96,7 +96,7 @@ where
|
||||
///
|
||||
/// See the docs for [`use_coroutine`] for more details.
|
||||
#[must_use]
|
||||
pub fn use_coroutine_handle<M: 'static>() -> Option<Coroutine<M>> {
|
||||
pub fn use_coroutine_handle<M: 'static>() -> Coroutine<M> {
|
||||
use_hook(|| consume_context::<Coroutine<M>>())
|
||||
}
|
||||
|
||||
@@ -118,6 +118,10 @@ impl<T> Coroutine<T> {
|
||||
let _ = self.tx.read().as_ref().unwrap().unbounded_send(msg);
|
||||
}
|
||||
|
||||
pub fn tx(&self) -> UnboundedSender<T> {
|
||||
self.tx.read().as_ref().unwrap().clone()
|
||||
}
|
||||
|
||||
/// Restart this coroutine
|
||||
///
|
||||
/// Forces the component to re-render, which will re-invoke the coroutine.
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use dioxus_core::{prelude::consume_context, prelude::provide_root_context, use_hook};
|
||||
use dioxus_core::{prelude::provide_root_context, prelude::try_consume_context, use_hook};
|
||||
|
||||
///
|
||||
pub fn use_root_context<T: 'static + Clone>(new: impl FnOnce() -> T) -> T {
|
||||
use_hook(|| {
|
||||
consume_context::<T>()
|
||||
try_consume_context::<T>()
|
||||
// If no context is provided, create a new one at the root
|
||||
.unwrap_or_else(|| provide_root_context(new()).expect(" A runtime to exist"))
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user