-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
54 additions
and
684 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,64 @@ | ||
//! Concoct is a framework for user-interfaces in Rust. | ||
//! | ||
//! This crate provides a virtual DOM and state management system for any backend. | ||
//! Concoct uses static typing to describe your UI at compile-time to create an efficient | ||
//! tree without allocations. | ||
//! | ||
//! ```ignore | ||
//! use concoct::{Scope, View}; | ||
//! use concoct_web::html; | ||
//! | ||
//! #[derive(Default)] | ||
//! struct Counter { | ||
//! count: i32, | ||
//! } | ||
//! | ||
//! impl View<Self> for Counter { | ||
//! fn body(&mut self, _cx: &Scope<Self>) -> impl View<Self> { | ||
//! ( | ||
//! format!("High five count: {}", self.count), | ||
//! html::button("Up high!").on_click(|_cx, state: &mut Self, _event| state.count += 1), | ||
//! html::button("Down low!").on_click(|_cx, state: &mut Self, _event| state.count -= 1), | ||
//! ) | ||
//! } | ||
//! } | ||
//! | ||
//! fn main() { | ||
//! concoct_web::launch(Counter::default()) | ||
//! } | ||
//! ``` | ||
//! | ||
use std::{ops::Deref, rc::Rc}; | ||
|
||
#![deny(missing_docs)] | ||
pub struct Updater<M> { | ||
send: Rc<dyn Fn(Option<M>)>, | ||
} | ||
|
||
use std::ops::DerefMut; | ||
impl<M> Updater<M> { | ||
pub fn new(send: impl Fn(Option<M>) + 'static) -> Self { | ||
let f: Rc<dyn Fn(Option<M>)> = Rc::new(send); | ||
f.into() | ||
} | ||
|
||
mod action; | ||
pub use self::action::{Action, IntoAction}; | ||
pub fn send(&self, message: M) { | ||
(self.send)(Some(message)) | ||
} | ||
|
||
mod handle; | ||
pub use self::handle::Handle; | ||
pub fn rebuild(&self) { | ||
(self.send)(None) | ||
} | ||
} | ||
|
||
pub mod hook; | ||
impl<M> From<Rc<dyn Fn(Option<M>)>> for Updater<M> { | ||
fn from(value: Rc<dyn Fn(Option<M>)>) -> Self { | ||
Self { send: value } | ||
} | ||
} | ||
|
||
impl<M> Clone for Updater<M> { | ||
fn clone(&self) -> Self { | ||
Self { | ||
send: self.send.clone(), | ||
} | ||
} | ||
} | ||
|
||
mod vdom; | ||
pub use self::vdom::VirtualDom; | ||
pub struct Context<'a, M> { | ||
updater: &'a Updater<M>, | ||
} | ||
|
||
pub mod view; | ||
pub use self::view::View; | ||
impl<'a, M> Context<'a, M> { | ||
pub fn new(updater: &'a Updater<M>) -> Self { | ||
Self { updater } | ||
} | ||
|
||
mod scope; | ||
pub use self::scope::Scope; | ||
pub fn updater(&self) -> &Updater<M> { | ||
&self.updater | ||
} | ||
} | ||
|
||
/// Run a view on a new virtual dom. | ||
pub async fn run<T, V>(content: V) | ||
where | ||
T: 'static, | ||
V: View<T> + DerefMut<Target = T>, | ||
{ | ||
let mut vdom = VirtualDom::new(content); | ||
vdom.build(); | ||
impl<M> Deref for Context<'_, M> { | ||
type Target = Updater<M>; | ||
|
||
loop { | ||
vdom.rebuild().await; | ||
fn deref(&self) -> &Self::Target { | ||
self.updater | ||
} | ||
} | ||
|
||
pub trait View<M> { | ||
type State; | ||
|
||
fn build(self) -> Self::State; | ||
|
||
fn rebuild(self, state: &mut Self::State); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.