Skip to content
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

Reaction::on_update unexpected behavior #9

Open
arn-the-long-beard opened this issue Sep 2, 2020 · 0 comments
Open

Reaction::on_update unexpected behavior #9

arn-the-long-beard opened this issue Sep 2, 2020 · 0 comments

Comments

@arn-the-long-beard
Copy link
Contributor

Hello,

So we have this unit test

   #[atom]
    fn a() -> Atom<i32> {
        0
    }

    #[atom]
    fn b() -> Atom<i32> {
        0
    }

    #[reaction]
    fn a_b_subtraction() -> Reaction<i32> {
        let a = a().observe();
        let b = b().observe();
        a - b
    }

    #[reaction]
    fn count_subtraction_when_update() -> Reaction<i32> {
        let c = c();
        let _update = a_b_subtraction().on_update(|| {
            println!("UPDATE !!!");
            c.update(|v| *v += 1)
        });
        c.get()
    }

    #[test]
    fn test_on_update_reaction() {
        let count = count_subtraction_when_update();
        println!("subtraction value -> {:?}", a_b_subtraction().get());
        a().update(|v| *v = 32);
        println!("subtraction value -> {:?}", a_b_subtraction().get());
        a().set(2);
        println!("subtraction value -> {:?}", a_b_subtraction().get());
        a().set(25);
        println!("subtraction value -> {:?}", a_b_subtraction().get());
        a().set(1);
        println!("subtraction value -> {:?}", a_b_subtraction().get());

          assert_eq!(count.get(), 5, "We should get 5 update counted");

        assert_eq!(count.get(), 5);
    }

Here is the output

    Finished test [unoptimized + debuginfo] target(s) in 0.02s
     Running target/debug/deps/atomic_hooks-565eb592155b6fce
subtraction value -> 0
subtraction value -> 32
UPDATE !!!
subtraction value -> 2
UPDATE !!!
subtraction value -> 25
UPDATE !!!
subtraction value -> 1
number of updates-> 3

We should get 5 update counted
Left:  3
Right: 5
<Click to see difference>

thread 'reactive_state_access::reaction::test::test_on_update_reaction' panicked at 'assertion failed: `(left == right)`
  left: `3`,
 right: `5`: We should get 5 update counted', src/reactive_state_access/reaction.rs:705:9

While .get() is working as expected, we have unexpected behavior from .on_update.

How do you think we can fix it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant