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

Add an AnyViewSequence which is analogous to the AnyView for ViewSequences #158

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Philipp-M
Copy link
Contributor

This allows for type-erased ViewSequences.

Original Motivation was to check whether this helps with reducing wasm binary size in xilem_html (which wasn't really that successful, static optimization + compression seems to work quite well at this point). But I think this could be helpful otherwise too, e.g. for recursive ViewSequences (see e.g. here) or cross-language bindings that need type erasure in sequences.

@Philipp-M Philipp-M marked this pull request as draft February 8, 2024 17:43
@Philipp-M
Copy link
Contributor Author

Keeping as draft, until there's a real reason for this (maybe recursive ViewSequences, but I'm not sure whether that is more of an artificial use-case)

@casey
Copy link
Contributor

casey commented Aug 16, 2024

I mentioned this in Zulip, but I also wanted to comment on the issue so it wouldn't get lost. I have a place where this could be useful. I have a branch where depending on a state variable, I want to return different ViewSequence types. A .boxed combinator would be quite useful:

if foo {
  (
    h1("Foo"),
    span("hello"),
  ).boxed()
} else {
  (
    h1("Bar"),
    div("goodbye"),
  ).boxed()
}

@Philipp-M
Copy link
Contributor Author

Yeah I see use-cases for this, and I think we should indeed land an updated version of this (also just to reflect a similar API for View <-> AnyView to ViewSequence <-> AnyViewSequence).

For your example you could use boxed for the second element to make it work though.

As a more general workaround for now for a changing number of sequences as mentioned by Daniel on zulip:

You could do either something like this:

let mut seq = Vec::new();
if foo {
    seq.push(h1("Foo").boxed());
    seq.push(span("hello").boxed());
    seq.push(p("baz").boxed());
} else {
    seq.push(h1("Bar").boxed());
    seq.push(div("goodbye").boxed());
}

Or alternatively something like:

if foo {
    (
        h1("Foo"),
        span("hello").boxed(),
        Some(p("baz")),
    )
} else {
    (
        h1("Bar"),
        div("goodbye").boxed(),
        None
    )
}

may work.

(The Option version should also work with the return type impl DomFragment/impl ViewSequence, while the Vec does not).

@casey
Copy link
Contributor

casey commented Aug 16, 2024

For your example you could use boxed for the second element to make it work though.

Unfortunately my actual code is way more complicated, and has way more differing nodes.

You could do either something like this:

let mut seq = Vec::new();
if foo {
    seq.push(h1("Foo").boxed());
    seq.push(span("hello").boxed());
    seq.push(p("baz").boxed());
} else {
    seq.push(h1("Bar").boxed());
    seq.push(div("goodbye").boxed());
}

This seems pretty reasonable for now.

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

Successfully merging this pull request may close these issues.

2 participants