Interface WritableSignal<T>

Type Parameters:
T - the signal value type
All Superinterfaces:
Serializable, Signal<T>
All Known Implementing Classes:
MappedModifySignal, MappedWritableSignal, SharedNumberSignal, SharedValueSignal, ValueSignal

public interface WritableSignal<T> extends Signal<T>
A signal to which a new value can be directly written.
  • Method Details

    • set

      SignalOperation<T> set(T 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

      SignalOperation<Void> replace(T expectedValue, T newValue)
      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 to AtomicReference.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 value
      newValue - the new value
      Returns:
      an operation containing the eventual result
    • update

      CancelableOperation<T> update(SignalUpdater<T> updater)
      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, not null
      Returns:
      an operation containing the eventual result
    • asReadonly

      default Signal<T> 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

      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. 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, 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, not null
      merger - the function to create a new value for this signal given the current value and a new child value, not null
      Returns:
      a two-way mapped signal, not null