|
@@ -1,10 +1,14 @@
|
|
|
+use crate::runtime::RuntimeError;
|
|
|
use crate::{innerlude::SuspendedFuture, runtime::Runtime, CapturedError, Element, ScopeId, Task};
|
|
|
use std::future::Future;
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
/// Get the current scope id
|
|
|
-pub fn current_scope_id() -> Option<ScopeId> {
|
|
|
- Runtime::with(|rt| rt.current_scope_id()).flatten()
|
|
|
+pub fn current_scope_id() -> Result<ScopeId, RuntimeError> {
|
|
|
+ Runtime::with(|rt| rt.current_scope_id().ok())
|
|
|
+ .ok()
|
|
|
+ .flatten()
|
|
|
+ .ok_or(RuntimeError::new())
|
|
|
}
|
|
|
|
|
|
#[doc(hidden)]
|
|
@@ -31,19 +35,20 @@ pub fn vdom_is_rendering() -> bool {
|
|
|
/// }
|
|
|
/// ```
|
|
|
pub fn throw_error(error: impl Into<CapturedError> + 'static) {
|
|
|
- current_scope_id()
|
|
|
- .expect("to be in a dioxus runtime")
|
|
|
- .throw_error(error)
|
|
|
+ current_scope_id().unwrap().throw_error(error)
|
|
|
}
|
|
|
|
|
|
/// Consume context from the current scope
|
|
|
pub fn try_consume_context<T: 'static + Clone>() -> Option<T> {
|
|
|
- Runtime::with_current_scope(|cx| cx.consume_context::<T>()).flatten()
|
|
|
+ Runtime::with_current_scope(|cx| cx.consume_context::<T>())
|
|
|
+ .ok()
|
|
|
+ .flatten()
|
|
|
}
|
|
|
|
|
|
/// Consume context from the current scope
|
|
|
pub fn consume_context<T: 'static + Clone>() -> T {
|
|
|
Runtime::with_current_scope(|cx| cx.consume_context::<T>())
|
|
|
+ .ok()
|
|
|
.flatten()
|
|
|
.unwrap_or_else(|| panic!("Could not find context {}", std::any::type_name::<T>()))
|
|
|
}
|
|
@@ -54,23 +59,25 @@ pub fn consume_context_from_scope<T: 'static + Clone>(scope_id: ScopeId) -> Opti
|
|
|
rt.get_state(scope_id)
|
|
|
.and_then(|cx| cx.consume_context::<T>())
|
|
|
})
|
|
|
+ .ok()
|
|
|
.flatten()
|
|
|
}
|
|
|
|
|
|
/// Check if the current scope has a context
|
|
|
pub fn has_context<T: 'static + Clone>() -> Option<T> {
|
|
|
- Runtime::with_current_scope(|cx| cx.has_context::<T>()).flatten()
|
|
|
+ Runtime::with_current_scope(|cx| cx.has_context::<T>())
|
|
|
+ .ok()
|
|
|
+ .flatten()
|
|
|
}
|
|
|
|
|
|
/// Provide context to the current scope
|
|
|
pub fn provide_context<T: 'static + Clone>(value: T) -> T {
|
|
|
- Runtime::with_current_scope(|cx| cx.provide_context(value)).expect("to be in a dioxus runtime")
|
|
|
+ Runtime::with_current_scope(|cx| cx.provide_context(value)).unwrap()
|
|
|
}
|
|
|
|
|
|
/// Provide a context to the root scope
|
|
|
pub fn provide_root_context<T: 'static + Clone>(value: T) -> T {
|
|
|
- Runtime::with_current_scope(|cx| cx.provide_root_context(value))
|
|
|
- .expect("to be in a dioxus runtime")
|
|
|
+ Runtime::with_current_scope(|cx| cx.provide_root_context(value)).unwrap()
|
|
|
}
|
|
|
|
|
|
/// Suspended the current component on a specific task and then return None
|
|
@@ -108,7 +115,7 @@ pub fn suspend(task: Task) -> Element {
|
|
|
///
|
|
|
#[doc = include_str!("../docs/common_spawn_errors.md")]
|
|
|
pub fn spawn_isomorphic(fut: impl Future<Output = ()> + 'static) -> Task {
|
|
|
- Runtime::with_current_scope(|cx| cx.spawn_isomorphic(fut)).expect("to be in a dioxus runtime")
|
|
|
+ Runtime::with_current_scope(|cx| cx.spawn_isomorphic(fut)).unwrap()
|
|
|
}
|
|
|
|
|
|
/// Spawns the future but does not return the [`Task`]. This task will automatically be canceled when the component is dropped.
|
|
@@ -134,12 +141,12 @@ pub fn spawn_isomorphic(fut: impl Future<Output = ()> + 'static) -> Task {
|
|
|
///
|
|
|
#[doc = include_str!("../docs/common_spawn_errors.md")]
|
|
|
pub fn spawn(fut: impl Future<Output = ()> + 'static) -> Task {
|
|
|
- Runtime::with_current_scope(|cx| cx.spawn(fut)).expect("to be in a dioxus runtime")
|
|
|
+ Runtime::with_current_scope(|cx| cx.spawn(fut)).unwrap()
|
|
|
}
|
|
|
|
|
|
/// Queue an effect to run after the next render. You generally shouldn't need to interact with this function directly. [use_effect](https://docs.rs/dioxus-hooks/latest/dioxus_hooks/fn.use_effect.html) will call this function for you.
|
|
|
pub fn queue_effect(f: impl FnOnce() + 'static) {
|
|
|
- Runtime::with_current_scope(|cx| cx.queue_effect(f)).expect("to be in a dioxus runtime")
|
|
|
+ Runtime::with_current_scope(|cx| cx.queue_effect(f)).unwrap()
|
|
|
}
|
|
|
|
|
|
/// Spawn a future that Dioxus won't clean up when this component is unmounted
|
|
@@ -195,7 +202,7 @@ pub fn queue_effect(f: impl FnOnce() + 'static) {
|
|
|
///
|
|
|
#[doc = include_str!("../docs/common_spawn_errors.md")]
|
|
|
pub fn spawn_forever(fut: impl Future<Output = ()> + 'static) -> Option<Task> {
|
|
|
- Runtime::with_scope(ScopeId::ROOT, |cx| cx.spawn(fut))
|
|
|
+ Runtime::with_scope(ScopeId::ROOT, |cx| cx.spawn(fut)).ok()
|
|
|
}
|
|
|
|
|
|
/// Informs the scheduler that this task is no longer needed and should be removed.
|
|
@@ -248,30 +255,33 @@ pub fn remove_future(id: Task) {
|
|
|
/// })
|
|
|
/// }
|
|
|
/// ```
|
|
|
+#[track_caller]
|
|
|
pub fn use_hook<State: Clone + 'static>(initializer: impl FnOnce() -> State) -> State {
|
|
|
- Runtime::with_current_scope(|cx| cx.use_hook(initializer)).expect("to be in a dioxus runtime")
|
|
|
+ Runtime::with_current_scope(|cx| cx.use_hook(initializer)).unwrap()
|
|
|
}
|
|
|
|
|
|
/// Get the current render since the inception of this component
|
|
|
///
|
|
|
/// This can be used as a helpful diagnostic when debugging hooks/renders, etc
|
|
|
pub fn generation() -> usize {
|
|
|
- Runtime::with_current_scope(|cx| cx.generation()).expect("to be in a dioxus runtime")
|
|
|
+ Runtime::with_current_scope(|cx| cx.generation()).unwrap()
|
|
|
}
|
|
|
|
|
|
/// Get the parent of the current scope if it exists
|
|
|
pub fn parent_scope() -> Option<ScopeId> {
|
|
|
- Runtime::with_current_scope(|cx| cx.parent_id()).flatten()
|
|
|
+ Runtime::with_current_scope(|cx| cx.parent_id())
|
|
|
+ .ok()
|
|
|
+ .flatten()
|
|
|
}
|
|
|
|
|
|
/// Mark the current scope as dirty, causing it to re-render
|
|
|
pub fn needs_update() {
|
|
|
- Runtime::with_current_scope(|cx| cx.needs_update());
|
|
|
+ let _ = Runtime::with_current_scope(|cx| cx.needs_update());
|
|
|
}
|
|
|
|
|
|
/// Mark the current scope as dirty, causing it to re-render
|
|
|
pub fn needs_update_any(id: ScopeId) {
|
|
|
- Runtime::with_current_scope(|cx| cx.needs_update_any(id));
|
|
|
+ let _ = Runtime::with_current_scope(|cx| cx.needs_update_any(id));
|
|
|
}
|
|
|
|
|
|
/// Schedule an update for the current component
|
|
@@ -280,7 +290,7 @@ pub fn needs_update_any(id: ScopeId) {
|
|
|
///
|
|
|
/// You should prefer [`schedule_update_any`] if you need to update multiple components.
|
|
|
pub fn schedule_update() -> Arc<dyn Fn() + Send + Sync> {
|
|
|
- Runtime::with_current_scope(|cx| cx.schedule_update()).expect("to be in a dioxus runtime")
|
|
|
+ Runtime::with_current_scope(|cx| cx.schedule_update()).unwrap()
|
|
|
}
|
|
|
|
|
|
/// Schedule an update for any component given its [`ScopeId`].
|
|
@@ -289,7 +299,7 @@ pub fn schedule_update() -> Arc<dyn Fn() + Send + Sync> {
|
|
|
///
|
|
|
/// Note: Unlike [`needs_update`], the function returned by this method will work outside of the dioxus runtime.
|
|
|
pub fn schedule_update_any() -> Arc<dyn Fn(ScopeId) + Send + Sync> {
|
|
|
- Runtime::with_current_scope(|cx| cx.schedule_update_any()).expect("to be in a dioxus runtime")
|
|
|
+ Runtime::with_current_scope(|cx| cx.schedule_update_any()).unwrap()
|
|
|
}
|
|
|
|
|
|
/// Creates a callback that will be run before the component is removed.
|
|
@@ -393,12 +403,12 @@ pub fn use_after_render(f: impl FnMut() + 'static) {
|
|
|
/// This is a hook and will always run, so you can't unschedule it
|
|
|
/// Will run for every progression of suspense, though this might change in the future
|
|
|
pub fn before_render(f: impl FnMut() + 'static) {
|
|
|
- Runtime::with_current_scope(|cx| cx.push_before_render(f));
|
|
|
+ let _ = Runtime::with_current_scope(|cx| cx.push_before_render(f));
|
|
|
}
|
|
|
|
|
|
/// Push a function to be run after the render is complete, even if it didn't complete successfully
|
|
|
pub fn after_render(f: impl FnMut() + 'static) {
|
|
|
- Runtime::with_current_scope(|cx| cx.push_after_render(f));
|
|
|
+ let _ = Runtime::with_current_scope(|cx| cx.push_after_render(f));
|
|
|
}
|
|
|
|
|
|
/// Use a hook with a cleanup function
|