Skip to content

fix reactivity in computeds#69

Merged
treardon17 merged 2 commits into
mainfrom
fix/observer-reactivity
Jan 20, 2026
Merged

fix reactivity in computeds#69
treardon17 merged 2 commits into
mainfrom
fix/observer-reactivity

Conversation

@treardon17
Copy link
Copy Markdown
Collaborator

@treardon17 treardon17 commented Jan 20, 2026

Type Description Icon Changelog
fix Fixes an issue 🐛

Fix: Dependency tracking lost when creating reactive objects during computation

Problem

When a computed/observer creates and modifies reactive objects during its execution, dependencies on the original reactive sources were being incorrectly cleared.

This occurred because:

  1. Computed accesses sourceAtom.value → registers dependency under key 'value'
  2. Computed creates a new atom and reads from it → registers another 'value' dependency
  3. Computed writes to the new atom → triggers trackerChanged(newAtom, 'value')
  4. trackerChanged called observerClear(obs, 'value') unconditionally
  5. This cleared all 'value' dependencies, including the original sourceAtom

This pattern is common with reactive decorators, where each property uses an internal atom with 'value' as the tracking key.

Solution

Only call observerClear when the observer is NOT currently running. If it's mid-execution, it's still collecting dependencies and we must not clear them.

if (!isObserverRunning(obs)) {
  observerClear(obs, key);
}

Added a WeakSet to track running observers for O(1) lookup performance.

Changes

  • src/reactivity/Observer.ts: Added isObserverRunning() helper and RUNNING_OBSERVERS WeakSet
  • src/reactivity/global.ts: Guard observerClear call with isObserverRunning check
  • src/reactivity/spec/observer.spec.ts: Added 3 regression tests for the read-then-write pattern

Test Plan

  • All 399 existing tests pass
  • New regression tests fail without fix, pass with fix
  • Manually verified fix resolves the original issue in consuming application

📚 Bookkeeping:

Testing (if applicable):

  • Ran/wrote unit tests for this

Checklist

  • Assigned PR to myself
  • Added at least 1 person on the team as reviewer
  • Release Notes: PRs types that have the 🗒️ next to them also require release notes to be added to the CHANGELOG.md

@treardon17 treardon17 requested a review from bitencode January 20, 2026 23:08
@treardon17 treardon17 self-assigned this Jan 20, 2026
Copy link
Copy Markdown
Collaborator

@bitencode bitencode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Thanks! Looks good 😄

@treardon17 treardon17 merged commit c88a9cf into main Jan 20, 2026
6 checks passed
@treardon17 treardon17 deleted the fix/observer-reactivity branch January 20, 2026 23:24
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