Skip to content

Bug fix: Atom effect receives context with unintended scope #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 14, 2025

Conversation

ra1028
Copy link
Owner

@ra1028 ra1028 commented May 12, 2025

Pull Request Type

  • Bug fix
  • New feature
  • Refactoring
  • Documentation update
  • Chore

Issue for this PR

Link: #178

@ra1028 ra1028 marked this pull request as ready for review May 14, 2025 10:28
@Copilot Copilot AI review requested due to automatic review settings May 14, 2025 10:28
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug where the atom effect received an unintended context by ensuring that updates and side‐effects always use the initialized scope context. Key changes include:

  • Adding a new test (testEffectCrossScopeBoundary) under a compiler condition to exercise cross‐scope behavior.
  • Updating StoreContext functions to call switchContext with the atom’s initialized scope during state updates, refresh, reset, and transactional operations.
  • Renaming a test function from testTransitiveUpdate to testUpdatePropagation and updating associated comments for clarity.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
Tests/AtomsTests/Core/StoreContextTests.swift Added a test for cross-scope behavior and renamed a test function.
Sources/Atoms/Core/StoreContext.swift Revised update, refresh, reset, and release implementations to use switchContext with the correct scope.
Sources/Atoms/AtomScope.swift Updated documentation to clarify that only atoms initialized in the current scope are observed.
Comments suppressed due to low confidence (1)

Tests/AtomsTests/Core/StoreContextTests.swift:1295

  • [nitpick] Consider renaming the local helper function 'assert' to a more descriptive name (e.g. 'verifyAtomState') to avoid confusion with XCTest's assertions.
func assert<Node: Atom>(

@@ -640,4 +643,12 @@ private extension StoreContext {
observer.onUpdate(snapshot)
}
}

func switchContext(scope: Scope?) -> StoreContext {
Copy link
Preview

Copilot AI May 14, 2025

Choose a reason for hiding this comment

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

Consider adding a doc comment to switchContext to explain its purpose, how it constructs a new context with the provided scope, and its role in ensuring the correct update propagation.

Copilot uses AI. Check for mistakes.

@@ -306,22 +317,27 @@ private extension StoreContext {
updatedScopes[currentScope.key] = currentScope
}

func transitiveUpdate(for key: AtomKey, cache: some AtomCacheProtocol) {
func updatePropagation(for key: AtomKey, cache: some AtomCacheProtocol) {
Copy link
Preview

Copilot AI May 14, 2025

Choose a reason for hiding this comment

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

Review the inline comment 'Overridden atoms don't get updated transitively.' and update it to reflect the new terminology ('propagation') to improve clarity and consistency.

Copilot uses AI. Check for mistakes.

@ra1028 ra1028 merged commit 9e59598 into main May 14, 2025
8 checks passed
@ra1028 ra1028 deleted the fix/atom-context-scope branch May 14, 2025 10:30
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.

1 participant