rebo / atomic_hooks

4 stars 6 forks source link

Reaction::on_update unexpected behavior #9

Open arn-the-long-beard opened 4 years ago

arn-the-long-beard commented 4 years ago

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 ?