/// <summary> /// Creates an atom that store the value. /// </summary> /// <param name="lifetime">Atom lifetime.</param> /// <param name="value">Initial value.</param> /// <param name="debugName">Debug name for this atom.</param> /// <typeparam name="T">Atom value type.</typeparam> /// <returns>Created atom.</returns> /// <example> /// /// var counter = Atom.Value(Lifetime, 0); /// counter.Value += 1; /// /// Debug.Log(counter.Value); /// /// </example> public static MutableAtom <T> Value <T>(Lifetime lifetime, T value, string debugName = null) { var atom = new ValueAtom <T>(debugName, value); lifetime.Register(atom); return(atom); }
/// <summary> /// Creates an atom that compute its value by a function.<br/> /// </summary> /// <remarks> /// Computed values can be used to derive information from other atoms. /// They evaluate lazily, caching their output and only recomputing /// if one of the underlying atoms has changed. If they are not observed /// by anything, they suspend entirely.<br/> /// <br/> /// Conceptually, they are very similar to formulas in spreadsheets, /// and can't be underestimated. They help in reducing the amount of state /// you have to store and are highly optimized. Use them wherever possible. /// </remarks> /// <param name="lifetime">Atom lifetime.</param> /// <param name="pull">Function for pulling value.</param> /// <param name="keepAlive">Should an atom keep its value actualized when there are no subscribers?</param> /// <param name="debugName">Debug name for this atom.</param> /// <typeparam name="T">Atom value type.</typeparam> /// <returns>Created atom.</returns> /// <example> /// /// var a = Atom.Value(1); /// var b = Atom.Value(2); /// /// var sum = Atom.Computed(Lifetime, () => a.Value + b.Value); /// /// Debug.Log(sum.Value); /// /// </example> public static Atom <T> Computed <T>(Lifetime lifetime, Func <T> pull, bool keepAlive = false, string debugName = null) { var atom = new ComputedAtom <T>(debugName, pull, keepAlive); lifetime.Register(atom); return(atom); }
/// <summary> /// Creates an reaction that should run every time anything it observes changes. /// It also runs once when you create the reaction itself. It only responds /// to changes in observable state, things you have annotated atom. /// </summary> /// <param name="lifetime">Reaction lifetime.</param> /// <param name="reaction">A function for reaction.</param> /// <param name="exceptionHandler">A function that called when an exception is thrown while computing an reaction.</param> /// <param name="debugName">Debug name for this reaction.</param> /// <returns>Created reaction.</returns> /// <example> /// /// var counter = Atom.Value(1); /// /// Atom.Reaction(Lifetime, () => /// { /// Debug.Log("Counter: " + counter.Value); /// }); /// /// </example> public static Reaction Reaction( Lifetime lifetime, Action reaction, Action<Exception> exceptionHandler = null, string debugName = null) { var atom = new ReactionAtom(debugName, reaction, exceptionHandler); lifetime.Register(atom); atom.Activate(); return atom; }