computed store can return stale value

Author: russelldavisCreated May 2, 2024Updated May 3, 2024

Here's a test case to illustrate:

typescript
test('stale computed', () => {
  let $atom = atom(1)
  let values: (number | string)[] = []
  let $computed1 = computed($atom, () => {values.push($computed2.get())})
  let $computed2 = computed($atom, value => value * 2)
  $computed1.get()
  $atom.set(2)
  deepStrictEqual(values, [2, 4])
})

$computed2.get() inside the $computed1 callback should return 2 and then 4. But it actually returns 2 and then 2.