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 support for computed full-stack signals #2933

Open
peholmst opened this issue Nov 27, 2024 · 2 comments
Open

Add support for computed full-stack signals #2933

peholmst opened this issue Nov 27, 2024 · 2 comments
Labels
enhancement New feature or request hilla Issues related to Hilla

Comments

@peholmst
Copy link
Member

peholmst commented Nov 27, 2024

Describe your motivation

When working with signals to build a more complex UI, you often want to compute one signal from another. The resulting computed signal is then a read-only signal whose value changes whenever the original signal value changes. This already exists on the client side through useComputed(). I would like something similar on the Java side.

Describe the solution you'd like

One to one

For mapping a signal to another, I'd like a map() method, similar to the one found in Stream and Optional:

ValueSignal<String> myValue = new ValueSignal<String>(null, String.class);
Signal<Boolean> hasMyValue = myValue.map(Objects::nonNull); // True whenever myValue contains a non-null value

The returned signal is read-only.

Many to one

For mapping multiple signals to one, I'd like something like this:

ValueSignal<String> name = new ValueSignal<String>("", String.class);
NumberSignal age = new NumberSignal(0d);
Signal<String> nameAndAge = computedSignal(() -> "%s (%d years)".formatted(name.getValue(), age.getValue()), name, age);

In this example, the computedSignal factory method takes two parameters: a Supplier that computes the value, and an array of signals that the computed signal depends on. Whenever any of the dependencies change, the computed signal is re-computed. If you can figure out a way of deducing the dependencies from the Supplier alone, it would be even better.

The returned signal is read-only.

Describe alternatives you've considered

No response

Additional context

No response

@peholmst peholmst added enhancement New feature or request hilla Issues related to Hilla labels Nov 27, 2024
@Legioth
Copy link
Member

Legioth commented Nov 27, 2024

You read my mind except that the type of a readonly signal should be just Signal<T> instead of ValueSignal<T>.

At the same time, it's usually more efficient with Hilla to do the computation on the client so that you don't need to send multiple related value updates from the server to the client.

@peholmst
Copy link
Member Author

peholmst commented Nov 27, 2024

Won't these signals eventually end up in Flow as well? (and I fixed my examples)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hilla Issues related to Hilla
Projects
None yet
Development

No branches or pull requests

2 participants