Feat: push new component API and context API
This commit is contained in:
1 parent
8329268d39
commit
125f5426a4
14 files changed
+432
-264
No files matched your search
@@ -0,0 +1,19 @@
|
||||
//! An alternative function syntax
|
||||
//!
|
||||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use dioxus_core::prelude::VNode;
|
||||
|
||||
fn main() {}
|
||||
|
||||
struct Context2<'a> {
|
||||
_p: PhantomData<&'a ()>,
|
||||
}
|
||||
|
||||
type FC2<'a, 'b, 'c: 'a + 'b, P> = fn(Context2<'a>, &'b P) -> VNode<'c>;
|
||||
|
||||
static Example: FC2<()> = |ctx, props| {
|
||||
//
|
||||
todo!()
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::{marker::PhantomData, ops::Deref};
|
||||
use std::{borrow::Borrow, marker::PhantomData, ops::Deref};
|
||||
|
||||
use builder::{button, div};
|
||||
use dioxus_core::prelude::*;
|
||||
@@ -7,34 +7,27 @@ fn main() {}
|
||||
struct SomeContext {
|
||||
items: Vec<String>,
|
||||
}
|
||||
/*
|
||||
desired behavior:
|
||||
|
||||
free to move the context guard around
|
||||
not free to move contents of context guard into closure
|
||||
struct Props {
|
||||
name: String,
|
||||
}
|
||||
|
||||
rules:
|
||||
can deref in a function
|
||||
cannot drag the refs into the closure w
|
||||
*/
|
||||
#[allow(unused)]
|
||||
static Example: FC<Props> = |ctx, props| {
|
||||
let value = ctx.use_context(|c: &SomeContext| c.items.last().unwrap());
|
||||
|
||||
static Example: FC<()> = |ctx| {
|
||||
// let value = use_context(&ctx, |ctx: &SomeContext| ctx.items.last().unwrap());
|
||||
|
||||
// let b = *value;
|
||||
// let v2 = *value;
|
||||
let cb = move |e| {
|
||||
// let g = b.as_str();
|
||||
// let g = (v2).as_str();
|
||||
// let g = (value).as_str();
|
||||
// let g = b.as_str();
|
||||
};
|
||||
// let r = *value;
|
||||
// let r2 = *r;
|
||||
|
||||
ctx.view(|bump| {
|
||||
ctx.view(move |bump| {
|
||||
button(bump)
|
||||
.listeners([builder::on(bump, "click", cb)])
|
||||
.on("click", move |_| {
|
||||
// //
|
||||
println!("Value is {}", props.name);
|
||||
println!("Value is {}", value.as_str());
|
||||
println!("Value is {}", *value);
|
||||
})
|
||||
//
|
||||
.on("click", move |_| {
|
||||
println!("Value is {}", props.name);
|
||||
})
|
||||
.finish()
|
||||
})
|
||||
// ctx.view(html! {
|
||||
@@ -48,24 +41,3 @@ static Example: FC<()> = |ctx| {
|
||||
// </div>
|
||||
// })
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct ContextGuard<T> {
|
||||
val: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<'a, T> Deref for ContextGuard<T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
fn use_context<'scope, 'dope, 'a, P: Properties, I, O: 'a>(
|
||||
ctx: &'scope Context<P>,
|
||||
s: fn(&'a I) -> O,
|
||||
) -> &'scope ContextGuard<O> {
|
||||
// ) -> &'scope ContextGuard<O> {
|
||||
todo!()
|
||||
}
|
||||
@@ -1,9 +1,101 @@
|
||||
#![allow(unused, non_upper_case_globals)]
|
||||
use bumpalo::Bump;
|
||||
use dioxus_core::nodebuilder::*;
|
||||
use dioxus_core::prelude::VNode;
|
||||
use dioxus_core::prelude::*;
|
||||
use once_cell::sync::{Lazy, OnceCell};
|
||||
// #![allow(unused, non_upper_case_globals)]
|
||||
// use bumpalo::Bump;
|
||||
// use dioxus_core::nodebuilder::*;
|
||||
// use dioxus_core::prelude::VNode;
|
||||
// use dioxus_core::prelude::*;
|
||||
// use once_cell::sync::{Lazy, OnceCell};
|
||||
use std::{collections::HashMap, future::Future, marker::PhantomData};
|
||||
|
||||
use std::ops::Deref;
|
||||
|
||||
/*
|
||||
A guard over underlying T that provides access in callbacks via "Copy"
|
||||
*/
|
||||
|
||||
// #[derive(Clone)]
|
||||
struct ContextGuard2<T> {
|
||||
_val: std::marker::PhantomData<T>,
|
||||
}
|
||||
impl<T> Clone for ContextGuard2<T> {
|
||||
// we aren't cloning the underlying data so clone isn't necessary
|
||||
fn clone(&self) -> Self {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
impl<T> Copy for ContextGuard2<T> {}
|
||||
|
||||
impl<T> ContextGuard2<T> {
|
||||
fn get<'a>(&'a self) -> ContextLock<'a, T> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
struct ContextLock<'a, T> {
|
||||
_val: std::marker::PhantomData<&'a T>,
|
||||
}
|
||||
impl<'a, T: 'a + 'static> Deref for ContextLock<'a, T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref<'b>(&'b self) -> &'b T {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
The source of the data that gives out context guards
|
||||
*/
|
||||
struct Context<'a> {
|
||||
_p: std::marker::PhantomData<&'a ()>,
|
||||
}
|
||||
|
||||
impl<'a> Context<'a> {
|
||||
fn use_context<'b, I, O: 'b>(&self, f: fn(&'b I) -> O) -> ContextGuard2<O> {
|
||||
todo!()
|
||||
}
|
||||
fn add_listener(&self, f: impl Fn(()) + 'static) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn view(self, f: impl FnOnce(&'a String) + 'a) {}
|
||||
// fn view(self, f: impl for<'b> FnOnce(&'a String) + 'a) {}
|
||||
// fn view(self, f: impl for<'b> FnOnce(&'b String) + 'a) {}
|
||||
}
|
||||
|
||||
struct Example {
|
||||
value: String,
|
||||
}
|
||||
/*
|
||||
Example compiling
|
||||
*/
|
||||
fn t<'a>(ctx: Context<'a>) {
|
||||
let value = ctx.use_context(|b: &Example| &b.value);
|
||||
|
||||
// Works properly, value is moved by copy into the closure
|
||||
let refed = value.get();
|
||||
println!("Value is {}", refed.as_str());
|
||||
let r2 = refed.as_str();
|
||||
|
||||
ctx.add_listener(move |_| {
|
||||
// let val = value.get().as_str();
|
||||
let val2 = r2.as_bytes();
|
||||
});
|
||||
|
||||
// let refed = value.deref();
|
||||
// returns &String
|
||||
|
||||
// returns &String
|
||||
// let refed = value.deref(); // returns &String
|
||||
// let refed = value.deref(); // returns &String
|
||||
|
||||
// Why does this work? This closure should be static but is holding a reference to refed
|
||||
// The context guard is meant to prevent any references moving into the closure
|
||||
// if the references move they might become invalid due to mutlithreading issues
|
||||
ctx.add_listener(move |_| {
|
||||
// let val = value.as_str();
|
||||
// let val2 = refed.as_bytes();
|
||||
});
|
||||
|
||||
ctx.view(move |b| {});
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -4,19 +4,28 @@ use dioxus_core::prelude::*;
|
||||
|
||||
fn main() {}
|
||||
|
||||
static Example: FC<()> = |ctx| {
|
||||
let (val1, set_val1) = use_state(&ctx, || "b1");
|
||||
static Example: FC<()> = |ctx, props| {
|
||||
todo!()
|
||||
// let (val1, set_val1) = use_state(&ctx, || "b1");
|
||||
|
||||
ctx.view(html! {
|
||||
<div>
|
||||
<button onclick={move |_| set_val1("b1")}> "Set value to b1" </button>
|
||||
<button onclick={move |_| set_val1("b2")}> "Set value to b2" </button>
|
||||
<button onclick={move |_| set_val1("b3")}> "Set value to b3" </button>
|
||||
<div>
|
||||
<p> "Value is: {val1}" </p>
|
||||
</div>
|
||||
</div>
|
||||
})
|
||||
// ctx.view(|bump| {
|
||||
// builder::button(bump)
|
||||
// .on("click", move |c| {
|
||||
// //
|
||||
// println!("Value is {}", val1);
|
||||
// })
|
||||
// .finish()
|
||||
// })
|
||||
// ctx.view(html! {
|
||||
// <div>
|
||||
// <button onclick={move |_| set_val1("b1")}> "Set value to b1" </button>
|
||||
// <button onclick={move |_| set_val1("b2")}> "Set value to b2" </button>
|
||||
// <button onclick={move |_| set_val1("b3")}> "Set value to b3" </button>
|
||||
// <div>
|
||||
// <p> "Value is: {val1}" </p>
|
||||
// </div>
|
||||
// </div>
|
||||
// })
|
||||
};
|
||||
|
||||
use use_state_def::use_state;
|
||||
@@ -52,8 +61,8 @@ mod use_state_def {
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub fn use_state<'b, 'a, P: Properties + 'static, T: 'static, F: FnOnce() -> T + 'static>(
|
||||
ctx: &'b Context<'a, P>,
|
||||
pub fn use_state<'b, 'a, T: 'static, F: FnOnce() -> T + 'static>(
|
||||
ctx: &'b Context<'a>,
|
||||
initial_state_fn: F,
|
||||
) -> (&'a T, &'a impl Fn(T)) {
|
||||
ctx.use_hook(
|
||||
@@ -116,8 +125,8 @@ mod use_ref_def {
|
||||
/// To read the value, borrow the ref.
|
||||
/// To change it, use modify.
|
||||
/// Modifications to this value do not cause updates to the component
|
||||
pub fn use_ref<'a, P, T: 'static>(
|
||||
ctx: &'a Context<'a, P>,
|
||||
pub fn use_ref<'a, T: 'static>(
|
||||
ctx: &'a Context<'a>,
|
||||
initial_state_fn: impl FnOnce() -> T + 'static,
|
||||
) -> &'a UseRef<T> {
|
||||
ctx.use_hook(|| UseRef::new(initial_state_fn()), |state| &*state, |_| {})
|
||||
|
||||
@@ -25,7 +25,7 @@ impl Properties for Props {
|
||||
}
|
||||
}
|
||||
|
||||
static Example: FC<Props> = |ctx| {
|
||||
static Example: FC<Props> = |ctx, props| {
|
||||
ctx.view(html! {
|
||||
<div>
|
||||
<h1> </h1>
|
||||
|
||||
Reference in new issue
Block a user