feat: transition to cx, props

This commit is contained in:
Jonathan Kelley committed 2021-09-21 13:13:15 -04:00
1 parent f1cff845ce
commit bfdcb20437
14 files changed
+88 -50

No files matched your search

+1 -1
View File
@@ -29,7 +29,7 @@ use crate::innerlude::{Context, DomTree, LazyNodes, FC};
/// You want to use this free-function when your fragment needs a key and simply returning multiple nodes from rsx! won't cut it.
///
#[allow(non_upper_case_globals, non_snake_case)]
pub fn Fragment<'a>(cx: Context<'a, ()>) -> DomTree<'a> {
pub fn Fragment<'a>(cx: Context<'a>, props: &'a ()) -> DomTree<'a> {
cx.render(LazyNodes::new(|f| f.fragment_from_iter(cx.children())))
}
+8 -9
View File
@@ -27,16 +27,15 @@ use std::{any::TypeId, ops::Deref, rc::Rc};
/// }
/// }
/// ```
pub struct Context<'src, T> {
pub props: &'src T,
pub struct Context<'src> {
pub scope: &'src Scope,
}
impl<'src, T> Copy for Context<'src, T> {}
impl<'src, T> Clone for Context<'src, T> {
impl<'src> Copy for Context<'src> {}
impl<'src> Clone for Context<'src> {
fn clone(&self) -> Self {
Self {
props: self.props,
// props: self.props,
scope: self.scope,
}
}
@@ -44,14 +43,14 @@ impl<'src, T> Clone for Context<'src, T> {
// We currently deref to props, but it might make more sense to deref to Scope?
// This allows for code that takes cx.xyz instead of cx.props.xyz
impl<'a, T> Deref for Context<'a, T> {
type Target = &'a T;
impl<'a> Deref for Context<'a> {
type Target = &'a Scope;
fn deref(&self) -> &Self::Target {
&self.props
&self.scope
}
}
impl<'src, P> Context<'src, P> {
impl<'src> Context<'src> {
/// Access the children elements passed into the component
///
/// This enables patterns where a component is passed children from its parent.
+7 -10
View File
@@ -24,8 +24,8 @@ use std::{
///
///
///
pub fn use_task<'src, Out, Fut, Init, P>(
cx: Context<'src, P>,
pub fn use_task<'src, Out, Fut, Init>(
cx: Context<'src>,
task_initializer: Init,
) -> (&'src TaskHandle, &'src Option<Out>)
where
@@ -83,8 +83,8 @@ where
/// # Example
///
///
pub fn use_suspense<'src, Out, Fut, Cb, P>(
cx: Context<'src, P>,
pub fn use_suspense<'src, Out, Fut, Cb>(
cx: Context<'src>,
task_initializer: impl FnOnce() -> Fut,
user_callback: Cb,
) -> DomTree<'src>
@@ -123,10 +123,7 @@ where
Some(value) => {
let out = value.downcast_ref::<Out>().unwrap();
let sus = SuspendedContext {
inner: Context {
props: &(),
scope: cx.scope,
},
inner: Context { scope: cx.scope },
};
user_callback(sus, out)
}
@@ -171,7 +168,7 @@ pub(crate) struct SuspenseHook {
}
pub struct SuspendedContext<'a> {
pub(crate) inner: Context<'a, ()>,
pub(crate) inner: Context<'a>,
}
impl<'src> SuspendedContext<'src> {
@@ -196,7 +193,7 @@ impl<'a, T> Deref for NodeRef<'a, T> {
}
}
pub fn use_node_ref<T, P>(cx: Context<P>) -> NodeRef<T> {
pub fn use_node_ref<T, P>(cx: Context) -> NodeRef<T> {
cx.use_hook(
|f| {},
|f| {
+1 -1
View File
@@ -54,7 +54,7 @@ pub(crate) mod innerlude {
pub use crate::virtual_dom::*;
pub type DomTree<'a> = Option<VNode<'a>>;
pub type FC<P> = fn(Context<P>) -> DomTree;
pub type FC<P> = for<'a> fn(Context<'a>, &'a P) -> DomTree<'a>;
pub use dioxus_core_macro::{format_args_f, html, rsx};
}
+1 -1
View File
@@ -511,7 +511,7 @@ impl<'a> NodeFactory<'a> {
let caller: &'a mut dyn for<'b> Fn(&'b Scope) -> DomTree<'b> =
bump.alloc(move |scope: &Scope| -> DomTree {
let props: &'_ P = unsafe { &*(raw_props as *const P) };
let res = component(Context { props, scope });
let res = component(Context { scope }, props);
unsafe { std::mem::transmute(res) }
});
+1 -4
View File
@@ -275,10 +275,7 @@ impl Scope {
let sus: &'a VSuspended<'a> = unsafe { std::mem::transmute(sus) };
let cx: SuspendedContext<'a> = SuspendedContext {
inner: Context {
props: &(),
scope: self,
},
inner: Context { scope: self },
};
let mut cb = sus.callback.borrow_mut().take().unwrap();
+2 -2
View File
@@ -130,7 +130,7 @@ impl VirtualDom {
// Safety: this callback is only valid for the lifetime of the root props
let root_caller: Box<dyn Fn(&Scope) -> DomTree> = Box::new(move |scope: &Scope| unsafe {
let props: &'_ P = &*(props_ptr as *const P);
std::mem::transmute(root(Context { props, scope }))
std::mem::transmute(root(Context { scope }, props))
});
let scheduler = Scheduler::new();
@@ -203,7 +203,7 @@ impl VirtualDom {
let root_caller: Box<dyn Fn(&Scope) -> DomTree> =
Box::new(move |scope: &Scope| unsafe {
let props: &'_ P = &*(props_ptr as *const P);
std::mem::transmute(root(Context { props, scope }))
std::mem::transmute(root(Context { scope }, props))
});
root_scope.update_scope_dependencies(&root_caller, ScopeChildren(&[]));