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

fix(utils): make 'unwrap' and 'loadable' able to access async atom's resolved value immediately #2417

Conversation

iwoplaza
Copy link
Contributor

Summary

In our project, we had the need to await on a secure store to load proper auth information before initializing the rest of the app, and as soon as that atom resolved, we had to check whether we were previously logged in, or logged out.

const secureAuthStateAtom = atom(/* syncing with the async secure store */);

const userIdentityAtom = atom(/* fetching user identity from our backend, makes sure the auth information is correct */);

function usePrepare(onReady: () => void) {
  const store = useStore();

  useEffect(() => {
    // making sure the effect is ran only once
    // ...
    
    (async () => {
      await store.get(secureAuthStateAtom);

      // UNEXPECTED BEHAVIOR HERE, returns 'undefined', the pending value, not the resolved value.
      const userIdentity = store.get(unwrap(userIdentityAtom));

      if (userIdentity) {
        // we are logged in
      } else {
        // we are not logged in
      }
    })();
  }, []);
}

The same happens with the loadable utility. I managed to write a test for it and implement a fix, hope it does not stray to heavy into the 'experimental' promise usage, or does not break anything else unintentionally.

Check List

  • yarn run prettier for formatting code and docs

Copy link

vercel bot commented Feb 23, 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 Feb 28, 2024 2:42am

Copy link

codesandbox-ci bot commented Feb 23, 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 Feb 23, 2024

LiveCodes Preview in LiveCodes

Latest commit: ed59ecc
Last updated: Feb 28, 2024 2:41am (UTC)

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

See documentations for usage instructions.

Copy link

LiveCodes Preview in LiveCodes

Latest commit: c6e4a55
Last updated: Feb 23, 2024 10:16pm (UTC)

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

See documentations for usage instructions.

@dai-shi
Copy link
Member

dai-shi commented Feb 24, 2024

Thanks for the suggestion. Yeah, it depends on the internal promise behavior, but I think it's a good capability to add.
Unfortunately, the coding style is slightly off from my preference (and, especially, we can't let react depend on utils, because it's a different bundle), so let me add some commit for refactor.

@dai-shi dai-shi changed the title fix: make 'unwrap' and 'loadable' able to access async atom's resolved value immediately fix(utils): make 'unwrap' and 'loadable' able to access async atom's resolved value immediately Feb 24, 2024
@iwoplaza
Copy link
Contributor Author

No worries! I failed to notice the problem with separate bundles. Thanks for a fast response 🐸

@dai-shi dai-shi merged commit f8731b0 into pmndrs:main Feb 28, 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