Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Previous value of an Atom #3

Closed
slimshader opened this issue Mar 3, 2021 · 2 comments
Closed

Previous value of an Atom #3

slimshader opened this issue Mar 3, 2021 · 2 comments

Comments

@slimshader
Copy link

Hey, Is there a way to get previous value of an [Atom] property inside of Reaction?

@vanifatovvlad
Copy link
Collaborator

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;
    });
}

@slimshader
Copy link
Author

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;
    });
}

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.

@codewriter-packages codewriter-packages locked and limited conversation to collaborators Jul 21, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants