Class ValueSignal<T>
- Type Parameters:
T- the signal value type
- All Implemented Interfaces:
Signal<T>,WritableSignal<T>,Serializable
Local signals are non-serializable and intended for UI-local state only. They do not participate in clustering and are simpler than shared signals.
Changing the signal to reference another immutable value is an atomic operation. It is safe to concurrently read and write the signal value from multiple threads.
The signal can also be used with mutable values in which case no thread
safety is provided. Mutations must be done through
modify(ValueModifier) to ensure dependents are informed after the
modification is applied.
Local value signals can't be used inside signal transactions.
All operation objects returned from methods on this class are resolved immediately.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new value signal with an initial value ofnull.ValueSignal(T initialValue) Creates a new value signal with the given initial value. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidHook for subclasses to perform precondition checks before accessing the value.<C> WritableSignal<C> mapMutable(SignalMapper<T, C> getter, SignalModifier<T, C> modifier) Creates a two-way mapped signal that uses in-place modification for writing.voidmodify(ValueModifier<T> modifier) Runs the given callback to apply changes to a mutable referenced value and then notifies dependents.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.update(SignalUpdater<T> updater) Updates the signal value based on the given callback.Methods inherited from class com.vaadin.flow.signals.local.AbstractLocalSignal
assertLockHeld, get, getSignalValue, getSignalValueUnsafe, lock, peek, setSignalValue, tryLock, unlockMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.signals.WritableSignal
asReadonly, map
-
Constructor Details
-
ValueSignal
Creates a new value signal with the given initial value.- Parameters:
initialValue- the initial value, may benull
-
ValueSignal
public ValueSignal()Creates a new value signal with an initial value ofnull.
-
-
Method Details
-
checkPreconditions
protected void checkPreconditions()Description copied from class:AbstractLocalSignalHook for subclasses to perform precondition checks before accessing the value. Called while holding the lock. Default implementation does nothing.- Overrides:
checkPreconditionsin classAbstractLocalSignal<T>
-
set
Description copied from interface:WritableSignalSets 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.- Specified by:
setin interfaceWritableSignal<T>- Parameters:
value- the value to set- Returns:
- an operation containing the eventual result
-
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.Comparison between the expected value and the new value is performed using
Object.equals(Object).- Specified by:
replacein interfaceWritableSignal<T>- 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. This implementation acquires a lock while running the updater which means that it's never necessary to run the callback again. This also means that canceling the returned operation will never have any effect.The result of the returned operation is resolved with the same value that was passed to the updater callback.
- Specified by:
updatein interfaceWritableSignal<T>- Parameters:
updater- the value update callback, notnull- Returns:
- an operation containing the result
-
modify
Runs the given callback to apply changes to a mutable referenced value and then notifies dependents.This method is only intended for cases where concurrency is limited through other means, such as Vaadin's session lock. Using this method concurrently with any other methods on the same instance may, but is not guaranteed to, cause an
ConcurrentModificationException. The exception can be thrown either from this method or from the other invoked method. This can happen even if the other method is safe for concurrent use.- Parameters:
modifier- a callback that receives the current value to modify, notnull
-
mapMutable
Creates a two-way mapped signal that uses in-place modification for writing. Reading the mapped signal applies the getter function to extract a child value. Writing to the mapped signal uses the modifier function to update this signal's value in place.This method is named differently from
WritableSignal.map(SignalMapper, SignalSetter)to avoid ambiguity in method overload resolution when using method references or lambdas.This is useful for mutable bean patterns where the parent object's properties are modified directly using setters. For example:
class Todo { private String text; private boolean done; public boolean isDone() { return done; } public void setDone(boolean done) { this.done = done; } } ValueSignal<Todo> todoSignal = new ValueSignal<>(new Todo()); WritableSignal<Boolean> doneSignal = todoSignal.mapMutable(Todo::isDone, Todo::setDone); checkbox.bindValue(doneSignal, doneSignal::set); // Two-way binding- Type Parameters:
C- the child (mapped) signal type- Parameters:
getter- the function to extract the child value from this signal's value, notnullmodifier- the function to modify this signal's value in place with the new child value, notnull- Returns:
- a two-way mapped signal using in-place modification, not
null
-