Skip to content

Conversation

@dmaskasky
Copy link
Member

@dmaskasky dmaskasky commented Jul 13, 2025

Summary

In Jotai, reactive state is an atom + a store. To scope a value, we can either clone the atom or replace the store. Replacing the store has its own complexities but could be a viable long-term solution. For now, we will continue with the approach that clones atoms. For this to work, we need to ensure that unscoped derived atoms do not needlessly recompute.

Derived atoms that are not explicitly scoped or inherited can either be unscoped or consumer atoms.

To differentiate between consumer and unscoped atoms, we use the following piece of information.
consumer atoms are atoms that read:

  1. explicit scoped atoms
  2. other consumer atoms (besides self)
  3. inherited explicit atoms
  4. inherited consumer atoms

Before a consumer atom has first been read, we cannot yet determine whether it is a consumer atom or not. This is currently an UNSOLVED PROBLEM.

Goal

unscoped atoms should not recompute in separate scopes.

The new code should address #25 and #36.

TODO

  1. figure out how determine a consumer atom on first read. Idea: determine this in flushPending to decide whether to notify listeners. Note: this would not address the atomWithObservable bug as that atom has a side-effect in the read function.

  2. fix tests

  3. fix INF loop with getAtom for nested scope providers.

Open Questions

  1. Should all scopes share invalidatedAtoms, changedAtoms, and storeHooks?

@dmaskasky dmaskasky changed the title fix: full rewrite of ScopeProvider to address known issues [WIP] Refactor to use the new jotai store exposed internal methods (Attempt 11) Jul 13, 2025
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 13, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

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