Skip to content

Commit

Permalink
use cow str for element
Browse files Browse the repository at this point in the history
  • Loading branch information
schell committed Oct 29, 2023
1 parent 8d93495 commit 6a6bc47
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
4 changes: 2 additions & 2 deletions crates/mogwai-dom/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mogwai-dom"
version = "0.2.5"
version = "0.2.6"
edition = "2021"
authors = ["Schell Scivally <efsubenovex@gmail.com>"]
license = "MIT"
Expand All @@ -23,7 +23,7 @@ async-lock = "2.6.0"
futures-lite = { version = "1.12.0", default-features = false }
lazy_static = "1.4.0"
log = "^0.4"
mogwai = { version = "0.6.7", path = "../mogwai" }
mogwai = { version = "0.6.8", path = "../mogwai" }
mogwai-macros = { version = "0.1.0", path = "../mogwai-macros", features = ["dom"] }
send_wrapper = "^0.6"
serde = { version = "^1.0", features = ["derive"] }
Expand Down
15 changes: 7 additions & 8 deletions crates/mogwai-dom/src/view/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
ops::{Bound, Deref, RangeBounds},
pin::Pin,
sync::{Arc, Weak},
task::Waker,
task::Waker, borrow::Cow,
};

use anyhow::Context;
Expand Down Expand Up @@ -463,7 +463,7 @@ impl JsDom {
event_target,
sink,
}: Listener,
f: fn(JsDomEvent) -> AnyEvent
f: fn(JsDomEvent) -> AnyEvent,
) -> anyhow::Result<()> {
let tx = sink.contra_map(f);
let callback = match event_target {
Expand Down Expand Up @@ -493,10 +493,7 @@ impl JsDom {
}

/// Add an event listener to this element.
pub fn add_listener(
&self,
listener: Listener,
) -> anyhow::Result<()> {
pub fn add_listener(&self, listener: Listener) -> anyhow::Result<()> {
self.add_listener_with(listener, |event: JsDomEvent| AnyEvent::new(event))
}

Expand Down Expand Up @@ -607,7 +604,9 @@ pub(crate) fn build(
});
let key = match identity {
ViewIdentity::Branch(t) => HydrationKey::try_new(t, attribs, may_parent),
ViewIdentity::NamespacedBranch(t, _) => HydrationKey::try_new(t, attribs, may_parent),
ViewIdentity::NamespacedBranch(t, _) => {
HydrationKey::try_new(t, attribs, may_parent)
}
ViewIdentity::Leaf(t) => HydrationKey::try_new(t, attribs, may_parent),
}?;
key.hydrate()?
Expand Down Expand Up @@ -704,7 +703,7 @@ impl From<JsDom> for ViewBuilder {
fn from(js: JsDom) -> Self {
ViewBuilder {
// this doesn't matter as it's replaced by the hydration_root
identity: ViewIdentity::Branch(""),
identity: ViewIdentity::Branch(Cow::from("")),
initial_values: Default::default(),
updates: Default::default(),
post_build_ops: Default::default(),
Expand Down
14 changes: 7 additions & 7 deletions crates/mogwai-dom/src/view/ssr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use anyhow::Context;
use async_executor::Executor;
use async_lock::RwLock;
use std::{collections::HashMap, future::Future, ops::DerefMut, pin::Pin, sync::Arc};
use std::{collections::HashMap, future::Future, ops::DerefMut, pin::Pin, sync::Arc, borrow::Cow};

use mogwai::{
either::Either,
Expand Down Expand Up @@ -60,7 +60,7 @@ impl Downcast<SsrDomEvent> for AnyEvent {
// keygen - facilitates public key generation for web certificates
// [deprecated] source - specifies media sources for picture, audio, and
// video elements
fn tag_is_voidable(tag: &'static str) -> bool {
fn tag_is_voidable(tag: &Cow<'static, str>) -> bool {
tag == "area"
|| tag == "base"
|| tag == "br"
Expand All @@ -83,7 +83,7 @@ pub enum SsrNode {
/// Parent node.
Container {
/// Tag name.
name: &'static str,
name: Cow<'static, str>,
/// Tag attributes.
attributes: Vec<(String, Option<String>)>,
/// Styles
Expand Down Expand Up @@ -231,11 +231,11 @@ impl SsrDom {
}

/// Creates a container node that may contain child nodes.
pub fn element(executor: Arc<Executor<'static>>, tag: &'static str) -> Self {
pub fn element(executor: Arc<Executor<'static>>, tag: impl Into<Cow<'static, str>>) -> Self {
SsrDom {
executor,
node: Arc::new(RwLock::new(SsrNode::Container {
name: tag,
name: tag.into(),
attributes: vec![],
styles: vec![],
children: vec![],
Expand Down Expand Up @@ -453,9 +453,9 @@ pub(crate) fn build(
} = builder;
// intialize it
let dom = match identity {
ViewIdentity::Branch(tag) => SsrDom::element(executor.clone(), &tag),
ViewIdentity::Branch(tag) => SsrDom::element(executor.clone(), tag),
ViewIdentity::NamespacedBranch(tag, ns) => {
let el = SsrDom::element(executor.clone(), &tag);
let el = SsrDom::element(executor.clone(), tag);
el.set_attrib("xmlns", Some(&ns))?;
el
}
Expand Down
2 changes: 1 addition & 1 deletion crates/mogwai/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mogwai"
version = "0.6.7"
version = "0.6.8"
edition = "2021"
authors = ["Schell Scivally <efsubenovex@gmail.com>"]
license = "MIT"
Expand Down
23 changes: 15 additions & 8 deletions crates/mogwai/src/view.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
//! Domain agnostic view doclaration.
use std::{
any::Any,
borrow::Cow,
future::Future,
pin::Pin,
sync::Arc,
task::{RawWaker, Wake, Waker},
};

use crate::{
model::Model,
patch::{HashPatch, ListPatch},
sink::{Sink, SinkExt},
stream::{Stream, StreamExt}, model::Model,
stream::{Stream, StreamExt},
};
use anyhow::Context;
pub use anyhow::Error;
Expand Down Expand Up @@ -289,7 +291,9 @@ impl<T, St: Stream<Item = T> + Send + 'static> MogwaiValue<T, St> {

pub type PinBoxStream<T> = Pin<Box<dyn Stream<Item = T> + Send + 'static>>;

impl<T: Clone + PartialEq + Send + Sync + 'static> From<Model<T>> for MogwaiValue<T, PinBoxStream<T>> {
impl<T: Clone + PartialEq + Send + Sync + 'static> From<Model<T>>
for MogwaiValue<T, PinBoxStream<T>>
{
fn from(model: Model<T>) -> Self {
let stream = Box::pin(model.stream());
if let Some(current) = model.current() {
Expand Down Expand Up @@ -370,8 +374,8 @@ where
/// The starting identity of a view.
#[derive(Debug)]
pub enum ViewIdentity {
Branch(&'static str),
NamespacedBranch(&'static str, &'static str),
Branch(Cow<'static, str>),
NamespacedBranch(Cow<'static, str>, Cow<'static, str>),
Leaf(String),
}

Expand Down Expand Up @@ -468,9 +472,9 @@ impl ViewBuilder {
}

/// Create a new container element builder.
pub fn element(tag: &'static str) -> Self {
pub fn element(tag: impl Into<Cow<'static, str>>) -> Self {
ViewBuilder {
identity: ViewIdentity::Branch(tag),
identity: ViewIdentity::Branch(tag.into()),
initial_values: Default::default(),
updates: Default::default(),
post_build_ops: vec![],
Expand All @@ -482,9 +486,12 @@ impl ViewBuilder {
}

/// Create a new namespaced container element builder.
pub fn element_ns(tag: &'static str, ns: &'static str) -> Self {
pub fn element_ns(
tag: impl Into<Cow<'static, str>>,
ns: impl Into<Cow<'static, str>>,
) -> Self {
ViewBuilder {
identity: ViewIdentity::NamespacedBranch(tag, ns),
identity: ViewIdentity::NamespacedBranch(tag.into(), ns.into()),
initial_values: Default::default(),
updates: Default::default(),
post_build_ops: vec![],
Expand Down

0 comments on commit 6a6bc47

Please sign in to comment.