Skip to content

Added more docs (and split dom element into few files) #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
### Fixed

* `LazyCache::force_update` really forces the update even if value not expired
* `JsJson` and `JsValue`` list size as u32 - fixes large DOM updates
* `JsJson` and `JsValue` list size as u32 - fixes large DOM updates

## 0.3.2 - 2023-07-17

Expand Down
4 changes: 2 additions & 2 deletions crates/vertigo-cli/src/serve/serve_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ pub struct ServeOpts {
#[arg(long, default_value_t = {4444})]
pub port: u16,

/// sets up proxy: --proxy /path=http://domain.com/path
/// sets up proxy: `--proxy /path=http://domain.com/path`
#[arg(long, value_parser = parse_key_val::<String, String>)]
pub proxy: Vec<(String, String)>,

/// Setting the parameters --env api=http://domain.com/api --env api2=http://domain.com/api2
/// Setting the parameters `--env api=http://domain.com/api --env api2=http://domain.com/api2`
#[arg(long, value_parser = parse_key_val::<String, String>)]
pub env: Vec<(String, String)>,
}
Expand Down
4 changes: 2 additions & 2 deletions crates/vertigo-cli/src/watch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ pub struct WatchOpts {
#[arg(short, long)]
pub disable_wasm_opt: bool,

/// sets up proxy: --proxy /path=http://domain.com/path
/// sets up proxy: `--proxy /path=http://domain.com/path`
#[arg(long, value_parser = parse_key_val::<String, String>)]
pub proxy: Vec<(String, String)>,

/// Setting the parameters --env api=http://domain.com/api --env api2=http://domain.com/api2
/// Setting the parameters `--env api=http://domain.com/api --env api2=http://domain.com/api2`
#[arg(long, value_parser = parse_key_val::<String, String>)]
pub env: Vec<(String, String)>,
}
Expand Down
70 changes: 70 additions & 0 deletions crates/vertigo/src/computed/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
rc::Rc,
};
use std::hash::Hash;

use crate::DomNode;
use crate::{
computed::{Computed, ToComputed, Dependencies, GraphId}, struct_mut::ValueMut, DropResource,
Expand Down Expand Up @@ -181,10 +182,56 @@ impl<T: Clone + PartialEq + 'static> Value<T> {
}

impl<T: Clone + PartialEq + 'static> Value<T> {
/// Render value (reactively transforms `T` into `DomNode`)
///
/// See [computed_tuple](macro.computed_tuple.html) if you want to render multiple values in a handy way.
///
/// ```rust
/// use vertigo::{dom, Value};
///
/// let my_value = Value::new(5);
///
/// let element = my_value.render_value(|bare_value| dom! { <div>{bare_value}</div> });
///
/// dom! {
/// <div>
/// {element}
/// </div>
/// };
/// ```
///
pub fn render_value(&self, render: impl Fn(T) -> DomNode + 'static) -> DomNode {
self.to_computed().render_value(render)
}

/// Render optional value (reactively transforms `Option<T>` into `Option<DomNode>`)
///
/// See [computed_tuple](macro.computed_tuple.html) if you want to render multiple values in a handy way.
///
/// ```rust
/// use vertigo::{dom, Value};
///
/// let value1 = Value::new(Some(5));
/// let value2 = Value::new(None::<i32>);
///
/// let element1 = value1.render_value_option(|bare_value|
/// bare_value.map(|value| dom! { <div>{value}</div> })
/// );
/// let element2 = value2.render_value_option(|bare_value|
/// match bare_value {
/// Some(value) => Some(dom! { <div>{value}</div> }),
/// None => Some(dom! { <div>"default"</div> }),
/// }
/// );
///
/// dom! {
/// <div>
/// {element1}
/// {element2}
/// </div>
/// };
/// ```
///
pub fn render_value_option(&self, render: impl Fn(T) -> Option<DomNode> + 'static) -> DomNode {
self.to_computed().render_value_option(render)
}
Expand All @@ -194,6 +241,29 @@ impl<
T: PartialEq + Clone + 'static,
L: IntoIterator<Item=T> + Clone + PartialEq + 'static
> Value<L> {
/// Render iterable value (reactively transforms `Iterator<T>` into Node with list of rendered elements )
///
/// ```rust
/// use vertigo::{dom, Value};
///
/// let my_list = Value::new(vec![
/// (1, "one"),
/// (2, "two"),
/// (3, "three"),
/// ]);
///
/// let elements = my_list.render_list(
/// |el| el.0,
/// |el| dom! { <div>{el.1}</div> }
/// );
///
/// dom! {
/// <div>
/// {elements}
/// </div>
/// };
/// ```
///
pub fn render_list<
K: Eq + Hash,
>(
Expand Down
109 changes: 109 additions & 0 deletions crates/vertigo/src/dom/callback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use std::rc::Rc;

use vertigo_macro::bind;

use crate::{Computed, struct_mut::ValueMut, DropResource};

pub enum Callback<R> {
Basic(Rc<dyn Fn() -> R + 'static>),
Computed(Computed<Rc<dyn Fn() -> R + 'static>>),
}

impl<R> From<Rc<dyn Fn() -> R + 'static>> for Callback<R> {
fn from(value: Rc<dyn Fn() -> R + 'static>) -> Self {
Callback::Basic(value)
}
}

impl<R, F: Fn() -> R + 'static> From<F> for Callback<R> {
fn from(value: F) -> Self {
Callback::Basic(Rc::new(value))
}
}

impl<R> From<Computed<Rc<dyn Fn() -> R + 'static>>> for Callback<R> {
fn from(value: Computed<Rc<dyn Fn() -> R + 'static>>) -> Self {
Callback::Computed(value)
}
}

impl<R: 'static> Callback<R> {
pub fn subscribe(self) -> (Rc<dyn Fn() -> R + 'static>, Option<DropResource>) {
match self {
Self::Basic(func) => (func, None),
Self::Computed(computed) => {

let current = Rc::new(ValueMut::new(None));

let drop = computed.subscribe_all(bind!(current, |new_fn| {
current.set(Some(new_fn));
}));

let callback = Rc::new(move || -> R {
let callback = current.get();

let Some(callback) = callback else {
unreachable!();
};

callback()
});

(callback, Some(drop))
}
}
}
}

pub enum Callback1<T, R> {
Basic(Rc<dyn Fn(T) -> R + 'static>),
Rc(Rc<dyn Fn(T) -> R + 'static>),
Computed(Computed<Rc<dyn Fn(T) -> R + 'static>>),
}

impl<T, R, F: Fn(T) -> R + 'static> From<F> for Callback1<T, R> {
fn from(value: F) -> Self {
Callback1::Basic(Rc::new(value))
}
}

impl<T, R> From<Rc<dyn Fn(T) -> R + 'static>> for Callback1<T, R> {
fn from(value: Rc<dyn Fn(T) -> R + 'static>) -> Self {
Callback1::Rc(value)
}
}

impl<T, R> From<Computed<Rc<dyn Fn(T) -> R + 'static>>> for Callback1<T, R> {
fn from(value: Computed<Rc<dyn Fn(T) -> R + 'static>>) -> Self {
Callback1::Computed(value)
}
}

impl<T: 'static, R: 'static> Callback1<T, R> {
pub fn subscribe(self) -> (Rc<dyn Fn(T) -> R + 'static>, Option<DropResource>) {
match self {
Self::Basic(func) => (func, None),
Self::Rc(func) => (func, None),
Self::Computed(computed) => {

let current = Rc::new(ValueMut::new(None));

let drop = computed.subscribe_all(bind!(current, |new_fn| {
current.set(Some(new_fn));
}));

let callback = Rc::new(move |param: T| -> R {
let callback = current.get();

let Some(callback) = callback else {
unreachable!();
};

callback(param)
});

(callback, Some(drop))
}
}
}
}
145 changes: 9 additions & 136 deletions crates/vertigo/src/dom/dom_element.rs
Original file line number Diff line number Diff line change
@@ -1,148 +1,21 @@
use std::rc::Rc;

use vertigo_macro::bind;

use crate::{
driver_module::{driver::Driver, api::DomAccess, StaticString},
driver_module::{driver::Driver, StaticString},
dom::{dom_node::DomNode, dom_id::DomId},
get_driver, Computed, struct_mut::{VecMut, ValueMut}, ApiImport, DropResource, DropFileItem,
get_driver, Computed, struct_mut::VecMut, DropResource, DropFileItem,
JsValue, DomText, Css,
};

use crate::struct_mut::VecDequeMut;

use super::attr_value::{AttrValue, CssAttrValue};
use super::types::{KeyDownEvent, DropFileEvent};
use super::dom_element_class::DomElementClassMerge;

#[derive(Clone)]
pub struct DomElementRef {
api: ApiImport,
id: DomId,
}

impl DomElementRef {
pub fn new(api: ApiImport, id: DomId) -> DomElementRef {
DomElementRef {
api,
id,
}
}

pub fn dom_access(&self) -> DomAccess {
self.api.dom_access().element(self.id)
}
}

impl PartialEq for DomElementRef {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}

pub enum Callback<R> {
Basic(Rc<dyn Fn() -> R + 'static>),
Computed(Computed<Rc<dyn Fn() -> R + 'static>>),
}

impl<R> From<Rc<dyn Fn() -> R + 'static>> for Callback<R> {
fn from(value: Rc<dyn Fn() -> R + 'static>) -> Self {
Callback::Basic(value)
}
}

impl<R, F: Fn() -> R + 'static> From<F> for Callback<R> {
fn from(value: F) -> Self {
Callback::Basic(Rc::new(value))
}
}

impl<R> From<Computed<Rc<dyn Fn() -> R + 'static>>> for Callback<R> {
fn from(value: Computed<Rc<dyn Fn() -> R + 'static>>) -> Self {
Callback::Computed(value)
}
}

impl<R: 'static> Callback<R> {
pub fn subscribe(self) -> (Rc<dyn Fn() -> R + 'static>, Option<DropResource>) {
match self {
Self::Basic(func) => (func, None),
Self::Computed(computed) => {

let current = Rc::new(ValueMut::new(None));

let drop = computed.subscribe_all(bind!(current, |new_fn| {
current.set(Some(new_fn));
}));

let callback = Rc::new(move || -> R {
let callback = current.get();

let Some(callback) = callback else {
unreachable!();
};

callback()
});

(callback, Some(drop))
}
}
}
}

pub enum Callback1<T, R> {
Basic(Rc<dyn Fn(T) -> R + 'static>),
Rc(Rc<dyn Fn(T) -> R + 'static>),
Computed(Computed<Rc<dyn Fn(T) -> R + 'static>>),
}

impl<T, R, F: Fn(T) -> R + 'static> From<F> for Callback1<T, R> {
fn from(value: F) -> Self {
Callback1::Basic(Rc::new(value))
}
}

impl<T, R> From<Rc<dyn Fn(T) -> R + 'static>> for Callback1<T, R> {
fn from(value: Rc<dyn Fn(T) -> R + 'static>) -> Self {
Callback1::Rc(value)
}
}

impl<T, R> From<Computed<Rc<dyn Fn(T) -> R + 'static>>> for Callback1<T, R> {
fn from(value: Computed<Rc<dyn Fn(T) -> R + 'static>>) -> Self {
Callback1::Computed(value)
}
}

impl<T: 'static, R: 'static> Callback1<T, R> {
pub fn subscribe(self) -> (Rc<dyn Fn(T) -> R + 'static>, Option<DropResource>) {
match self {
Self::Basic(func) => (func, None),
Self::Rc(func) => (func, None),
Self::Computed(computed) => {

let current = Rc::new(ValueMut::new(None));

let drop = computed.subscribe_all(bind!(current, |new_fn| {
current.set(Some(new_fn));
}));

let callback = Rc::new(move |param: T| -> R {
let callback = current.get();

let Some(callback) = callback else {
unreachable!();
};

callback(param)
});

(callback, Some(drop))
}
}
}
}
use super::{
attr_value::{AttrValue, CssAttrValue},
callback::{Callback, Callback1},
types::{KeyDownEvent, DropFileEvent},
dom_element_class::DomElementClassMerge,
dom_element_ref::DomElementRef,
};

/// A Real DOM representative - element kind
pub struct DomElement {
Expand Down
Loading