feat: add no-scope api to ScopeProvider #35
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
Unscoped derived atoms must be copied in a scope in case they depend on atoms that are scoped. This copy causes read side-effects such as fetch to rerun which may be undesirable. There is no way for developers to indicate that a derived atom does not depend on scoped atoms in the current api.
Proposed Solution
This PR proposes a new prop for ScopeProvider,
noScope: Iterable<AnyAtom>
. Passing atoms to this prop will declare them to be not dependent on any scoped atoms.Behavior
Fixes:
Discussion
1. Should we instead export a utility to set this globally?
I think I'm still in favor of a noScope property on the ScopeProvider as it provides greater specificity.
2. Should implicit dependencies adhere to noScope?
Should atomA be implicit scoped in the following example?
3. 💡 Another idea
Jotai exposes an api that describes the atom's current dependencies and notifies when dependencies change. Jotai Scope will determine if any of those dependencies are scoped to determine whether the atom needs to be copied or not.
4. 💡 Yet another idea
An atom's Getter and Setter are defined by the store and can be overwritten by jotai-scope. This eliminates the for copying unscoped derived atoms.