Skip to content

Commit

Permalink
fix: clarify Operation return type
Browse files Browse the repository at this point in the history
  • Loading branch information
taefi committed Jan 8, 2025
1 parent 39daf77 commit ea700ae
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions articles/hilla/guides/full-stack-signals.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ package com.example.application;
import com.vaadin.flow.server.auth.AnonymousAllowed;
import com.vaadin.hilla.BrowserCallable;
import com.vaadin.hilla.Nonnull;
import org.jspecify.annotations.NonNull;
import com.vaadin.hilla.signals.ValueSignal;
@AnonymousAllowed
Expand All @@ -172,8 +172,8 @@ public class PersonService {
private final ValueSignal<Person> sharedPerson =
new ValueSignal<>(initialValue, Person.class); // <3>
@Nonnull
public ValueSignal<@Nonnull Person> sharedPerson() { // <4>
@NonNull
public ValueSignal<@NonNull Person> sharedPerson() { // <4>
return sharedPerson;
}
}
Expand Down Expand Up @@ -233,6 +233,34 @@ Incidentally, a call to `cancel()` may not always be effective, as a succeeding

Operations such as `replace` and `update` perform a "compare and set" on the server using the [methodname]`equals` method of the value type to compare the values. Thus, it's important to make sure the value type has a proper implementation of the [methodname]`equals` method.

==== Using Operation Results

The `Operation` and `OperationSubscription` objects returned by the `set`, `replace`, and `update` methods have a `result` property that is a `Promise`. This promise is resolved when the operation is completed and confirmed by the server. The promise can also be rejected if the operation fails, for example, due to a validation error. The promise can be used to chain further operations, as well.

The following example demonstrates how to use the `result` property of an operation:

[source,tsx]
.`person.tsx`
----
const sharedPerson: ValueSignal<Person> =
PersonService.sharedPerson({ defaultValue: { name: '', age: 0 } });
export default function PersonView() {
return (
<VerticalLayout theme="padding">
<Button onClick={() => {
sharedPerson.replace({
name: sharedPerson.value.name,
age: sharedPerson.value.age + 1
}).result // <1>
.then(() => console.log('Successfully increased the age.'))
.catch(() => console.error('Server rejected the operation.'));
}}>Increase age</Button>
</VerticalLayout>
);
}
----
<1> The `result` property of the `replace` operation is a `Promise` that can be used to chain further operations. In this case, the promise is used to log a message when the operation is successfully completed, or to log an error message when the operation is rejected by the server.

[[number-signal]]
=== NumberSignal
Expand Down

0 comments on commit ea700ae

Please sign in to comment.