Change context api to panic with nice error message

This commit is contained in:
Jonathan Kelley committed 2024-01-15 18:02:58 -08:00
1 parent ee5020c41f
commit 14651a3573
19 files changed
+96 -69

No files matched your search

+2 -2
View File
@@ -1,5 +1,5 @@
use crate::{
global_context::{consume_context, current_scope_id},
global_context::{current_scope_id, try_consume_context},
innerlude::provide_context,
use_hook, Element, IntoDynNode, Properties, ScopeId, Template, TemplateAttribute, TemplateNode,
VNode,
@@ -202,7 +202,7 @@ pub trait Throw<S = ()>: Sized {
}
fn throw_error<T>(e: impl Debug + 'static) -> Option<T> {
if let Some(cx) = consume_context::<ErrorBoundary>() {
if let Some(cx) = try_consume_context::<ErrorBoundary>() {
match current_scope_id() {
Some(id) => cx.insert_error(id, Box::new(e), Backtrace::capture()),
None => {
+8 -1
View File
@@ -19,10 +19,17 @@ pub fn vdom_is_rendering() -> bool {
}
/// Consume context from the current scope
pub fn consume_context<T: 'static + Clone>() -> Option<T> {
pub fn try_consume_context<T: 'static + Clone>() -> Option<T> {
with_current_scope(|cx| cx.consume_context::<T>()).flatten()
}
/// Consume context from the current scope
pub fn consume_context<T: 'static + Clone>() -> T {
with_current_scope(|cx| cx.consume_context::<T>())
.flatten()
.unwrap_or_else(|| panic!("Could not find context {}", std::any::type_name::<T>(),))
}
/// Consume context from the current scope
pub fn consume_context_from_scope<T: 'static + Clone>(scope_id: ScopeId) -> Option<T> {
with_runtime(|rt| {
+2 -2
View File
@@ -88,8 +88,8 @@ pub mod prelude {
consume_context, consume_context_from_scope, current_scope_id, fc_to_builder, generation,
has_context, needs_update, parent_scope, provide_context, provide_root_context,
push_future, remove_future, schedule_update, schedule_update_any, spawn, spawn_forever,
suspend, use_error_boundary, use_hook, AnyValue, Attribute, Component, Element,
ErrorBoundary, Event, EventHandler, Fragment, HasAttributes, IntoAttributeValue,
suspend, try_consume_context, use_error_boundary, use_hook, AnyValue, Attribute, Component,
Element, ErrorBoundary, Event, EventHandler, Fragment, HasAttributes, IntoAttributeValue,
IntoDynNode, Properties, Runtime, RuntimeGuard, ScopeId, ScopeState, Task, Template,
TemplateAttribute, TemplateNode, Throw, VNode, VNodeInner, VirtualDom,
};
-1
View File
@@ -1,5 +1,4 @@
use crate::ScopeId;
use slab::Slab;
mod task;
mod wait;