This project has been deprecated. In modern version of Arezs it is trivial to reimplement and with upcoming projects such as Spritz, it is no longer consider a recommended pattern.
An Arez component that defines a a "when" observer. When observers contain two functions; the condition function and the effect callback. The condition function is the tracked function that re-executes anytime a dependency changes and returns a boolean value. When the condition callback returns true, the observer invokes the effect function as an action and disposes itself. i.e. "When" observers are used when the developer needs to perform an action when a condition is true.
A when observer is more complex than other observers within Arez. The when observer is actually composed of
an autorun observer, a computed property and an action. There are several When.when(...)
methods that can be
used to create when observers.
An example of a basic autorun observer:
When.when( () -> {
// Interact with arez observable state (or computed values) here
// and any time these changed this function will be re-run.
// This method should return true, when the effect is to be run
...
}, () -> {
// This action will be invoked when the condition block returns true
...
} );
A more complex example that passes several additional parameters:
final String name = "MyWatcher";
final boolean doesEffectMutateState = true;
final Priority conditionPriority = Priority.HIGH;
final boolean runImmediately = true;
final Observer whenObserver =
When.when( name, doesEffectMutateState, () -> {
// Interact with arez observable state (or computed values) here
// and any time these changed this function will be re-run.
// This method should return true, when the effect is to be run
...
}, () -> {
// This action will be invoked when the condition block returns true
...
}, conditionPriority, runImmediately );
The simplest way to use component;
- add the dependency into the build system. i.e. add the following snippet to Maven or equivalent.
<dependency>
<groupId>org.realityforge.arez.when</groupId>
<artifactId>arez-when</artifactId>
<version>0.29</version>
</dependency>
-
add the snippet
<inherits name="arez.when.When"/>
into the .gwt.xml file if you are using GWT. -
Write the java code to use the when observer.
For more information about component, please see the Website. For the source code and project support please visit the GitHub project.
The component was released as open source so others could benefit from the project. We are thankful for any contributions from the community. A Code of Conduct has been put in place and a Contributing document is under development.
The component is licensed under Apache License, Version 2.0.