Skip to content
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

map_fragment and collect_fragment don't work with custom element children #13

Open
cameronbraid opened this issue Jan 22, 2024 · 2 comments

Comments

@cameronbraid
Copy link
Contributor

cameronbraid commented Jan 22, 2024

{data.into_iter().map_fragment(|item| html! {
    <CustomElement/>
})}

errors with 'await' is only allowed inside 'async' functions and blocks

I am experimenting with the following trait and impl

#[async_trait]
pub trait MapFragmentAsyncExt<V> {
  async fn map_fragment<F, B, C>(self, mut f: F) -> String
  where
      Self: Sized,
      F: FnMut(V) -> C + std::marker::Send,
      C: Future<Output = B> + std::marker::Send,
      B: ToString,
      V: std::marker::Send
  ;
}


#[async_trait]
impl<V> MapFragmentAsyncExt<V> for Vec<V>
{
  async fn map_fragment<F, B, C>(self, mut f: F) -> String
  where
      Self: Sized,
      F: FnMut(V) -> C + std::marker::Send,
      C: Future<Output = B> + std::marker::Send,
      B: ToString,
      V: std::marker::Send
  {
    use futures::stream::StreamExt;

    let mut out  = Vec::with_capacity(self.len());
    let mut stream = futures::stream::iter(self);
    while let Some(v) = stream.next().await {
      let c = f(v).await;
      out.push(c.to_string());
    }
    out.into_iter().collect::<Vec<_>>().join("")
  }
}

which allows the following :

{data.map_fragment(|item| async { html! {
    <CustomElement/>
}}).await}

It moves data, and a bit awkward with the bracketing and .await but works none the less

@cameronbraid cameronbraid changed the title map_fragment and collect_fragment don't work with custom elements map_fragment and collect_fragment don't work with custom element children Jan 22, 2024
@cameronbraid
Copy link
Contributor Author

Another though on this.

Currently if I write my compoent fn as non async, it gets converted to async.

I don't need async components, so if the [component] macro could preserve my non async then I would be happy

@cameronbraid
Copy link
Contributor Author

cameronbraid commented Jan 23, 2024

Hrm, I just realised that this can't be done. Since at the callsite for the using custom element <CustomElement/> there is no way to know if the custom element is async or not and therefore know if .await sholud be added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant