Skip to content

feat!: replace atom getters with ecosystem function properties #169

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 2 commits into from
Feb 7, 2025

Conversation

bowheart
Copy link
Collaborator

@bowheart bowheart commented Feb 7, 2025

@affects atoms, react

Description

Atom getters are an unnecessary extra concept for learning Zedux. We can make the ecosystem's existing get and getNode methods destructurable function properties that do what atom getters currently do.

Remove the ecosystem.live atom getters and the AtomGettersBase type. Deprecate the AtomGetters type and point it to Ecosystem itself.

Add a circular ecosystem property to ecosystems that references itself. This is for backwards compatibility with the old atom getters type. To upgrade simply stop destructuring that property off the ecosystem and use the ecosystem directly.

Add getNodeOnce and getOnce destructurable function properties to Ecosystem.

Make the Ecosystem get, getNode, and select methods destructurable function properties for full compatibility with the old atom getters type.

Deprecate select, injectAtomSelector, and useAtomSelector. Add overloads to get, getNode, injectAtomValue, injectAtomInstance, useAtomValue, and useAtomInstance that accept selectors.

Deprecate injectAtomGetters in favor of a new injector: injectEcosystem.

Breaking Changes

ecosystem.live is removed. ecosystem.get and ecosystem.getNode are now reactive - they will register graph edges in reactive contexts. This is the exact opposite of their prior use which was specifically to prevent registering graph edges. Instead, ecosystems now have ecosystem.getOnce and ecosystem.getNodeOnce for that.

The ability offered by ecosystem.select of running an atom selector without caching it no longer exists. This is because it was never a good idea - the selector itself wouldn't be cached, but the atom getters it called would still cache all their resolved nodes. This made it very difficult to clean up those transitive deps manually - hard enough that you'd probably just leave that memory leaked.

The recommended approach now is to use ecosystem.getNode, allowing the selector to be cached, then use selectorInstance.destroy() to clear it

// before:
const value = ecosystem.select(mySelector)
doStuffWith(value)
// ❌  have fun figuring out the transitive nodes created by the `.select` call and destroying them 😩 

// after:
const mySelectorNode = ecosystem.getNode(mySelector)
doStuffWith(mySelectorNode.get())
mySelectorNode.destroy() // ✅ leak-free

@bowheart bowheart merged commit 9277efa into master Feb 7, 2025
2 checks passed
@bowheart bowheart deleted the josh/kill-atom-getters branch February 7, 2025 23:14
@bowheart bowheart mentioned this pull request Feb 7, 2025
53 tasks
@bowheart bowheart added this to the Zedux v2 milestone Feb 11, 2025
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