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

idea2: add getParams to createAtom #2679

Closed
wants to merge 1 commit into from

Conversation

dmaskasky
Copy link
Collaborator

@dmaskasky dmaskasky commented Jul 29, 2024

Related Bug Reports or Discussions

idea1: #2678
Discussion: #2056

Fixes #
jotaijs/jotai-scope#50

Summary

jotai-scope is trying to support scoping atomFamily.

const fooFamily = atomFamily((id) => atom(id))
<ScopedProvider atoms={[fooFamily]}>{children}</ScopeProvider>

This PR demonstrates one approach in which the atomFamily supports a new getParams function.
The following pseudo code represents logic that would live in jotai-scope. For obvious complexity reasons, I find this solution less favorable to #2678.

const isExplicitlyScoped = atomSet.has(atom) || 
  Array.from(atomSet)
    .filter(isAtomFamily)
    .some((af) => af.getParams().map(af).includes(atom))

Check List

  • pnpm run prettier for formatting code and docs

Copy link

vercel bot commented Jul 29, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
jotai ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 29, 2024 3:27am

Copy link

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.

Copy link

LiveCodes Preview in LiveCodes

Latest commit: 6008aa7
Last updated: Jul 29, 2024 3:27am (UTC)

Playground Link
React demo https://livecodes.io?x=id/DYPJ8VT87

See documentations for usage instructions.

@dmaskasky dmaskasky changed the title feat(atomFamily): add getParams to createAtom idea2: add getParams to createAtom Jul 29, 2024
@dmaskasky dmaskasky requested a review from dai-shi July 29, 2024 03:30
@dmaskasky dmaskasky marked this pull request as draft July 29, 2024 03:31
@@ -47,6 +47,10 @@ export function atomFamily<Param, AtomType extends Atom<unknown>>(
return newAtom
}

createAtom.getParams = () => {
Copy link
Member

Choose a reason for hiding this comment

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

This looks good as it's sometimes requested.

@@ -47,6 +47,10 @@ export function atomFamily<Param, AtomType extends Atom<unknown>>(
return newAtom
}

createAtom.getParams = () => {
return Array.from(atoms.keys())
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should just return the iterator:

Suggested change
return Array.from(atoms.keys())
return atoms.keys()

@dmaskasky
Copy link
Collaborator Author

I don't think this change will satisfy jotai-scope's use-case. Closing this PR for now, we can reopen when a community need arises.

@dmaskasky dmaskasky closed this Jul 30, 2024
@dai-shi
Copy link
Member

dai-shi commented Jul 30, 2024

I think this should be reopened and we consider merging, even if it doesn't work for jotai-scope. But, yeah, we can do it when there's a need.

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