Skip to content
Source
Sign inSign up
TanStack/store

fix(store): run compare when the previous atom value is undefined #403

Openmbret(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/403/headopened

🎯 Changes

Atoms skip compare whenever their previous value is undefined, so a computed atom that stays undefined notifies its subscribers on every recompute.

const stack = createAtom<Array<number>>([])
const top = createAtom(() => stack.get().at(-1))
top.subscribe(() => console.log('notified'))
stack.set([]) // logs "notified" although `top` is still undefined
  • Cause: _update reads an undefined previous snapshot as "this computed atom has not computed yet" so that a custom compare never sees an uncomputed snapshot. undefined is also a valid snapshot, so the check fires for every atom that holds undefined, and compare never runs for it. createAtom(undefined, { compare: () => true }).set(1) applies the write.
  • Fix: a hasSnapshot flag tracks whether a computed atom has computed yet. The first computation still skips compare.
  • Tests: tests/undefined-snapshot.test.ts. The first two cases fail on main and pass with the fix. The third pins that compare is not called before the first computation.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm test:pr, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

🤖 Generated with Claude Code

https://claude.ai/code/session_01BvSDyHMgrdgjFz8EAk5VKo

Summary by CodeRabbit

  • Bug Fixes
    • Computed values that return undefined are now handled consistently. The first computation stores its result, while later unchanged undefined results do not repeatedly notify subscribers. Custom comparison rules can determine whether a result should be treated as an update, including when the previous value is undefined. By default, value comparisons continue to use standard identity checks.
    1. changeset-bot[bot](GitHub profile, opens in a new tab)commented

      🦋 Changeset detected

      Latest commit: f72cae0b71e66c1805c9e715b160146de6fb7596

      The changes in this PR will be included in the next version bump.

      This PR includes changesets to release 9 packages
      NameType
      @tanstack/storePatch
      @tanstack/angular-storePatch
      @tanstack/lit-storePatch
      @tanstack/octane-storePatch
      @tanstack/preact-storePatch
      @tanstack/react-storePatch
      @tanstack/solid-storePatch
      @tanstack/svelte-storePatch
      @tanstack/vue-storePatch

      Not sure what this means? Click here to learn what changesets are.

      Click here if you're a maintainer who wants to add another changeset to this PR

    2. chatgpt-codex-connector[bot](GitHub profile, opens in a new tab)commented

      Codex Review Summary

      This comment shows the latest Codex review activity on this pull request.

      ReviewStatusCommitReview trigger
      📝 Code Review✅ Completed 2026-10-27T12:46:51.811084Zff8b25bPR opened
      ℹ️ About Codex in GitHub

      Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

      • Open a pull request for review
      • Mark a draft as ready
      • Comment "@codex review" or "@codex security review".

      Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      No actionable comments were generated in the recent review.

      Recent review info
      Run configuration
      Commits

      Reviewing files that changed from the base of the PR and between ff8b25bf7f93b4f2459e87df1134bda6fdf4c730 and f72cae0b71e66c1805c9e715b160146de6fb7596.

      Files selected for processing: 1
      • packages/store/src/atom.ts

      Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 1 remain after this review.

      Walkthrough

      Computed atoms now store their first result, including undefined, without running the comparator. Later results use the configured comparator or Object.is. Tests cover repeated undefined results, custom comparator behavior, and the first computation.

      Changes: Computed atom snapshot comparison

      Layer / File(s)Summary
      First snapshot and subsequent comparisons
      packages/store/src/atom.ts, packages/store/tests/undefined-snapshot.test.ts, .changeset/atom-compare-undefined.md
      Computed atoms track whether a snapshot exists. The first result is stored without comparison; later results use the comparator. Tests cover undefined results, subscriber notifications, and the first computation. The changeset describes the comparison behavior.
      Priority: Normal

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Change: Bug fix

      Possibly related PRs

      • TanStack/store#402: Both changes define how atom updates handle undefined values and snapshots.

      Suggested reviewers: davidkpiano

      Merge Risk: Minimal · up to f72ca

      The change preserves the intended computed-atom comparison and notification behavior, with no merge-blocking risk established.

      Pre-merge checks: Passed 4 · Failed 1
      Failed Failed checks: 1 warning
      Check nameStatusExplanationResolution
      Docstring CoverageWarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within survi…Write docstrings for the functions missing them to satisfy the coverage threshold.
      Passed Passed checks: 4 passed
      Check nameStatusExplanation
      Description checkPassedThe description follows the required template. It explains the change, cause, fix, tests, checklist status, and release impact. It also includes a changeset reference.
      Title checkPassedThe title clearly and concisely describes the main change: running atom comparisons when the previous value is undefined.
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      Full details: Docstring Coverage

      Explanation

      Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 2 files.

      Resolution

      Write docstrings for the functions missing them to satisfy the coverage threshold.

      Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.

Sign in to join this conversation.