Class Reactive

java.lang.Object
com.vaadin.client.flow.reactive.Reactive

public class Reactive extends Object
Handles global features related to reactivity, such as keeping track of the current Computation, providing a lazy flush cycle and registering reactive event collectors.

With a reactive programming model, the dependencies needed for producing a result are automatically registered when the result is computed. When any dependency of a computation is changed, that computation is scheduled to be recomputed. To reduce the number of recomputations performed when many dependencies are updated, the recomputation is performed lazily the next time flush() is invoked.

Since:
1.0
Author:
Vaadin Ltd
See Also:
  • Method Details

    • addFlushListener

      public static void addFlushListener(FlushListener flushListener)
      Adds a listener that will be invoked the next time flush() is invoked. A listener added during a flush will be invoked before that flush finishes.
      Parameters:
      flushListener - the flush listener to add
    • addPostFlushListener

      public static void addPostFlushListener(FlushListener postFlushListener)
      Adds a listener that will be invoked during the next flush(), after all regular flush listeners have been invoked. If a post flush listener adds new flush listeners, those flush listeners will be invoked before the next post flush listener is invoked.
      Parameters:
      postFlushListener - the listener to add
    • flush

      public static void flush()
      Flushes all flush listeners and post flush listeners. A listener is discarded after it has been invoked once. This means that there will be no listeners registered for the next flush at the time this method returns.
      See Also:
    • getCurrentComputation

      public static Computation getCurrentComputation()
      Gets the currently active computation. Any reactive value that is accessed when a computation is active should be added as a dependency to that computation so that the computation will be invalidated if the value changes.
      Returns:
      the current computation, or null if there is no current computation.
    • runWithComputation

      public static void runWithComputation(Computation computation, Command command)
      Runs a task with the given computation set as getCurrentComputation(). If another computation is set as the current computation, it is temporarily replaced by the provided computation, but restored again after the provided task has been run.
      Parameters:
      computation - the computation to set as current
      command - the command to run while the computation is set as current
    • addEventCollector

      public static elemental.events.EventRemover addEventCollector(ReactiveValueChangeListener reactiveValueChangeListener)
      Adds a reactive change listener that will be invoked whenever a reactive change event is fired from any reactive event router.
      Parameters:
      reactiveValueChangeListener - the listener to add
      Returns:
      an event remover that can be used to remove the listener
    • notifyEventCollectors

      public static void notifyEventCollectors(ReactiveValueChangeEvent event)
      Fires a reactive change event to all registered event collectors.
      Parameters:
      event - the fired event
      See Also:
    • runWhenDependenciesChange

      public static Computation runWhenDependenciesChange(Command command)
      Evaluates the given command whenever there is a change in any ReactiveValue used in the command.
      Parameters:
      command - the command to run
      Returns:
      A Computation object which can be used to control the evaluation
    • reset

      public static void reset()
      Resets Reactive to the initial state.

      Intended for test cases to call in setup to avoid having tests affect each other as Reactive state is static and shared.

      Should never be called from non-test code!