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

Server-side default value of a ValueSignal is not immutable #2786

Open
Legioth opened this issue Oct 3, 2024 · 0 comments
Open

Server-side default value of a ValueSignal is not immutable #2786

Legioth opened this issue Oct 3, 2024 · 0 comments
Labels
bug Something isn't working hilla Issues related to Hilla needs design

Comments

@Legioth
Copy link
Member

Legioth commented Oct 3, 2024

Describe the bug

If you have a mutable bean as the value of a server-side ValueSignal, then changes made to that object will be seen by new subscribers but not by old subscribers which may in turn lead to lots of confusion.

Expected-behavior

Expected that the value signal creates its own copy of the data so that future changes to the original instance will be ignored by the signal. This should probably be done so that the value field in ValueSignal is a com.fasterxml.jackson.databind.JsonNode instead of T. This would also have the benefit that comparisons done by compareAndSet would always be based only on JSON equality rather than using the object's own equals() implementation which might not be consistent with comparisons done in the browser.

Reproduction

Service:

@BrowserCallable
@AnonymousAllowed
public class CountService {
    public static class Holder {
        private int value;

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }
    }

    private Holder holder = new Holder();
    private ValueSignal<Holder> signal = new ValueSignal<>(holder, Holder.class);

    public ValueSignal<Holder> signal() {
        return signal;
    }

    public void updateValue() {
        holder.value++;
    }
}

View:

const signal = CountService.signal({defaultValue: {value: -1}});

export default function TestView() {
    return (
        <VerticalLayout className="poll">
            <span>Value: {"" + signal.value.value}</span>
            <Button onClick={() => CountService.updateValue()}>Update</Button>
        </VerticalLayout>
    );
}

Steps:

  1. Open view in the browser, observe that the shown value is 0
  2. Click the button, observe that the shown value remains as 0
  3. Open the same URL in a new browser tab, observe that the shown value in that tab is 1

System Info

Vaadin 24.5.0.beta4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hilla Issues related to Hilla needs design
Projects
None yet
Development

No branches or pull requests

2 participants