Skip to content

fix: make trigger recompute computed signals - #132

Open
kanashimia wants to merge 1 commit into
stackblitz:masterfrom
kanashimia:trigger-a-computed
Open

kanashimia wants to merge 1 commit into
stackblitz:masterfrom
kanashimia:trigger-a-computed

Conversation

@kanashimia

Copy link
Copy Markdown

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.

@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@lilian1315

Copy link
Copy Markdown
Contributor

I think trigger() is meant to notify dependents that one or many dependencies changed, not to re-trigger a computed factory.

The trigger() function allows you to manually trigger updates for downstream dependencies when you've directly mutated a signal's value without using the signal setter

@kanashimia

kanashimia commented Nov 17, 2026 •

Copy link
Copy Markdown
Author

I think trigger() is meant to notify dependents that one or many dependencies changed, not to re-trigger a computed factory.

Well, computed signals aren't exactly meant to be mutated anyways in this library? If they were it would make some sense.

when you've directly mutated a signal's value without using the signal setter

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 signal(value, { equals: () => false }) instead of a trigger(accessor).

IDK, I also know some other libraries do have similar API with similar behaviour on the derived signals, where computeds don't recompute 🤷🏻‍♀️

@kanashimia kanashimia closed this Nov 17, 2026
@kanashimia kanashimia reopened this Nov 17, 2026
@kanashimia

Copy link
Copy Markdown
Author

(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.

@lilian1315

lilian1315 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Disclosure: I'm not a maintainer of this package.

For me, this is a feature, not a bug.

The problem that trigger() solves is dealing with mutable values (Arrays, class instances, plain objects, etc.) that can change without going through the Signal setter.

When dealing with vendor code that you don't own, you can replace your Computed with a Signal and a factory function:

// 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 Signals instead of raw variables:

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() })

@kanashimia

kanashimia commented Nov 18, 2026 •

Copy link
Copy Markdown
Author

How is that relevant? Your code has zero computed signals which you trigger or would want to trigger.
This PR doesn't touch regular trigger(signal), it only changes the behaviour of trigger(computed)

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.

@kanashimia

kanashimia commented Nov 18, 2026 •

Copy link
Copy Markdown
Author

Anyways,
I originally haven't noticed but there are also such and such behaviours:

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:
// 1 prints FN FN DER
// 2 prints FN DER DER

Behaviour from this branch:
// 1 prints FN FN DER
// 2 prints FN DER FN

But, when you remove that src() signal:

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:
// 1 prints FN DER
// 2 prints FN DER DER

Behaviour from this branch:
// 1 prints FN FN DER
// 2 prints FN DER FN

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 { equals: false }.

Even if the computed fns aren't rerun, adding tests and fixing this bug would be good.
I have a fix for that that just adds an if (triggering) return check inside of the unwatched(), instead of the patch that I have in this PR. It isn't elegant but also fixes that weird behaviour.

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

Successfully merging this pull request may close these issues.

2 participants