Android Open Source - Reactor Reactor Dependency






From Project

Back to project page Reactor.

License

The source code is released under:

MIT License

If you think the Android project Reactor listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package io.dwak.tracker;
/*w  w w .  ja v  a 2  s.  com*/
import android.util.SparseArray;

/**
 * A Dependency represents an atomic unit of reactive data that a
 * computation might depend on.
 * When the data changes, the computations are invalidated.
 */
public class ReactorDependency {
    private SparseArray<ReactorComputation> mDependentsById;

    public ReactorDependency() {
        mDependentsById = new SparseArray<ReactorComputation>();
    }

    public boolean depend() {
        return depend(null);
    }

    /**
     * Declares that the current computation (or {@link ReactorComputation} if given) depends on `dependency`.
     * The computation will be invalidated the next time `dependency` changes.
     * If there is no current computation and {@link #depend()}} is called with no arguments, it does nothing and returns false.
     *
     * @param reactorComputation computation that depends on this dependency
     * @return true if computation is a new dependant rather than an existing one
     */
    public boolean depend(ReactorComputation reactorComputation) {
        if (reactorComputation == null) {
            if (!Reactor.mActive)
                return false;

            reactorComputation = Reactor.getInstance().getCurrentReactorComputation();
        }

        final int id = reactorComputation.getId();
        if (mDependentsById.get(id) == null) {
            mDependentsById.put(id, reactorComputation);
            reactorComputation.addInvalidateComputationFunction(new ReactorComputationFunction() {
                @Override
                public void react() {
                    mDependentsById.remove(id);
                }
            });

            return true;
        }
        return false;
    }

    /**
     * Invalidate all dependent computations immediately and remove them as dependents.
     */
    public void changed() {
        int key;
        for (int i = 0; i < mDependentsById.size(); i++) {
            key = mDependentsById.keyAt(i);
            mDependentsById.get(key).invalidate();
        }
    }

    /**
     * True if this Dependency has one or more dependent {@link ReactorComputation},
     * which would be invalidated if this {@link ReactorDependency} were to change.
     * @return true if the dependency has dependants
     */
    public boolean hasDependants() {
        return mDependentsById.size() > 0;
    }
}




Java Source Code List

io.dwak.androidtracker.AndroidReactorApplication.java
io.dwak.androidtracker.ApplicationTest.java
io.dwak.androidtracker.MainActivity.java
io.dwak.androidtracker.viewmodel.FavoriteFoodViewModel.java
io.dwak.tracker.ApplicationTest.java
io.dwak.tracker.ReactorComputationFunction.java
io.dwak.tracker.ReactorComputation.java
io.dwak.tracker.ReactorDependency.java
io.dwak.tracker.Reactor.java
io.dwak.tracker.TrackerFlushCallbacks.java