Feat: update solved problems with context API

This commit is contained in:
Jonathan Kelley committed 2021-02-10 12:48:02 -05:00
1 parent 24e8bcb028
commit c97a9647e3
7 files changed
+209 -9

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
use std::{marker::PhantomData, ops::Deref};
use builder::{button, div};
use dioxus_core::prelude::*;
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
rules:
can deref in a function
cannot drag the refs into the closure w
*/
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| {
button(bump)
.listeners([builder::on(bump, "click", cb)])
.finish()
})
// ctx.view(html! {
// <div>
// <button onclick={move |_| println!("Value is {}", value)} />
// <button onclick={move |_| println!("Value is {}", value)} />
// <button onclick={move |_| println!("Value is {}", value)} />
// <div>
// <p> "Value is: {val}" </p>
// </div>
// </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!()
}
+3 -2
View File
@@ -1068,9 +1068,10 @@ pub fn attr<'a>(name: &'a str, value: &'a str) -> Attribute<'a> {
/// // do something when a click happens...
/// });
/// ```
pub fn on<'a, F>(bump: &'a Bump, event: &'a str, callback: F) -> Listener<'a>
pub fn on<'a, 'b, F: 'b>(bump: &'a Bump, event: &'a str, callback: F) -> Listener<'a>
where
F: Fn(()) + 'a,
'b: 'a + 'static,
F: Fn(()) + 'b,
{
Listener {
event,
+5 -1
View File
@@ -174,7 +174,11 @@ mod velement {
}
/// An event listener.
pub struct Listener<'a> {
pub struct Listener<'a>
// pub struct Listener<'a, 'b>
// where
// 'b: 'a + 'static,
{
/// The type of event to listen for.
pub(crate) event: &'a str,
/// The callback to invoke when the event happens.