com.vaadin.hilla.signals.
Class ValueSignal<T>
Direct Known Subclasses:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
ValueSignal
(ValueSignal<T> delegate) ValueSignal
(Class<T> valueType) Creates a new ValueSignal with provided valueType and
null
as 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 boolean
compareAndSet
(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.ObjectNode
Creates 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.ObjectNode
handleReplaceEvent
(StateEvent<T> stateEvent) protected com.fasterxml.jackson.databind.node.ObjectNode
handleSetEvent
(StateEvent<T> stateEvent) protected com.fasterxml.jackson.databind.node.ObjectNode
handleValidationResult
(StateEvent<T> event, ValidationResult validationResult, Function<StateEvent<T>, com.fasterxml.jackson.databind.node.ObjectNode> handler) 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.
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, notnull
valueType
- the value type class, notnull
Throws:
NullPointerException
- if the default defaultValue or the valueType isnull
-
ValueSignal
Creates a new ValueSignal with provided valueType and
null
as 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:
getDelegate
in classSignal<T>
-
subscribe
public reactor.core.publisher.Flux<com.fasterxml.jackson.databind.node.ObjectNode> subscribe()Description copied from class:
Signal
Subscribes to the signal.
-
subscribe
public reactor.core.publisher.Flux<com.fasterxml.jackson.databind.node.ObjectNode> subscribe(String signalId) Description copied from class:
Signal
Subscribes 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:
Signal
Creates a snapshot event reflecting the current state of the signal.
Specified by:
createSnapshotEvent
in 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:
processEvent
in classSignal<T>
Parameters:
event
- the event to processReturns:
the processed event, with the accepted flag set to either
true
orfalse
, 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 valueReturns:
true
if the value was successfully updated,false
otherwise -
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(); });
ValueOperation
type 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(); });
SetValueOperation
andReplaceValueOperation
implement 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:
Signal
Returns 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:
asReadonly
in classSignal<T>
Returns:
the read-only signal
-