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

Computed breaks if dependency is set after being unwatched by a Watcher #27

Open
justinfagnani opened this issue Jul 31, 2024 · 2 comments

Comments

@justinfagnani
Copy link

Reproduction: https://stackblitz.com/edit/signal-polyfil-unwatch-bug?file=src%2Fmain.ts

If a computed is watched by a watcher, then a dependency is updated, then the computed is re-watched by the watcher, the computed will not update.

const count = new Signal.State(0);
const countPlusOne = new Signal.Computed(() => {
  return count.get() + 1;
});

const watcher = new Signal.subtle.Watcher(() => {
  watcher.watch();
});

watcher.watch(countPlusOne);

console.log('expected 1, got', countPlusOne.get());
count.set(1);
console.log('expected 2, got', countPlusOne.get());

watcher.unwatch(countPlusOne);
count.set(2);
// If this line is removed, the computed works.
watcher.watch(countPlusOne);

// Reading countPlusOne here gives an incorrect value of 2!
console.log('expected 3, got', countPlusOne.get());
@justinfagnani
Copy link
Author

As a workaround in cases where you need to unwatch and re-watch, you can throw away the computed and create a fresh one.

@divdavem
Copy link

For info, this issue is the same as tc39/proposal-signals#216, it is fixed by #16

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

2 participants