Signals & Deferrable — Series 2

Preview — 3 of 10 questions

Why does the second signal notify less often?

javascript
a = signal<string[]>([]);
b = signal<string[]>([], { equal: (x, y) => x.length === y.length });
Ab only accepts arrays of the same length; other writes throw
Bequal is only used by computed(), never by a writable signal
Cequal makes the signal deeply compare its contents
Dequal decides whether a write counts as a change. By default signals compare with Object.is, so any new array notifies; b treats same-length arrays as equal and skips the notification

What does wrapping the read change?

javascript
effect(() => {
  const id = this.selectedId();
  const all = untracked(() => this.allItems());
  this.log(id, all.length);
});
AIt reads a stale value from the previous tick
BIt defers the read until after the effect returns
CIt reads the current value without registering a dependency — the effect re-runs when selectedId changes, but not when allItems does
DIt prevents other effects from reading allItems concurrently

What is onCleanup for?

javascript
effect((onCleanup) => {
  const id = setInterval(() => this.poll(this.url()), 1000);
  onCleanup(() => clearInterval(id));
});
AIt runs only once, when the component is destroyed
BIt runs before each re-run of the effect and once on destroy — so the previous interval is cleared before a new one starts when url changes
CIt runs after the effect body, in the same tick, always
DIt cancels the effect permanently

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.