diff --git a/CHANGES.md b/CHANGES.md index 5020131e..7a9d90d6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,12 +1,13 @@ -## 0.3.2 - Unreleased +## 0.3.2 - 2023-07-17 ### Added * `computed_tuple!` macro * `on_blur`, `on_mouse_down`, `on_mouse_up` event +* `ToComputed` trait ## 0.3.1 - 2023-05-25 diff --git a/crates/vertigo-cli/src/watch/mod.rs b/crates/vertigo-cli/src/watch/mod.rs index 61116e11..503e2b0b 100644 --- a/crates/vertigo-cli/src/watch/mod.rs +++ b/crates/vertigo-cli/src/watch/mod.rs @@ -176,8 +176,9 @@ pub async fn run(mut opts: WatchOpts) -> Result<(), i32> { loop { version += 1; - let Ok(()) = tx.send(Status::Building) else { - unreachable!(); + if let Err(err) = tx.send(Status::Building) { + log::error!("Can't contact the browser: {err} (Other watch process already running?)"); + return Err(-2); }; log::info!("build run ..."); diff --git a/crates/vertigo-macro/src/bind.rs b/crates/vertigo-macro/src/bind.rs index 4de8c0b6..e90dd03d 100644 --- a/crates/vertigo-macro/src/bind.rs +++ b/crates/vertigo-macro/src/bind.rs @@ -239,7 +239,7 @@ pub fn bind_rc_fn(input: TokenStream) -> Result { let types = { let mut types_macro: Vec = Vec::new(); - + for type_item in func_params.into_iter() { let type_item = get_type(type_item)?; @@ -254,7 +254,7 @@ pub fn bind_rc_fn(input: TokenStream) -> Result { Ok(quote!{ { - let func: std::rc::Rc:: = std::rc::Rc::new(vertigo::bind!(#(#bind_params,)* #body)); + let func: std::rc::Rc:: _> = std::rc::Rc::new(vertigo::bind!(#(#bind_params,)* #body)); func } }.into()) diff --git a/crates/vertigo/src/computed/auto_map.rs b/crates/vertigo/src/computed/auto_map.rs index 7e4eda09..03390710 100644 --- a/crates/vertigo/src/computed/auto_map.rs +++ b/crates/vertigo/src/computed/auto_map.rs @@ -4,7 +4,7 @@ use std::{ rc::Rc, }; -use crate::{struct_mut::HashMapMut}; +use crate::struct_mut::HashMapMut; type CreateType = Box, &K) -> V>; diff --git a/crates/vertigo/src/computed/computed_box.rs b/crates/vertigo/src/computed/computed_box.rs index a2e11634..2609d85e 100644 --- a/crates/vertigo/src/computed/computed_box.rs +++ b/crates/vertigo/src/computed/computed_box.rs @@ -2,7 +2,7 @@ use std::{cmp::PartialEq, rc::Rc}; use crate::{ computed::{GraphValue, graph_id::GraphId}, dom_value::{render_value, render_value_option}, - dom_list::{render_list}, + dom_list::render_list, Value, DomNode, DropResource, struct_mut::ValueMut }; use std::hash::Hash; @@ -199,3 +199,19 @@ impl From<&str> for Computed { Value::new(value.to_string()).to_computed() } } + +pub trait ToComputed { + fn to_computed(&self) -> Computed; +} + +impl ToComputed for Computed { + fn to_computed(&self) -> Computed { + self.clone() + } +} + +impl ToComputed for &Computed { + fn to_computed(&self) -> Computed { + (*self).clone() + } +} diff --git a/crates/vertigo/src/computed/dependencies/graph_connections.rs b/crates/vertigo/src/computed/dependencies/graph_connections.rs index e5adee23..74ef6b09 100644 --- a/crates/vertigo/src/computed/dependencies/graph_connections.rs +++ b/crates/vertigo/src/computed/dependencies/graph_connections.rs @@ -1,4 +1,4 @@ -use std::{collections::{BTreeSet, BTreeMap}}; +use std::collections::{BTreeSet, BTreeMap}; use crate::{GraphId, struct_mut::ValueMut}; use super::graph_one_to_many::GraphOneToMany; @@ -64,7 +64,7 @@ impl GraphConnectionsInner { let mut new_list = Vec::new(); for command in command_list { - new_list.extend(self.exec_command(command).into_iter()); + new_list.extend(self.exec_command(command)); } new_list diff --git a/crates/vertigo/src/computed/dependencies/transaction_state.rs b/crates/vertigo/src/computed/dependencies/transaction_state.rs index 8734c53a..4eb2a1d8 100644 --- a/crates/vertigo/src/computed/dependencies/transaction_state.rs +++ b/crates/vertigo/src/computed/dependencies/transaction_state.rs @@ -102,7 +102,7 @@ impl TransactionState { self.state.change(move |mut state| { match &mut state { State::Modification { client_ids, .. } => { - client_ids.extend(client.into_iter()); + client_ids.extend(client); } _ => { log::error!("You can only call the trigger if you are in a transaction block"); diff --git a/crates/vertigo/src/computed/mod.rs b/crates/vertigo/src/computed/mod.rs index d9f9efd5..f21370ce 100644 --- a/crates/vertigo/src/computed/mod.rs +++ b/crates/vertigo/src/computed/mod.rs @@ -12,11 +12,11 @@ pub mod context; mod tests; pub use auto_map::AutoMap; -pub use computed_box::Computed; +pub use computed_box::{Computed, ToComputed}; pub use dependencies::Dependencies; pub use graph_id::GraphId; -pub use graph_value::{GraphValue}; -pub use value::{Value}; +pub use graph_value::GraphValue; +pub use value::Value; pub use drop_resource::DropResource; /// Allows to create `Computed` out of `Value`, `Value`, ... diff --git a/crates/vertigo/src/computed/tests/computed.rs b/crates/vertigo/src/computed/tests/computed.rs index 3b9ed171..2520387f 100644 --- a/crates/vertigo/src/computed/tests/computed.rs +++ b/crates/vertigo/src/computed/tests/computed.rs @@ -4,7 +4,7 @@ use crate::{transaction, get_driver}; use crate::computed::{Computed, Value, DropResource}; use crate::computed::tests::box_value_version::SubscribeValueVer; -use crate::struct_mut::{ValueMut}; +use crate::struct_mut::ValueMut; #[test] fn basic() { diff --git a/crates/vertigo/src/computed/value.rs b/crates/vertigo/src/computed/value.rs index deeca7f9..503f4fc3 100644 --- a/crates/vertigo/src/computed/value.rs +++ b/crates/vertigo/src/computed/value.rs @@ -5,7 +5,7 @@ use std::{ use std::hash::Hash; use crate::DomNode; use crate::{ - computed::{Computed, Dependencies, GraphId}, struct_mut::ValueMut, DropResource, + computed::{Computed, ToComputed, Dependencies, GraphId}, struct_mut::ValueMut, DropResource, get_driver, }; @@ -140,6 +140,14 @@ impl Value { }) } + pub fn id(&self) -> GraphId { + self.inner.id + } + + pub fn deps(&self) -> &'static Dependencies { + self.inner.deps + } + pub fn to_computed(&self) -> Computed { let self_clone = self.clone(); @@ -147,13 +155,17 @@ impl Value { self_clone.get(context) }) } +} - pub fn id(&self) -> GraphId { - self.inner.id +impl ToComputed for Value { + fn to_computed(&self) -> Computed { + self.to_computed() } +} - pub fn deps(&self) -> &'static Dependencies { - self.inner.deps +impl ToComputed for &Value { + fn to_computed(&self) -> Computed { + (*self).to_computed() } } diff --git a/crates/vertigo/src/dom/dom_element.rs b/crates/vertigo/src/dom/dom_element.rs index 4d311471..e33db3bb 100644 --- a/crates/vertigo/src/dom/dom_element.rs +++ b/crates/vertigo/src/dom/dom_element.rs @@ -1,4 +1,4 @@ -use std::{rc::Rc}; +use std::rc::Rc; use vertigo_macro::bind; diff --git a/crates/vertigo/src/dom/dom_text.rs b/crates/vertigo/src/dom/dom_text.rs index 19901bef..40f83828 100644 --- a/crates/vertigo/src/dom/dom_text.rs +++ b/crates/vertigo/src/dom/dom_text.rs @@ -1,37 +1,9 @@ use crate::{ + computed::ToComputed, driver_module::driver::Driver, - dom::dom_id::DomId, get_driver, DropResource, struct_mut::VecMut, Computed, Value, + dom::dom_id::DomId, get_driver, DropResource, struct_mut::VecMut, }; -pub trait ToComputed { - fn to_computed_param(&self) -> Computed; -} - -impl ToComputed for Computed { - fn to_computed_param(&self) -> Computed { - self.clone() - } -} - -impl ToComputed for &Computed { - fn to_computed_param(&self) -> Computed { - (*self).clone() - } -} - -impl ToComputed for Value { - fn to_computed_param(&self) -> Computed { - self.to_computed() - } -} - -impl ToComputed for &Value { - fn to_computed_param(&self) -> Computed { - self.to_computed() - } -} - - /// A Real DOM representative - text kind pub struct DomText { driver: Driver, @@ -59,7 +31,7 @@ impl DomText { let id_dom = text_node.id_dom; let driver = get_driver(); - let computed = computed.to_computed_param(); + let computed = computed.to_computed(); let client = computed.subscribe(move |value| { let value: String = value.into(); driver.inner.dom.update_text(id_dom, &value); diff --git a/crates/vertigo/src/dom_list.rs b/crates/vertigo/src/dom_list.rs index 5b0863f0..e2d6d13a 100644 --- a/crates/vertigo/src/dom_list.rs +++ b/crates/vertigo/src/dom_list.rs @@ -55,7 +55,7 @@ pub fn render_list< let render = render.clone(); move |new_list| { - let new_list = VecDeque::from_iter(new_list.into_iter()); + let new_list = VecDeque::from_iter(new_list); current_list.change({ let get_key = get_key.clone(); diff --git a/crates/vertigo/src/driver_module/api/api_dom_access.rs b/crates/vertigo/src/driver_module/api/api_dom_access.rs index 067a5326..9943a2e3 100644 --- a/crates/vertigo/src/driver_module/api/api_dom_access.rs +++ b/crates/vertigo/src/driver_module/api/api_dom_access.rs @@ -76,7 +76,7 @@ impl DomAccess { JsValue::str(name), ); - value_params.extend(params.into_iter()); + value_params.extend(params); self.builder.push(JsValue::List(value_params)); self diff --git a/crates/vertigo/src/driver_module/dom_command.rs b/crates/vertigo/src/driver_module/dom_command.rs index d61595de..1b33abcb 100644 --- a/crates/vertigo/src/driver_module/dom_command.rs +++ b/crates/vertigo/src/driver_module/dom_command.rs @@ -181,7 +181,7 @@ pub fn sort_commands(list: Vec) -> Vec { } } - dom.extend(events.into_iter()); + dom.extend(events); dom } diff --git a/crates/vertigo/src/fetch/lazy_cache.rs b/crates/vertigo/src/fetch/lazy_cache.rs index fcd5449e..6d151ea7 100644 --- a/crates/vertigo/src/fetch/lazy_cache.rs +++ b/crates/vertigo/src/fetch/lazy_cache.rs @@ -4,7 +4,7 @@ use std::rc::Rc; use crate::{ computed::{context::Context, Value}, struct_mut::ValueMut, - Instant, Resource, get_driver, Computed, transaction, DomNode, + Instant, Resource, get_driver, Computed, transaction, DomNode, ToComputed, }; use super::request_builder::{RequestBuilder, RequestBody}; @@ -219,13 +219,19 @@ impl LazyCache { } } +impl ToComputed>> for LazyCache { + fn to_computed(&self) -> Computed>> { + self.to_computed() + } +} + impl PartialEq for LazyCache { fn eq(&self, other: &Self) -> bool { self.id == other.id } } -impl LazyCache { +impl LazyCache { pub fn render(&self, render: impl Fn(Rc) -> DomNode + 'static) -> DomNode { self.to_computed().render_value(move |value| { match value { @@ -241,7 +247,7 @@ impl LazyCache { }, Resource::Error(error) => { use crate as vertigo; - + vertigo::dom! {
"error = " diff --git a/crates/vertigo/src/fetch/resource.rs b/crates/vertigo/src/fetch/resource.rs index 4d065d89..dc1a0c49 100644 --- a/crates/vertigo/src/fetch/resource.rs +++ b/crates/vertigo/src/fetch/resource.rs @@ -1,4 +1,7 @@ use core::ops::{ControlFlow, FromResidual, Try}; +use std::rc::Rc; + +use crate::{ToComputed, Computed}; /// The state of the resource. #[derive(Clone, Debug)] @@ -80,3 +83,20 @@ impl PartialEq for Resource { } } } + +impl ToComputed>> for Resource { + fn to_computed(&self) -> crate::Computed>> { + Computed::from({ + let myself = self.clone(); + move |_| myself.clone().map(|item| Rc::new(item)) + }) + } +} + +impl ToComputed>> for Computed> { + fn to_computed(&self) -> crate::Computed>> { + self.map(|res| + res.map(|item| Rc::new(item)) + ) + } +} diff --git a/crates/vertigo/src/html_macro/dom.rs b/crates/vertigo/src/html_macro/dom.rs index a1ea7401..6793c46a 100644 --- a/crates/vertigo/src/html_macro/dom.rs +++ b/crates/vertigo/src/html_macro/dom.rs @@ -1,5 +1,5 @@ use crate::{ - dom::dom_node::{DomNode}, + dom::dom_node::DomNode, Computed, DomText, DomElement, DomComment, Value, }; diff --git a/crates/vertigo/src/html_macro/tests/bind.rs b/crates/vertigo/src/html_macro/tests/bind.rs index feb5f2b6..acca015c 100644 --- a/crates/vertigo/src/html_macro/tests/bind.rs +++ b/crates/vertigo/src/html_macro/tests/bind.rs @@ -29,5 +29,3 @@ fn test_bind() { on_click3(); } - - diff --git a/crates/vertigo/src/lib.rs b/crates/vertigo/src/lib.rs index 6dd96bf2..b4614af9 100644 --- a/crates/vertigo/src/lib.rs +++ b/crates/vertigo/src/lib.rs @@ -104,7 +104,7 @@ mod external_api; use computed::struct_mut::ValueMut; pub use computed::{ - AutoMap, Computed, context::Context, Dependencies, DropResource, GraphId, struct_mut, Value + AutoMap, Computed, ToComputed, context::Context, Dependencies, DropResource, GraphId, struct_mut, Value }; pub use dom::{ @@ -140,12 +140,8 @@ pub use fetch::{ resource::Resource, }; pub use future_box::{FutureBoxSend, FutureBox}; -pub use html_macro::{ - EmbedDom -}; -pub use instant::{ - Instant, InstantType -}; +pub use html_macro::EmbedDom; +pub use instant::{Instant, InstantType}; pub use websocket::{WebsocketConnection, WebsocketMessage}; /// Allows to include a static file @@ -288,6 +284,10 @@ pub fn transaction R>(f: F) -> R { get_driver().transaction(f) } +pub mod prelude { + pub use crate::{bind, css, dom, DomNode, ToComputed, Value, Computed, Css}; +} + //------------------------------------------------------------------------------------------------------------------ // Internals below //------------------------------------------------------------------------------------------------------------------ diff --git a/examples/counter/Cargo.toml b/examples/counter/Cargo.toml index 01e91fdb..55b6499d 100644 --- a/examples/counter/Cargo.toml +++ b/examples/counter/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "vertigo-example-counter" -version = "0.3.1" +version = "0.3.2" authors = ["Grzegorz Szeliga ", "Michał Pokrywka "] edition = "2021" @@ -8,4 +8,4 @@ edition = "2021" crate-type = ["cdylib", "rlib"] [dependencies] -vertigo = { path = "../../crates/vertigo", version = "0.3.1" } +vertigo = { path = "../../crates/vertigo", version = "0.3.2" } diff --git a/examples/router/Cargo.toml b/examples/router/Cargo.toml index 876dd5f3..ff3a80e3 100644 --- a/examples/router/Cargo.toml +++ b/examples/router/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "vertigo-example-router" -version = "0.3.1" +version = "0.3.2" authors = ["Grzegorz Szeliga ", "Michał Pokrywka "] edition = "2021" @@ -9,4 +9,4 @@ crate-type = ["cdylib", "rlib"] [dependencies] log = "0.4.14" -vertigo = { path = "../../crates/vertigo", version = "0.3.1" } +vertigo = { path = "../../crates/vertigo", version = "0.3.2" } diff --git a/examples/trafficlights/Cargo.toml b/examples/trafficlights/Cargo.toml index a2ea2eea..1c6e67f3 100644 --- a/examples/trafficlights/Cargo.toml +++ b/examples/trafficlights/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "vertigo-example-trafficlights" -version = "0.3.1" +version = "0.3.2" authors = ["Grzegorz Szeliga ", "Michał Pokrywka "] edition = "2021" @@ -8,4 +8,4 @@ edition = "2021" crate-type = ["cdylib", "rlib"] [dependencies] -vertigo = { path = "../../crates/vertigo", version = "0.3.1" } +vertigo = { path = "../../crates/vertigo", version = "0.3.2" }