Skip to content
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

feat(atoms)!: support @atom node type filter in findAll and dehydrate #204

Merged
merged 1 commit into from
Mar 11, 2025

Conversation

bowheart
Copy link
Collaborator

Description

Zedux's "built-in devtools", ecosystem.findAll and ecosystem.dehydrate received some upgrades for v2. But hitherto, there was still no good way to make them return only atoms.

While #201 made all other node types easier to search for by simply passing an @ prefix, it did not give atoms an @atom prefix. I think it still makes sense that these methods should accept @atom specifically.

So: Give both findAll and dehydrate an overload for TS auto completion of the node types. Handle the @atom string specifically.

ecosystem.findAll('@atom') // get a list of all atom nodes
ecosystem.findAll('@selector') // get a list of all selector nodes
ecosystem.findAll('@component') // get a list of all React component nodes
ecosystem.findAll({ include: ['@atom', '@signal'] }) // get a list of all atom or signal nodes
ecosystem.findAll({ exclude: ['@component', '@selector'] }) // get all nodes but components/selectors

ecosystem.dehydrate works exactly the same (but returning an object with dehydrated state values).

Breaking Changes

To better support custom ids where you might have to manually filter nodes yourself, ecosystem.findAll now returns an array instead of an object keyed by node id.

Before, you'd have to use Object.values to get the list of returned nodes for manually iterating. That was annoying. Now this API more closely aligns to document.querySelectorAll which is probably where the *All namesake came from.

// before:
const nodes = Object.values(ecosystem.findAll('my/atom/key')).filter(myCustomFilter)
// after:
const nodes = ecosystem.findAll('my/atom/key').filter(myCustomFilter)

In particular, I did this all the time:

console.log(Object.values(ecosystem.findAll('my/namespace')).map(({ id }) => id))
// now it's:
console.log(ecosystem.findAll('my/namespace').map(({ id }) => id))

Additonally, making the @atom string filter by all atoms is technically a breaking change. It's a best practice to not use the @ character in atom keys/manual id strings so Zedux can add features like this.

@bowheart bowheart force-pushed the josh/find-all-atoms branch from 58654e7 to 5616e7e Compare March 11, 2025 04:23
@bowheart bowheart merged commit b80f762 into master Mar 11, 2025
2 checks passed
@bowheart bowheart deleted the josh/find-all-atoms branch March 11, 2025 15:17
@bowheart bowheart mentioned this pull request Feb 20, 2025
53 tasks
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