Previous value of an Atom #6
Answered
by
vanifatovvlad
slimshader
asked this question in
Q&A
-
Hey, Is there a way to get previous value of an [Atom] property inside of Reaction? |
Beta Was this translation helpful? Give feedback.
Answered by
vanifatovvlad
Mar 5, 2021
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.
0 replies
Answer selected by
vanifatovvlad
-
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.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No, there is no way get the previous value of an atom. Alternatively, you can write reaction that remembers the previous value.