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 'loadable' update immediate after resolve #2790

Merged
merged 3 commits into from
Nov 11, 2024

Conversation

e7h4n
Copy link
Contributor

@e7h4n e7h4n commented Oct 31, 2024

Summary

This commit aims to update the Loadable state derived from loadable immediately after the Promise resolves.

The original code used the following approach to update the loadable cache, which resulted in a delay of one tick for the cache update:

export function loadable<Value>(anAtom: Atom<Value>): Atom<Loadable<Value>> {
  return memo1(() => {
    // ...
    const derivedAtom = atom(
      (get, { setSelf }) => {
        get(refreshAtom);
        // ...
        promise
          .then(
              // ...
          )
          .finally(setSelf); // <---------- Note that here, .finally is applied to the Promise derived from .then(), not the original promise itself.
        // ...
      },
      (_get, set) => {
        set(refreshAtom, (c) => c + 1);
      },
    );
    // ...
    return atom((get) => get(derivedAtom));
  }, anAtom);
}

The use of .finally as shown above causes the following case to fail:

const delayPromise = new Promise((r) => setTimeout(r, 10));
const store = createStore();
const asyncAtom = atom(delayPromise);
const loadableAtom = loadable(asyncAtom);

await store.get(asyncAtom);

expect(store.get(loadableAtom).state).toBe('hasData'); // <---- Error: it's still loading, although asyncAtom has been resolved.

To resolve this issue, setSelf needs to be executed within the .then() callback.

Check List

  • Ran pnpm run prettier for formatting code and documentation.

Copy link

vercel bot commented Oct 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 Nov 11, 2024 11:06am

Copy link

codesandbox-ci bot commented Oct 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 Oct 31, 2024

LiveCodes Preview in LiveCodes

Latest commit: 2d1b390
Last updated: Nov 11, 2024 11:05am (UTC)

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

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.

This is nice catch!

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.

LGTM. I'll merge this when we prepare the next version.

@dai-shi dai-shi merged commit c883763 into pmndrs:main Nov 11, 2024
39 checks passed
dmaskasky pushed a commit to dmaskasky/jotai that referenced this pull request Nov 14, 2024
* fix(utils): make 'loadable' update immediate after resolve

* fix comments in pmndrs#2790

---------

Co-authored-by: Daishi Kato <[email protected]>
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