Docs(TODO) https://docs.rs/respo
Reimagine Respo.cljs in Rust.
A preview example:
```rust
pub struct Store {
pub counted: i32,
pub tasks: Vec
pub struct Task { pub id: String, pub done: bool, pub content: String, pub time: f32, }
pub enum ActionOp {
Increment,
Decrement,
StatesChange(Vec
pub fn applyaction(store: &mut Store, op: ActionOp) -> Result<(), String> { match op { ActionOp::Increment => { store.counted += 1; } ActionOp::Decrement => { store.counted -= 1; } ActionOp::StatesChange(path, newstate) => { store.states = store.states.setin(&path, newstate); } ActionOp::AddTask(id, content) => store.tasks.push(Task { id, content, time: 0.0, done: false, }), ActionOp::RemoveTask(id) => { store.tasks.retain(|task| task.id != id); } ActionOp::UpdateTask(id, content) => { let mut found = false; for task in &mut store.tasks { if task.id == id { task.content = content.to_owned(); found = true; } } if !found { return Err(format!("task {} not found", id)); } } ActionOp::ToggleTask(id) => { let mut found = false; for task in &mut store.tasks { if task.id == id { util::log!("change task {:?}", task); task.done = !task.done; found = true; } } if !found { return Err(format!("task {} not found", id)); } } } Ok(()) } ```
```rust
pub fn loaddemoapp() -> JsValue { panic::sethook(Box::new(consoleerrorpanichook::hook));
let mounttarget = queryselect_node(".app").expect("found mount target");
// need to push store inside function to keep all in one thread let global_store = Rc::new(RefCell::new(Store { counted: 0, states: StatesTree::default(), tasks: vec![], }));
let storetoaction = globalstore.clone(); let dispatchaction = move |op: ActionOp| -> Result<(), String> { // util::log!("action {:?} store, {:?}", op, storetoaction.borrow()); let mut store = storetoaction.borrowmut(); applyaction(&mut store, op)?;
// util::log!("store after action {:?}", store);
Ok(())
};
rendernode(
mounttarget,
Box::new(move || -> Result
// util::log!("global store: {:?}", store);
Ok(
div()
.class(ui_global())
.add_style(RespoStyle::default().padding(12.0).to_owned())
.add_children([
comp_counter(&states.pick("counter"), store.counted),
comp_panel(&states.pick("panel"))?,
comp_todolist(&states.pick("todolist"), &store.tasks)?,
])
.to_owned(),
)
}),
DispatchFn::new(dispatch_action),
) .expect("rendering node");
JsValue::NULL } ```
Apache License 2.0 .