Package com.vaadin.client.flow.reactive
Class Reactive
java.lang.Object
com.vaadin.client.flow.reactive.Reactive
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 Summary
Modifier and TypeMethodDescriptionstatic elemental.events.EventRemoveraddEventCollector(ReactiveValueChangeListener reactiveValueChangeListener) Adds a reactive change listener that will be invoked whenever a reactive change event is fired from any reactive event router.static voidaddFlushListener(FlushListener flushListener) Adds a listener that will be invoked the next timeflush()is invoked.static voidaddPostFlushListener(FlushListener postFlushListener) Adds a listener that will be invoked during the nextflush(), after all regular flush listeners have been invoked.static voidflush()Flushes all flush listeners and post flush listeners.static ComputationGets the currently active computation.static voidFires a reactive change event to all registered event collectors.static voidreset()Resets Reactive to the initial state.static ComputationrunWhenDependenciesChange(Command command) Evaluates the given command whenever there is a change in anyReactiveValueused in the command.static voidrunWithComputation(Computation computation, Command command) Runs a task with the given computation set asgetCurrentComputation().
-
Method Details
-
addFlushListener
Adds a listener that will be invoked the next timeflush()is invoked. A listener added during a flush will be invoked before that flush finishes.- Parameters:
flushListener- the flush listener to add
-
addPostFlushListener
Adds a listener that will be invoked during the nextflush(), 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. -
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
nullif there is no current computation.
-
runWithComputation
Runs a task with the given computation set asgetCurrentComputation(). 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 currentcommand- 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
Fires a reactive change event to all registered event collectors.- Parameters:
event- the fired event- See Also:
-
runWhenDependenciesChange
Evaluates the given command whenever there is a change in anyReactiveValueused in the command.- Parameters:
command- the command to run- Returns:
- A
Computationobject 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!
-