com.vaadin.hilla.signals.

Class ValueSignal<T>

java.lang.Object
com.vaadin.hilla.signals.Signal<T>
com.vaadin.hilla.signals.ValueSignal<T>

Direct Known Subclasses:

NumberSignal

public class ValueSignal<T> extends Signal<T>
  • Constructor Details

    • ValueSignal

      public ValueSignal(T defaultValue, Class<T> valueType)

      Creates a new ValueSignal with the provided default value.

      Parameters:

      defaultValue - the default value, not null

      valueType - the value type class, not null

      Throws:

      NullPointerException - if the default defaultValue or the valueType is null

    • ValueSignal

      public ValueSignal(Class<T> valueType)

      Creates a new ValueSignal with provided valueType and null as the default value.

      Parameters:

      valueType - the value type class, not null

      Throws:

      NullPointerException - if the default defaultValue or the valueType is null

    • ValueSignal

      protected ValueSignal(ValueSignal<T> delegate)
  • Method Details

    • getDelegate

      protected ValueSignal<T> getDelegate()

      Overrides:

      getDelegate in class Signal<T>

    • subscribe

      public reactor.core.publisher.Flux<com.fasterxml.jackson.databind.node.ObjectNode> subscribe()

      Description copied from class: Signal

      Subscribes to the signal.

      Overrides:

      subscribe in class Signal<T>

      Returns:

      a Flux of JSON events

    • 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.

      Overrides:

      subscribe in class Signal<T>

      Parameters:

      signalId - the internal signal id

      Returns:

      a Flux of JSON events

    • getValue

      @Nullable public T 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 class Signal<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 class Signal<T>

      Parameters:

      event - the event to process

      Returns:

      the processed event, with the accepted flag set to either true or false, and the validation error set with the validator message (if case of a failure).

    • handleSetEvent

      protected com.fasterxml.jackson.databind.node.ObjectNode handleSetEvent(StateEvent<T> stateEvent)
    • handleReplaceEvent

      protected com.fasterxml.jackson.databind.node.ObjectNode handleReplaceEvent(StateEvent<T> stateEvent)
    • compareAndSet

      protected boolean compareAndSet(T newValue, T expectedValue)

      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 set

      expectedValue - the expected value

      Returns:

      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

      public ValueSignal<T> withOperationValidator(OperationValidator<T> validator)

      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":

      
       ValueSignal<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();
       });
       
      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, the 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:
      
       ValueSignal<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();
       });
       
      As both SetValueOperation and ReplaceValueOperation implement the ValueOperation, the validator covers both operations.

      Parameters:

      validator - the operation validator, not null

      Returns:

      a new signal that validates the operations with the provided validator

      Throws:

      NullPointerException - if the validator is null

    • asReadonly

      public ValueSignal<T> 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 class Signal<T>

      Returns:

      the read-only signal