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

idea4: atomFamily supports has #2683

Closed
wants to merge 1 commit into from

Conversation

dmaskasky
Copy link
Collaborator

Related Bug Reports or Discussions

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

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 has for O(1) atom lookup.

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

const isExplicitlyScoped = atomSet.has(atom) || Array.from(atomFamilySet).some(af => af.has(atom))

Check List

  • pnpm run prettier for formatting code and docs

Copy link

vercel bot commented Jul 30, 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 30, 2024 3:58pm

@dmaskasky dmaskasky marked this pull request as draft July 30, 2024 15:50
@dmaskasky dmaskasky requested a review from dai-shi July 30, 2024 15:50
Copy link

codesandbox-ci bot commented Jul 30, 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 30, 2024

LiveCodes Preview in LiveCodes

Latest commit: 3d21928
Last updated: Jul 30, 2024 3:58pm (UTC)

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

See documentations for usage instructions.

@dmaskasky dmaskasky changed the title feat(atomFamily): supports has idea4: atomFamily supports has Jul 30, 2024
@@ -4,6 +4,7 @@ type ShouldRemove<Param> = (createdAt: number, param: Param) => boolean

export interface AtomFamily<Param, AtomType> {
(param: Param): AtomType
has(atom: AtomType): boolean
Copy link
Member

Choose a reason for hiding this comment

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

Thanks. This is exactly what I imagined including the implementation.

I'm a bit hesitant with this new api, though.

  • I don't see other use cases than jotai-scope
  • I would like to see another solution "injecting callback" and compare with it
Suggested change
has(atom: AtomType): boolean
onCreate(callback: (param: Param, atom: AtomType) => void): void
getParams(): Iterator<Param>

Copy link
Member

Choose a reason for hiding this comment

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

"injecting callback"

It requires onRemove too.

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