Previous value of an Atom #6
-
Hey, Is there a way to get previous value of an [Atom] property inside of Reaction? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
No, there is no way get the previous value of an atom. Alternatively, you can write reaction that remembers the previous value. private void Start()
{
Reaction(
() => Counter,
(current, previous) => Debug.Log($"Counter changed from {previous} to {current}")
);
}
private static Reaction Reaction<T>(AtomPull<T> reaction, Action<T, T> effect)
{
var hasPrevious = false;
var previous = default(T);
return Atom.Reaction(reaction, value =>
{
if (hasPrevious)
{
effect(value, previous);
}
previous = value;
hasPrevious = true;
});
} |
Beta Was this translation helpful? Give feedback.
-
Thanks! Btw, I want to use it to find the difference of 2 collections so that the view layer could efficiently update it's contents. I am trying to replace UniRx-based view models with [Atom]-based. |
Beta Was this translation helpful? Give feedback.
No, there is no way get the previous value of an atom. Alternatively, you can write reaction that remembers the previous value.