Search in sources :

Example 1 with Any

use of jsinterop.base.Any in project react4j by react4j.

the class IntrospectUtil method prepareStateUpdate.

/**
 * Prepare the newState value to be updated given specified current state.
 * If no changes are required then return false.
 *
 * @param newState     the new "state" of the view.
 * @param currentState the current "state" of the view.
 * @return true if newState needs to be saved to native view, false otherwise.
 */
public static boolean prepareStateUpdate(@Nonnull final JsPropertyMap<Object> newState, @Nullable final JsPropertyMap<Object> currentState) {
    /*
     * To determine whether we need to do a state update we do compare each key and value and make sure
     * they match. In some cases keys can be removed (i.e. a dependency is no longer observed) but as state
     * updates in react are merges, we need to implement this by putting undefined values into the state.
     */
    if (null != currentState) {
        boolean[] needsSave = new boolean[1];
        currentState.forEach(key -> {
            if (!newState.has(key)) {
                newState.set(key, Js.undefined());
                needsSave[0] = true;
            } else {
                final Any newValue = currentState.getAsAny(key);
                final Any existingValue = newState.getAsAny(key);
                if (!Objects.equals(newValue, existingValue)) {
                    needsSave[0] = true;
                }
            }
        });
        return needsSave[0];
    } else {
        return true;
    }
}
Also used : Any(jsinterop.base.Any)

Aggregations

Any (jsinterop.base.Any)1