Repository navigation
fix: make trigger recompute computed signals - #132
kanashimia wants to merge 1 commit into
Conversation
|
|
da17b3f to
dc01984
Compare
|
I think
|
Well, computed signals aren't exactly meant to be mutated anyways in this library? If they were it would make some sense.
As notably there is no setter. And original request didn't involve any computed signals at all: #83 they were only briefly mentioned. That person also kinda wanted IDK, I also know some other libraries do have similar API with similar behaviour on the derived signals, where computeds don't recompute 🤷🏻♀️ |
|
(ahh, pressed the wrong button) Though, I see that the documentation needs to be updated too if you are ok with this. I forgot about it. |
|
Disclosure: I'm not a maintainer of this package. For me, this is a feature, not a bug. The problem that When dealing with vendor code that you don't own, you can replace your // VENDOR CODE -----------------------------------------------------------------------
let name = 'John Doe'
let count = 0
document
.querySelectorAll('input.name')
.forEach(input => input.addEventListener('change', () => { name = input.value }))
document
.querySelectorAll('.counter')
.forEach(counter => counter.addEventListener('click', () => count++))
// APP CODE ---------------------------------------------------------------------------
const getText = () => `Hello ${name}, here is your count: ${count}`
const $text = signal()
const update = () => $text(getText())
// Call this whenever you need to update `$text`
// (instead of using `trigger()` on a `Computed`).
update()
effect(() => { document.getElementById('app').textContent = $text() })When you own the whole codebase, you can use const $name = signal('John Doe')
const $count = signal(0)
document
.querySelectorAll('input.name')
.forEach(input => input.addEventListener('change', () => $name(input.value)))
document
.querySelectorAll('.counter')
.forEach(counter => counter.addEventListener('click', () => $count(++$count())))
const $text = computed(() => `Hello ${$name()}, here is your count: ${$count()}`)
effect(() => { document.getElementById('app').textContent = $text() }) |
|
How is that relevant? Your code has zero computed signals which you trigger or would want to trigger. No sane person would write stuff like this: const getScore = alien.signal(10);
const getUserMap = alien.computed(() => new Map([['alice', { score: getScore() }]]));
alien.effect(() => {
console.log(getUserMap().get('alice')!.score);
});
{
const userMap = getUserMap();
userMap.get('alice')!.score = 20;
alien.trigger(getUserMap);
}And if they do they deserve a punch in the face. |
|
Anyways, const src = alien.signal(0);
const der = alien.computed(() => {
src();
console.log("FN");
return 0;
});
// 1
// alien.trigger(der);
alien.effect(() => {
der();
console.log("DER");
});
// 2
// alien.trigger(der);If you uncomment either trigger number 1 or trigger number 2: Behaviour from the master: Behaviour from this branch: But, when you remove that const der = alien.computed(() => {
console.log("FN");
return 0;
});
// 1
// alien.trigger(der);
alien.effect(() => {
der();
console.log("DER");
});
// 2
// alien.trigger(der);Behaviour from the master: Behaviour from this branch: Weird glitchyy bug. So yeah, original is broken, there should be no difference. TBH this API isn't great for lazy computed signals anyways, because it would try to evaluate them before triggering the update. Compared to a separate signal for recompute, or solidjs 2 API where signal accessor stores a hidden "refresh" symbol. I will need to think more about this. Also this one "should not rerun effects if upon trigger a computed returns the same value" partially defeats the point, like there needs to be a way to emulate Even if the computed fns aren't rerun, adding tests and fixing this bug would be good. |
I've noticed that trigger doesn't trigger updates for the computed, unless that computed is invalidated by another signal, it was very weird, I don't think it is intentional, looks like a bug to me.