You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would have expected autorun to behave the same on its first run or its nth run, but it appears to do something different on the first run? For the following code I would have expected the autorun to loop until the observable value it's tracking stops changing.
test("autorun",()=>{consto=mobx.observable.box(0);constseen: number[]=[];mobx.autorun(()=>{seen.push(o.get());if(o.get()<5)o.set(o.get()+1);});expect(seen).toStrictEqual([0,1,2,3,4,5]);// this assertion fails as "seen" is [0]});
Actual outcome:
The following test case is the actual behavior.
test("autorun",()=>{consto=mobx.observable.box(0);constseen: number[]=[];mobx.autorun(()=>{seen.push(o.get());if(o.get()<5)o.set(o.get()+1);});// autorun doesn't continue to run even though a tracked observable value has changedexpect(seen).toStrictEqual([0]);// triggering the autorun again then causes it to loop (but it has "lost" the value 1)o.set(o.get()+1);expect(seen).toStrictEqual([0,2,3,4,5]);});
Versions
6.11.0
The text was updated successfully, but these errors were encountered:
This is working as intended; subscription happens after the body has completed. So you're subscribing only for the next change at the moment o is already 1, which is why you miss the first change. In other words, the first o.set doesn't trigger the autorun, because the autorun is not yet an observer of it.
Intended outcome:
I would have expected autorun to behave the same on its first run or its nth run, but it appears to do something different on the first run? For the following code I would have expected the autorun to loop until the observable value it's tracking stops changing.
Actual outcome:
The following test case is the actual behavior.
Versions
6.11.0
The text was updated successfully, but these errors were encountered: