wip: more examples
This commit is contained in:
1 parent
11f89e5d33
commit
891e366cd8
1 file changed
-77
@@ -162,80 +162,3 @@ fn CalculatorKey<'a, 'r>(cx: Context<'a, CalculatorKeyProps<'r>>) -> VNode<'a> {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
use odl::use_model;
|
||||
mod odl {
|
||||
use super::*;
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
ops::{Deref, DerefMut},
|
||||
};
|
||||
/// Use model makes it easy to use "models" as state for components. To modify the model, call "modify" and a clone of the
|
||||
/// current model will be made, with a RefMut lock on it. Dioxus will never run your components multithreaded, so you can
|
||||
/// be relatively sure that this won't fail in practice
|
||||
pub fn use_model<'a, T: Clone>(cx: &impl Scoped<'a>, f: impl FnOnce() -> T) -> &'a UseModel<T> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
trait ModelAble {
|
||||
type Out;
|
||||
}
|
||||
fn make_model<F: ModelAble>(f: F) -> F::Out {
|
||||
todo!()
|
||||
}
|
||||
|
||||
// fn use_model(w: &mut ModelWrapper<MyModel>) {
|
||||
// let mut g1 = move || {
|
||||
// //
|
||||
// w.eat();
|
||||
// };
|
||||
// let mut g2 = move || {
|
||||
// //
|
||||
// w.eat();
|
||||
// };
|
||||
// g1();
|
||||
// g2();
|
||||
// }
|
||||
|
||||
pub struct UseModel<T: Clone> {
|
||||
real: T,
|
||||
wip: RefCell<T>,
|
||||
}
|
||||
|
||||
use std::cell::{Ref, RefMut};
|
||||
impl<T: Clone> Deref for UseModel<T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.real
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone> UseModel<T> {
|
||||
pub fn new(t: T) -> Self {
|
||||
Self {
|
||||
real: t.clone(),
|
||||
wip: RefCell::new(t),
|
||||
}
|
||||
}
|
||||
pub fn get_mut(&self) -> RefMut<'_, T> {
|
||||
self.wip.borrow_mut()
|
||||
}
|
||||
pub fn modify(&self, f: impl FnOnce(&mut T)) {
|
||||
let mut g = self.get_mut();
|
||||
let r = g.deref_mut();
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct MyModel {
|
||||
hunger: u32,
|
||||
}
|
||||
|
||||
impl MyModel {
|
||||
fn eat(&mut self) {
|
||||
self.hunger -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user