Package com.vaadin.hilla.signals
Class ValueSignal<T>
java.lang.Object
com.vaadin.hilla.signals.Signal<T>
com.vaadin.hilla.signals.ValueSignal<T>
- Direct Known Subclasses:
NumberSignal
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedValueSignal(ValueSignal<T> delegate) ValueSignal(Class<T> valueType) Creates a new ValueSignal with provided valueType andnullas the default value.ValueSignal(T defaultValue, Class<T> valueType) Creates a new ValueSignal with the provided default value. -
Method Summary
Modifier and TypeMethodDescriptionReturns a read-only instance of the signal that rejects any attempt to modify the signal value.protected booleancompareAndSet(T newValue, T expectedValue) Compares the current value with the expected value and updates the signal value if they match.protected com.fasterxml.jackson.databind.node.ObjectNodeCreates a snapshot event reflecting the current state of the signal.protected ValueSignal<T>getValue()Returns the signal's current value.protected com.fasterxml.jackson.databind.node.ObjectNodehandleReplaceEvent(StateEvent<T> stateEvent) protected com.fasterxml.jackson.databind.node.ObjectNodehandleSetEvent(StateEvent<T> stateEvent) protected com.fasterxml.jackson.databind.node.ObjectNodehandleValidationResult(StateEvent<T> event, ValidationResult validationResult, Function<StateEvent<T>, com.fasterxml.jackson.databind.node.ObjectNode> handler) protected com.fasterxml.jackson.databind.node.ObjectNodeprocessEvent(com.fasterxml.jackson.databind.node.ObjectNode event) Processes the event and updates the signal value if needed.reactor.core.publisher.Flux<com.fasterxml.jackson.databind.node.ObjectNode>Subscribes to the signal.reactor.core.publisher.Flux<com.fasterxml.jackson.databind.node.ObjectNode>Subscribes to an internal child signal with a specific signal id.withOperationValidator(OperationValidator<T> validator) Returns a new signal that validates the operations with the provided validator.
-
Constructor Details
-
ValueSignal
Creates a new ValueSignal with the provided default value.- Parameters:
defaultValue- the default value, notnullvalueType- the value type class, notnull- Throws:
NullPointerException- if the default defaultValue or the valueType isnull
-
ValueSignal
Creates a new ValueSignal with provided valueType andnullas the default value.- Parameters:
valueType- the value type class, notnull- Throws:
NullPointerException- if the default defaultValue or the valueType isnull
-
ValueSignal
-
-
Method Details
-
getDelegate
- Overrides:
getDelegatein classSignal<T>
-
subscribe
public reactor.core.publisher.Flux<com.fasterxml.jackson.databind.node.ObjectNode> subscribe()Description copied from class:SignalSubscribes to the signal. -
subscribe
public reactor.core.publisher.Flux<com.fasterxml.jackson.databind.node.ObjectNode> subscribe(String signalId) Description copied from class:SignalSubscribes to an internal child signal with a specific signal id. -
getValue
Returns the signal's current value.- Returns:
- the value
-
createSnapshotEvent
protected com.fasterxml.jackson.databind.node.ObjectNode createSnapshotEvent()Description copied from class:SignalCreates a snapshot event reflecting the current state of the signal.- Specified by:
createSnapshotEventin classSignal<T>- Returns:
- the snapshot event
-
processEvent
protected com.fasterxml.jackson.databind.node.ObjectNode processEvent(com.fasterxml.jackson.databind.node.ObjectNode event) Processes the event and updates the signal value if needed. Note that this method is not thread-safe and should be called from a synchronized context.- Specified by:
processEventin classSignal<T>- Parameters:
event- the event to process- Returns:
- the processed event, with the accepted flag set to either
trueorfalse, and the validation error set with the validator message (if case of a failure).
-
handleSetEvent
-
handleReplaceEvent
protected com.fasterxml.jackson.databind.node.ObjectNode handleReplaceEvent(StateEvent<T> stateEvent) -
compareAndSet
Compares the current value with the expected value and updates the signal value if they match. Note that this method is not thread-safe and should be called from a synchronized context.- Parameters:
newValue- the new value to setexpectedValue- the expected value- Returns:
trueif the value was successfully updated,falseotherwise
-
handleValidationResult
protected com.fasterxml.jackson.databind.node.ObjectNode handleValidationResult(StateEvent<T> event, ValidationResult validationResult, Function<StateEvent<T>, com.fasterxml.jackson.databind.node.ObjectNode> handler) -
withOperationValidator
Returns a new signal that validates the operations with the provided validator. As the same validator is for all operations, the validator should be able to handle all operations that the signal supports.For example, the following code creates a signal that disallows setting values containing the word "bad":
In the example above, the validator does not cover the replace operation. A similar type checking can be done for the replace operation if needed. However, theValueSignal<String> signal = new ValueSignal<>("Foo", String.class); ValueSignal<String> noBadWordSignal = signal.withOperationValidator(op -> { if (op instanceof SetValueOperation set && set.value().contains("bad")) { return ValidationResult.reject("Bad words are not allowed"); } return ValidationResult.allow(); });ValueOperationtype allows unifying the validation logic for all the operations that are manipulating the value. The following example shows how to define a validator that covers both the set and replace operations:
As bothValueSignal<String> signal = new ValueSignal<>("Foo", String.class); ValueSignal<String> noBadWordSignal = signal.withOperationValidator(op -> { if (op instanceof ValueOperation<String> valueOp && valueOp.value().contains("bad")) { return ValidationResult.reject("Bad words are not allowed"); } return ValidationResult.allow(); });SetValueOperationandReplaceValueOperationimplement theValueOperation, the validator covers both operations.- Parameters:
validator- the operation validator, notnull- Returns:
- a new signal that validates the operations with the provided validator
- Throws:
NullPointerException- if the validator isnull
-
asReadonly
Description copied from class:SignalReturns a read-only instance of the signal that rejects any attempt to modify the signal value. The read-only signal, however, receives the same updates as the original signal does.- Specified by:
asReadonlyin classSignal<T>- Returns:
- the read-only signal
-