examples: upgrade to new version of dioxus core.
also add the inline_props macro
This commit is contained in:
1 parent
21e00c114e
commit
cda759c659
41 files changed
+556
-253
No files matched your search
@@ -5,44 +5,71 @@ use std::{
|
||||
pin::Pin,
|
||||
rc::Rc,
|
||||
};
|
||||
/*
|
||||
|
||||
pub fn use_coroutine<'a, F: Future<Output = ()> + 'static>(
|
||||
|
||||
|
||||
let g = use_coroutine(&cx, || {
|
||||
// clone the items in
|
||||
async move {
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
|
||||
*/
|
||||
pub fn use_coroutine<'a, F>(
|
||||
cx: &'a ScopeState,
|
||||
mut f: impl FnMut() -> F + 'a,
|
||||
) -> CoroutineHandle<'a> {
|
||||
create_future: impl FnOnce() -> F,
|
||||
) -> CoroutineHandle<'a>
|
||||
where
|
||||
F: Future<Output = ()>,
|
||||
F: 'static,
|
||||
{
|
||||
cx.use_hook(
|
||||
move |_| State {
|
||||
running: Default::default(),
|
||||
fut: Default::default(),
|
||||
submit: Default::default(),
|
||||
pending_fut: Default::default(),
|
||||
running_fut: Default::default(),
|
||||
},
|
||||
|state| {
|
||||
let fut_slot = state.fut.clone();
|
||||
let running = state.running.clone();
|
||||
let submit: Box<dyn FnOnce() + 'a> = Box::new(move || {
|
||||
let g = async move {
|
||||
running.set(true);
|
||||
f().await;
|
||||
running.set(false);
|
||||
};
|
||||
let p: Pin<Box<dyn Future<Output = ()>>> = Box::pin(g);
|
||||
fut_slot
|
||||
.borrow_mut()
|
||||
.replace(unsafe { std::mem::transmute(p) });
|
||||
});
|
||||
let f = create_future();
|
||||
state.pending_fut.set(Some(Box::pin(f)));
|
||||
|
||||
let submit = unsafe { std::mem::transmute(submit) };
|
||||
state.submit.get_mut().replace(submit);
|
||||
|
||||
if state.running.get() {
|
||||
let mut fut = state.fut.borrow_mut();
|
||||
cx.push_task(|| fut.as_mut().unwrap().as_mut());
|
||||
} else {
|
||||
// make sure to drop the old future
|
||||
if let Some(fut) = state.fut.borrow_mut().take() {
|
||||
drop(fut);
|
||||
}
|
||||
if let Some(fut) = state.running_fut.as_mut() {
|
||||
cx.push_task(fut);
|
||||
}
|
||||
|
||||
// if let Some(fut) = state.running_fut.take() {
|
||||
// state.running.set(true);
|
||||
// fut.resume();
|
||||
// }
|
||||
|
||||
// let submit: Box<dyn FnOnce() + 'a> = Box::new(move || {
|
||||
// let g = async move {
|
||||
// running.set(true);
|
||||
// create_future().await;
|
||||
// running.set(false);
|
||||
// };
|
||||
// let p: Pin<Box<dyn Future<Output = ()>>> = Box::pin(g);
|
||||
// fut_slot
|
||||
// .borrow_mut()
|
||||
// .replace(unsafe { std::mem::transmute(p) });
|
||||
// });
|
||||
|
||||
// let submit = unsafe { std::mem::transmute(submit) };
|
||||
// state.submit.get_mut().replace(submit);
|
||||
|
||||
// if state.running.get() {
|
||||
// // let mut fut = state.fut.borrow_mut();
|
||||
// // cx.push_task(|| fut.as_mut().unwrap().as_mut());
|
||||
// } else {
|
||||
// // make sure to drop the old future
|
||||
// if let Some(fut) = state.fut.borrow_mut().take() {
|
||||
// drop(fut);
|
||||
// }
|
||||
// }
|
||||
CoroutineHandle { cx, inner: state }
|
||||
},
|
||||
)
|
||||
@@ -50,14 +77,20 @@ pub fn use_coroutine<'a, F: Future<Output = ()> + 'static>(
|
||||
|
||||
struct State {
|
||||
running: Rc<Cell<bool>>,
|
||||
submit: RefCell<Option<Box<dyn FnOnce()>>>,
|
||||
fut: Rc<RefCell<Option<Pin<Box<dyn Future<Output = ()>>>>>>,
|
||||
|
||||
// the way this is structure, you can toggle the coroutine without re-rendering the comppnent
|
||||
// this means every render *generates* the future, which is a bit of a waste
|
||||
// todo: allocate pending futures in the bump allocator and then have a true promotion
|
||||
pending_fut: Cell<Option<Pin<Box<dyn Future<Output = ()> + 'static>>>>,
|
||||
running_fut: Option<Pin<Box<dyn Future<Output = ()> + 'static>>>,
|
||||
// running_fut: Rc<RefCell<Option<Pin<Box<dyn Future<Output = ()> + 'static>>>>>,
|
||||
}
|
||||
|
||||
pub struct CoroutineHandle<'a> {
|
||||
cx: &'a ScopeState,
|
||||
inner: &'a State,
|
||||
}
|
||||
|
||||
impl Clone for CoroutineHandle<'_> {
|
||||
fn clone(&self) -> Self {
|
||||
CoroutineHandle {
|
||||
@@ -73,10 +106,11 @@ impl<'a> CoroutineHandle<'a> {
|
||||
if self.is_running() {
|
||||
return;
|
||||
}
|
||||
if let Some(submit) = self.inner.submit.borrow_mut().take() {
|
||||
submit();
|
||||
let mut fut = self.inner.fut.borrow_mut();
|
||||
self.cx.push_task(|| fut.as_mut().unwrap().as_mut());
|
||||
|
||||
if let Some(submit) = self.inner.pending_fut.take() {
|
||||
// submit();
|
||||
// let inner = self.inner;
|
||||
// self.cx.push_task(submit());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,5 +118,11 @@ impl<'a> CoroutineHandle<'a> {
|
||||
self.inner.running.get()
|
||||
}
|
||||
|
||||
pub fn resume(&self) {}
|
||||
pub fn resume(&self) {
|
||||
// self.cx.push_task(fut)
|
||||
}
|
||||
|
||||
pub fn stop(&self) {}
|
||||
|
||||
pub fn restart(&self) {}
|
||||
}
|
||||
@@ -89,9 +89,9 @@ pub fn use_model_coroutine<'a, T, F: Future<Output = ()> + 'static>(
|
||||
}
|
||||
},
|
||||
|inner| {
|
||||
if let Some(task) = inner.task.get_mut() {
|
||||
cx.push_task(|| task);
|
||||
}
|
||||
// if let Some(task) = inner.task.get_mut() {
|
||||
// cx.push_task(|| task);
|
||||
// }
|
||||
//
|
||||
todo!()
|
||||
},
|
||||
|
||||
@@ -171,7 +171,11 @@ impl<'a, T: 'static> UseState<'a, T> {
|
||||
}
|
||||
|
||||
impl<'a, T: 'static + ToOwned<Owned = T>> UseState<'a, T> {
|
||||
/// Gain mutable access to the new value. This method is only available when the value is a `ToOwned` type.
|
||||
/// Gain mutable access to the new value via RefMut.
|
||||
///
|
||||
/// If `modify` is called, then the component will re-render.
|
||||
///
|
||||
/// This method is only available when the value is a `ToOwned` type.
|
||||
///
|
||||
/// Mutable access is derived by calling "ToOwned" (IE cloning) on the current value.
|
||||
///
|
||||
|
||||
Reference in new issue
Block a user