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

[BREAKING] selectAtom does not resolve promises internally #2435

Merged
merged 6 commits into from
Apr 4, 2024

Conversation

dmaskasky
Copy link
Collaborator

@dmaskasky dmaskasky commented Mar 3, 2024

Related Issues or Discussions

https://discord.com/channels/740090768164651008/1213966054125084672

Summary

When selectAtom is based on an atom whose value was originally a promise but was later changed to a value, selectAtom would still return a promise. This is because the original calculation of slice is a promise, so all subsequent calculations of slice must also be promises.

[BREAKING] In this PR selectAtom does not internally resolve promises. Instead this must be handled outside of the utility.

Migration

selectAtom

selectAtom will no longer internally unwrap promises. To migrate to the new api, use the unwrap utility from jotai/utils package.

// suppose we have this
const baseAtom = atom(Promise.resolve({ id: 0, name: 'test' }))

// previously selectAtom would internally unwrap promises.
const idAtom = selectAtom(
  baseAtom,
  ({ name }) => name,
  (prev, curr) => prev.id === curr.id
)

// instead, you need to import `unwrap` from 'jotai/utils' and pass the unwrapped atom 
import { unwrap } from 'jotai/utils'
  ...
const idAtom = selectAtom(
  unwrap(baseAtom),
  ({ name }) => name,
  (prev, curr) => prev.id === curr.id
)

Check List

  • yarn run prettier for formatting code and docs

Copy link

vercel bot commented Mar 3, 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 Mar 29, 2024 0:21am

Copy link

codesandbox-ci bot commented Mar 3, 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 Mar 3, 2024

LiveCodes Preview in LiveCodes

Latest commit: 6b56e74
Last updated: Mar 29, 2024 12:20pm (UTC)

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

See documentations for usage instructions.

@dmaskasky dmaskasky force-pushed the async-identity branch 2 times, most recently from ebe8d57 to e2da3d6 Compare March 3, 2024 23:53
@dmaskasky dmaskasky force-pushed the async-identity branch 2 times, most recently from e36ad6d to 765ca6e Compare March 4, 2024 00:01
@dmaskasky dmaskasky force-pushed the async-identity branch 2 times, most recently from 1ce866e to dc54f36 Compare March 4, 2024 00:02
@dmaskasky dmaskasky closed this Mar 4, 2024
@dmaskasky dmaskasky reopened this Mar 4, 2024
@dmaskasky
Copy link
Collaborator Author

dmaskasky commented Mar 5, 2024

const baseAtom = atom(0)
const selectedAtom = selectAtom(
  baseAtom,
  (v) => v,
  (a, b) => a === b,
) 

// gives
function selectAtom<number, number>(
  anAtom: Atom<number>,
  selector: (v: number, prevSlice?: number | undefined) => number,
  equalityFn?: ((a: number, b: number) => boolean) | undefined
): Atom<number>
const baseAtom = atom(Promise.resolve(0))
const selectedAtom = selectAtom(
  baseAtom,
  (v) => v,
  (a, b) => a === b,
) 

// gives
function selectAtom<Promise<number>, Promise<number>>(
  anAtom: Atom<Promise<number>>,
  selector: (v: Promise<number>, prevSlice?: Promise<number> | undefined) => Promise<number>,
  equalityFn?: ((a: Promise<number>, b: Promise<number>) => boolean) | undefined
): Atom<Promise<number>>```

@dmaskasky dmaskasky changed the title selectAtom promises are not contagious selectAtom does not resolve promises internally Mar 5, 2024
@dmaskasky dmaskasky changed the title selectAtom does not resolve promises internally [BREAKING] selectAtom does not resolve promises internally Mar 5, 2024
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.

Yay! Looks far cleaner and more maintainable.

src/vanilla/utils/selectAtom.ts Outdated Show resolved Hide resolved
@dai-shi
Copy link
Member

dai-shi commented Mar 8, 2024

Would you please write a migration guide in the PR description?
It will be in the release note later.
https://github.com/pmndrs/jotai/releases/tag/v2.2.0 is an example, but it's a minimal guide. More descriptions would be nice.

@dai-shi dai-shi added this to the v2.8.0 milestone Mar 8, 2024
@dai-shi dai-shi merged commit 8847084 into pmndrs:main Apr 4, 2024
34 checks passed
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