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(utils): atomFamily supports getParams and unstable_listen api #2685

Merged
merged 5 commits into from
Aug 7, 2024

Conversation

dmaskasky
Copy link
Collaborator

@dmaskasky dmaskasky commented Jul 31, 2024

Related Bug Reports or Discussions

Past ideas

idea1: #2678
idea2: #2679
idea3: #2681
idea4: #2683

Fixes #2056
Fixes jotaijs/jotai-scope#50

Summary

jotai-scope is trying to support scoping atomFamily.

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

This PR demonstrates one approach in which the atomFamily supports getParams and unstable_listen.

The following pseudo code represents logic that would live in jotai-scope.

function handleChange({ type, atom }) {
  if (type === 'CREATE') {
    atomSet.add(atom)
  } else {
    atomSet.delete(atom)
  }
}

for (const atomFamily of atomFamilies) {
  atomFamily.unstable_listen(handleChange)
  for (const param of atomFamily.getParams()) {
    atomSet.add(atomFamily(param))
  }
}
  ...
const isExplicitlyScoped = atomSet.has(atom)

Check List

  • pnpm run prettier for formatting code and docs

Copy link

vercel bot commented Jul 31, 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 31, 2024 5:12pm

@dmaskasky dmaskasky requested a review from dai-shi July 31, 2024 07:35
@dmaskasky dmaskasky marked this pull request as draft July 31, 2024 07:35
Copy link

codesandbox-ci bot commented Jul 31, 2024

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

github-actions bot commented Jul 31, 2024

LiveCodes Preview in LiveCodes

Latest commit: 48ed9fa
Last updated: Jul 31, 2024 5:12pm (UTC)

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

See documentations for usage instructions.

Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

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

It looks good to me. Added a few comments.

src/vanilla/utils/atomFamily.ts Show resolved Hide resolved
src/vanilla/utils/atomFamily.ts Outdated Show resolved Hide resolved
Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
@dmaskasky
Copy link
Collaborator Author

It looks good to me. Added a few comments.

Ready to merge.

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.

Difficulty Managing Dynamic atomFamily Instances in ScopeProvider
2 participants