Class SharedValueSignal<T extends @Nullable Object>
- Type Parameters:
T- the signal value type
- All Implemented Interfaces:
Signal<T>,Serializable
- Direct Known Subclasses:
SharedNumberSignal
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.vaadin.flow.signals.shared.AbstractSignal
AbstractSignal.ChildSignalFactory<I extends AbstractSignal<?>>, AbstractSignal.ResultConverter<T extends @Nullable Object> -
Field Summary
Fields inherited from class com.vaadin.flow.signals.shared.AbstractSignal
ANYTHING_GOES -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedSharedValueSignal(SignalTree tree, Id id, CommandValidator validator, Class<T> valueType) Creates a new value signal instance with the given id and validator for the given signal tree with the given value type.SharedValueSignal(Class<T> valueType) Creates a new value signal of the given type with no value.SharedValueSignal(T initialValue) Creates a new value signal with the given initial value. -
Method Summary
Modifier and TypeMethodDescriptionasNode()Converts this signal into a node signal.Wraps this signal to not accept changes.booleanprotected @Nullable TextractValue(@Nullable Node.Data data) Extracts the value for this signal from the given signal data node.inthashCode()Sets the value of this signal if and only if the signal has the expected value at the time when the operation is confirmed.Sets the value of this signal.toString()update(SignalUpdater<T> updater) Updates the signal value based on the given callback.<C> SerializableConsumer<C> updater(ValueMerger<T, C> merger) Creates a callback that updates this signal value using the provided merger function.protected @Nullable ObjectusageChangeValue(Node.Data data) Gets a reference value that will be used to determine whether a dependency based on previous usage should be invalidated.verifyValue(T expectedValue) Checks that this signal has the expected value.withValidator(CommandValidator validator) Wraps this signal with a validator.Methods inherited from class com.vaadin.flow.signals.shared.AbstractSignal
clear, createUsage, data, data, fromJson, get, id, mergeValidators, nodeValue, peek, peekConfirmed, remove, submit, submit, submit, submitInsert, submitVoidOperation, toJson, tree, validator
-
Constructor Details
-
SharedValueSignal
Creates a new value signal with the given initial value. The type of the signal will be based on the type (Object.getClass()) of the initial value instance. The signal does not support clustering.- Parameters:
initialValue- the initial value to use, notnull
-
SharedValueSignal
Creates a new value signal of the given type with no value. The signal does not support clustering.- Parameters:
valueType- the value type, notnull
-
-
Method Details
-
set
Sets the value of this signal.The result of the returned operation will be resolved with the previous value at the time when this operation was confirmed.
Setting a new value will trigger effect functions that have reads from this signal.
- Parameters:
value- the value to set- Returns:
- an operation containing the eventual result
-
extractValue
Description copied from class:AbstractSignalExtracts the value for this signal from the given signal data node.- Specified by:
extractValuein classAbstractSignal<T extends @Nullable Object>- Parameters:
data- the data node to extract the value from, ornullif the node doesn't exist in the tree- Returns:
- the signal value
-
usageChangeValue
Description copied from class:AbstractSignalGets a reference value that will be used to determine whether a dependency based on previous usage should be invalidated. This is done by getting one reference value when the dependency occurs and then comparing that to the current value to determine if the value has changed.The implementation should return an object that changes if and only if the
AbstractSignal.get()of this signal changes.- Specified by:
usageChangeValuein classAbstractSignal<T extends @Nullable Object>- Parameters:
data- the data node to read from, notnull- Returns:
- a reference value to use for validity checks, may be
null
-
replace
Sets the value of this signal if and only if the signal has the expected value at the time when the operation is confirmed. This is the signal counterpart toAtomicReference.compareAndSet(Object, Object). The result of the returned operation will be resolved as successful if the expected value was present and resolved as unsuccessful if any other value was present when the operation is processed.- Parameters:
expectedValue- the expected valuenewValue- the new value- Returns:
- an operation containing the eventual result
-
update
Updates the signal value based on the given callback. The callback receives the current signal value and returns the new value to use. If the original value has changed by the time this change is confirmed, then the returned value is ignored and the callback is run again with the new value as input. This process is repeated until canceled or until the update succeeds without conflicting changes.The process can be canceled through the returned operation instance. Note that canceling will only prevent further retries but the change will still be made if the currently running attempt succeeds.
The result of the returned operation will be resolved with the previous value at the time when a successful update operation was confirmed.
Update operations cannot participate in transactions since any retry would occur after the original transaction has already been committed. For this reason, the whole operation completely bypasses all transaction handling.
- Parameters:
updater- the value update callback, notnull- Returns:
- an operation containing the eventual result
-
verifyValue
Checks that this signal has the expected value. This operation is only meaningful to use as a condition in atransaction. The result of the returned operation will be resolved as successful if the expected value was present and resolved as unsuccessful if any other value was present when the operation is processed.- Parameters:
expectedValue- the expected value- Returns:
- an operation containing the eventual result
-
withValidator
Wraps this signal with a validator. The validator is used to check all value changing commands issued through the new signal instance. If this signal has a validator, then the new signal will use both validators. Note that due to the way validators are retained byasNode(), there's a possibility that the validator also receives commands that cannot be directly issued for a value signal.This signal will keep its current configuration and changes applied through this instance will be visible through the wrapped instance.
- Parameters:
validator- the validator to use, notnull- Returns:
- a new value signal that uses the validator, not
null
-
asReadonly
Wraps this signal to not accept changes.This signal will keep its current configuration and changes applied through this instance will be visible through the wrapped instance.
- Returns:
- the new readonly signal, not
null
-
asNode
Description copied from class:AbstractSignalConverts this signal into a node signal. This allows further conversion into any specific signal type through the methods inSharedNodeSignal. The converted signal is backed by the same underlying data and uses the same validator as this signal.- Overrides:
asNodein classAbstractSignal<T extends @Nullable Object>- Returns:
- this signal as a node signal, not
null
-
equals
-
hashCode
public int hashCode() -
toString
-
updater
Creates a callback that updates this signal value using the provided merger function. This is useful for creating write callbacks forbindValuewhen working with immutable value patterns.The merger function receives the current signal value and a new child value, and should return a new signal value. This is typically a method reference to a "with" style method on an immutable record or class.
Example usage with an immutable record:
record Person(String name, int age) { Person withName(String name) { return new Person(name, this.age); } } SharedValueSignal<Person> personSignal = new SharedValueSignal<>( new Person("Alice", 30)); textField.bindValue(personSignal.map(Person::name), personSignal.updater(Person::withName));- Type Parameters:
C- the child value type that will be provided to the callback- Parameters:
merger- the function to create a new signal value from the old value and a new child value, notnull- Returns:
- a callback that updates this signal using the merger function,
not
null
-