Interface WritableSignal<T>
- Type Parameters:
T- the signal value type
- All Superinterfaces:
Serializable,Signal<T>
- All Known Implementing Classes:
MappedModifySignal,MappedWritableSignal,SharedNumberSignal,SharedValueSignal,ValueSignal
-
Method Summary
Modifier and TypeMethodDescriptionWraps this signal to not accept changes.default <C> WritableSignal<C> map(SignalMapper<T, C> getter, ValueMerger<T, C> merger) Creates a two-way mapped signal that provides a bidirectional view of this signal.Sets the value of this signal if and only if the signal has the expected value at the time when the operation is confirmed.update(SignalUpdater<T> updater) Updates the signal value based on the given callback.Sets the value of this signal.
-
Method Details
-
value
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.- 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.- 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 cancelled or until the update succeeds without conflicting changes.The process can be cancelled 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
-
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
-
map
Creates a two-way mapped signal that provides a bidirectional view of this signal. Reading the mapped signal applies the getter function to extract a child value. Writing to the mapped signal uses the setter function to update this signal with a new value derived from the current value and the new child value.This is useful for creating component bindings to properties of complex objects. For example, to bind a checkbox to the "done" property of a Todo record:
record Todo(String text, boolean done) { Todo withDone(boolean done) { return new Todo(this.text, done); } } WritableSignal<Todo> todoSignal = new ValueSignal<>( new Todo("Buy milk", false)); WritableSignal<Boolean> doneSignal = todoSignal.map(Todo::done, Todo::withDone); checkbox.bindValue(doneSignal); // 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, notnullmerger- the function to create a new value for this signal given the current value and a new child value, notnull- Returns:
- a two-way mapped signal, not
null
-