Skip to content

Commit

Permalink
Unified ToComputed trait
Browse files Browse the repository at this point in the history
  • Loading branch information
sfisol committed Jul 17, 2023
1 parent 0d231f2 commit fb1ff3c
Show file tree
Hide file tree
Showing 23 changed files with 99 additions and 73 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<!-- markdownlint-configure-file { "no-duplicate-heading": { "siblings_only": true } } -->

<!-- markdownlint-disable-next-line first-line-h1 -->
## 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

Expand Down
5 changes: 3 additions & 2 deletions crates/vertigo-cli/src/watch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...");
Expand Down
4 changes: 2 additions & 2 deletions crates/vertigo-macro/src/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub fn bind_rc_fn(input: TokenStream) -> Result<TokenStream, String> {

let types = {
let mut types_macro: Vec<TokenStream2> = Vec::new();

for type_item in func_params.into_iter() {
let type_item = get_type(type_item)?;

Expand All @@ -254,7 +254,7 @@ pub fn bind_rc_fn(input: TokenStream) -> Result<TokenStream, String> {

Ok(quote!{
{
let func: std::rc::Rc::<dyn Fn(#(#types,)*)> = std::rc::Rc::new(vertigo::bind!(#(#bind_params,)* #body));
let func: std::rc::Rc::<dyn Fn(#(#types,)*) -> _> = std::rc::Rc::new(vertigo::bind!(#(#bind_params,)* #body));
func
}
}.into())
Expand Down
2 changes: 1 addition & 1 deletion crates/vertigo/src/computed/auto_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
rc::Rc,
};

use crate::{struct_mut::HashMapMut};
use crate::struct_mut::HashMapMut;

type CreateType<K, V> = Box<dyn Fn(&AutoMap<K, V>, &K) -> V>;

Expand Down
18 changes: 17 additions & 1 deletion crates/vertigo/src/computed/computed_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -199,3 +199,19 @@ impl From<&str> for Computed<String> {
Value::new(value.to_string()).to_computed()
}
}

pub trait ToComputed<T: Clone> {
fn to_computed(&self) -> Computed<T>;
}

impl<T: Clone + 'static> ToComputed<T> for Computed<T> {
fn to_computed(&self) -> Computed<T> {
self.clone()
}
}

impl<T: Clone + 'static> ToComputed<T> for &Computed<T> {
fn to_computed(&self) -> Computed<T> {
(*self).clone()
}
}
4 changes: 2 additions & 2 deletions crates/vertigo/src/computed/dependencies/graph_connections.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
6 changes: 3 additions & 3 deletions crates/vertigo/src/computed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T1, T2, ...>` out of `Value<T1>`, `Value<T2>`, ...
Expand Down
2 changes: 1 addition & 1 deletion crates/vertigo/src/computed/tests/computed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
22 changes: 17 additions & 5 deletions crates/vertigo/src/computed/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -140,20 +140,32 @@ impl<T: Clone + 'static> Value<T> {
})
}

pub fn id(&self) -> GraphId {
self.inner.id
}

pub fn deps(&self) -> &'static Dependencies {
self.inner.deps
}

pub fn to_computed(&self) -> Computed<T> {
let self_clone = self.clone();

Computed::from(move |context| {
self_clone.get(context)
})
}
}

pub fn id(&self) -> GraphId {
self.inner.id
impl<T: Clone + 'static> ToComputed<T> for Value<T> {
fn to_computed(&self) -> Computed<T> {
self.to_computed()
}
}

pub fn deps(&self) -> &'static Dependencies {
self.inner.deps
impl<T: Clone + 'static> ToComputed<T> for &Value<T> {
fn to_computed(&self) -> Computed<T> {
(*self).to_computed()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/vertigo/src/dom/dom_element.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{rc::Rc};
use std::rc::Rc;

use vertigo_macro::bind;

Expand Down
34 changes: 3 additions & 31 deletions crates/vertigo/src/dom/dom_text.rs
Original file line number Diff line number Diff line change
@@ -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<T: Clone> {
fn to_computed_param(&self) -> Computed<T>;
}

impl<T: Clone + 'static> ToComputed<T> for Computed<T> {
fn to_computed_param(&self) -> Computed<T> {
self.clone()
}
}

impl<T: Clone + 'static> ToComputed<T> for &Computed<T> {
fn to_computed_param(&self) -> Computed<T> {
(*self).clone()
}
}

impl<T: Clone + 'static> ToComputed<T> for Value<T> {
fn to_computed_param(&self) -> Computed<T> {
self.to_computed()
}
}

impl<T: Clone + 'static> ToComputed<T> for &Value<T> {
fn to_computed_param(&self) -> Computed<T> {
self.to_computed()
}
}


/// A Real DOM representative - text kind
pub struct DomText {
driver: Driver,
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion crates/vertigo/src/dom_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion crates/vertigo/src/driver_module/api/api_dom_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/vertigo/src/driver_module/dom_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub fn sort_commands(list: Vec<DriverDomCommand>) -> Vec<DriverDomCommand> {
}
}

dom.extend(events.into_iter());
dom.extend(events);

dom
}
12 changes: 9 additions & 3 deletions crates/vertigo/src/fetch/lazy_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -219,13 +219,19 @@ impl<T> LazyCache<T> {
}
}

impl<T: Clone> ToComputed<Resource<Rc<T>>> for LazyCache<T> {
fn to_computed(&self) -> Computed<Resource<Rc<T>>> {
self.to_computed()
}
}

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

impl<T: PartialEq> LazyCache<T> {
impl<T: PartialEq + Clone> LazyCache<T> {
pub fn render(&self, render: impl Fn(Rc<T>) -> DomNode + 'static) -> DomNode {
self.to_computed().render_value(move |value| {
match value {
Expand All @@ -241,7 +247,7 @@ impl<T: PartialEq> LazyCache<T> {
},
Resource::Error(error) => {
use crate as vertigo;

vertigo::dom! {
<div>
"error = "
Expand Down
20 changes: 20 additions & 0 deletions crates/vertigo/src/fetch/resource.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -80,3 +83,20 @@ impl<T: PartialEq> PartialEq for Resource<T> {
}
}
}

impl<T: Clone + 'static> ToComputed<Resource<Rc<T>>> for Resource<T> {
fn to_computed(&self) -> crate::Computed<Resource<Rc<T>>> {
Computed::from({
let myself = self.clone();
move |_| myself.clone().map(|item| Rc::new(item))
})
}
}

impl<T: Clone + 'static> ToComputed<Resource<Rc<T>>> for Computed<Resource<T>> {
fn to_computed(&self) -> crate::Computed<Resource<Rc<T>>> {
self.map(|res|
res.map(|item| Rc::new(item))
)
}
}
2 changes: 1 addition & 1 deletion crates/vertigo/src/html_macro/dom.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
dom::dom_node::{DomNode},
dom::dom_node::DomNode,
Computed, DomText, DomElement, DomComment, Value,
};

Expand Down
2 changes: 0 additions & 2 deletions crates/vertigo/src/html_macro/tests/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,3 @@ fn test_bind() {

on_click3();
}


14 changes: 7 additions & 7 deletions crates/vertigo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -288,6 +284,10 @@ pub fn transaction<R, F: FnOnce(&Context) -> R>(f: F) -> R {
get_driver().transaction(f)
}

pub mod prelude {
pub use crate::{bind, css, dom, DomNode, ToComputed, Value, Computed, Css};
}

//------------------------------------------------------------------------------------------------------------------
// Internals below
//------------------------------------------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions examples/counter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "vertigo-example-counter"
version = "0.3.1"
version = "0.3.2"
authors = ["Grzegorz Szeliga <[email protected]>", "Michał Pokrywka <[email protected]>"]
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
vertigo = { path = "../../crates/vertigo", version = "0.3.1" }
vertigo = { path = "../../crates/vertigo", version = "0.3.2" }
Loading

0 comments on commit fb1ff3c

Please sign in to comment.