tc39 / proposal-signals

A proposal to add signals to JavaScript.
MIT License
2.87k stars 54 forks source link

Polyfill bug: calling watcher.watch on computed signal (before its latest value is computed) can prevent from getting its correct latest value afterward #216

Open divdavem opened 2 weeks ago

divdavem commented 2 weeks ago

Hello,

I have come across the following bug with the current polyfill implementation (0.1.0): if I call watcher.watch with a computed signal before reading its most up-to-date value, I can no longer get its correct up-to-date value:

  const signal = new Signal.State(0);
  const computedSignal = new Signal.Computed(() => signal.get());
  const watcher = new Signal.subtle.Watcher(() => {});
  expect(computedSignal.get()).toBe(0);
  signal.set(1); // let's update the signal on which computedSignal depends
  watcher.watch(computedSignal); // if this line is removed, the expectation on the following line is successful
  expect(computedSignal.get()).toBe(1); // this expectation fails with current polyfill implementation