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(vanilla): new store listeners for devtools #1966

Merged
merged 8 commits into from
Jun 12, 2023
Merged

Conversation

dai-shi
Copy link
Member

@dai-shi dai-shi commented May 30, 2023

This supersedes #1965.

Two improvements:

  • It doesn't notify on restore.
  • It comes with flushed set.

Also, we added the notion of "revision" which means we will probably keep improving/changing in the future.

@vercel
Copy link

vercel bot commented May 30, 2023

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 Jun 8, 2023 1:50am

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 30, 2023

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.

Latest deployment of this branch, based on commit f57a26b:

Sandbox Source
React Configuration
React Typescript Configuration
React Browserify Configuration
React Snowpack Configuration
Next.js Configuration
Next.js with custom Babel config Configuration
React with custom Babel config Configuration

@github-actions
Copy link

github-actions bot commented May 30, 2023

Size Change: +619 B (+1%)

Total Size: 66.9 kB

Filename Size Change
dist/system/vanilla.development.js 3.76 kB +203 B (+6%) 🔍
dist/umd/vanilla.development.js 4.55 kB +210 B (+5%) 🔍
dist/vanilla.js 4.46 kB +206 B (+5%) 🔍
ℹ️ View Unchanged
Filename Size
dist/babel/plugin-debug-label.js 916 B
dist/babel/plugin-react-refresh.js 1.15 kB
dist/babel/preset.js 1.39 kB
dist/index.js 229 B
dist/react.js 1.05 kB
dist/react/utils.js 1.23 kB
dist/system/babel/plugin-debug-label.development.js 1.09 kB
dist/system/babel/plugin-debug-label.production.js 764 B
dist/system/babel/plugin-react-refresh.development.js 1.29 kB
dist/system/babel/plugin-react-refresh.production.js 935 B
dist/system/babel/preset.development.js 1.57 kB
dist/system/babel/preset.production.js 1.14 kB
dist/system/index.development.js 236 B
dist/system/index.production.js 167 B
dist/system/react.development.js 1.17 kB
dist/system/react.production.js 701 B
dist/system/react/utils.development.js 677 B
dist/system/react/utils.production.js 424 B
dist/system/utils.development.js 241 B
dist/system/utils.production.js 175 B
dist/system/vanilla.production.js 1.91 kB
dist/system/vanilla/utils.development.js 4.46 kB
dist/system/vanilla/utils.production.js 2.72 kB
dist/umd/babel/plugin-debug-label.development.js 1.07 kB
dist/umd/babel/plugin-debug-label.production.js 850 B
dist/umd/babel/plugin-react-refresh.development.js 1.29 kB
dist/umd/babel/plugin-react-refresh.production.js 1.02 kB
dist/umd/babel/preset.development.js 1.53 kB
dist/umd/babel/preset.production.js 1.25 kB
dist/umd/index.development.js 371 B
dist/umd/index.production.js 321 B
dist/umd/react.development.js 1.18 kB
dist/umd/react.production.js 785 B
dist/umd/react/utils.development.js 1.4 kB
dist/umd/react/utils.production.js 1.01 kB
dist/umd/utils.development.js 386 B
dist/umd/utils.production.js 334 B
dist/umd/vanilla.production.js 2.11 kB
dist/umd/vanilla/utils.development.js 5.18 kB
dist/umd/vanilla/utils.production.js 3.18 kB
dist/utils.js 236 B
dist/vanilla/utils.js 5.05 kB

compressed-size-action

@dai-shi
Copy link
Member Author

dai-shi commented Jun 4, 2023

@arjunvegda check out the new one!

Copy link
Collaborator

@arjunvegda arjunvegda 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 great! The write and restore functionality is working as expected.

Is there a way to trigger write-async when a promise is resolved? Reference PR #1920

src/vanilla/store.ts Outdated Show resolved Hide resolved
src/vanilla/store.ts Outdated Show resolved Hide resolved
src/vanilla/store.ts Outdated Show resolved Hide resolved
Co-authored-by: Arjun Vegda <14841132+arjunvegda@users.noreply.github.com>
@dai-shi
Copy link
Member Author

dai-shi commented Jun 5, 2023

Is there a way to trigger write-async when a promise is resolved? Reference PR #1920

Our change here is about store listeners. Promise resolution isn't in it. (Our mental model is, Jotai store doesn't care about promise values.)
Basically, I still think you can handle promise resolution on your end.
We could add some kind of "promise listeners", but it just offload the task from jotai-devtools to jotai. (I mean, I don't think it adds any new capability. I might be missing something though. Maybe, if we want to track continuePromise. 🤔 )

@arjunvegda
Copy link
Collaborator

Our mental model is, Jotai store doesn't care about promise values.

I get your perspective now! I considered promise values as a first-class citizen for Jotai, similar to any other non-promise value. So to me, it's natural to fire an event (state change for example) when the async value in the store gets updated, similar to how we do it for any other non-promise value. 😅

jotai-devtools sounds like a better place for it then! 🙌

Copy link
Collaborator

@arjunvegda arjunvegda left a comment

Choose a reason for hiding this comment

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

LGTM!

@dai-shi
Copy link
Member Author

dai-shi commented Jun 6, 2023

Yeah, only the exception is that it wraps a promise to match with react behavior, like continuePromise, resolvePromise, abortPromise and so on. Now I think, ideally, this should go into jotai/react which would simplify jotai itself. We may try that road in the future, when we know more about react use best practice which doesn't exist yet.

@dai-shi dai-shi changed the title revisit store listeners feat(vanilla): new store listeners for devtools Jun 6, 2023
@dai-shi dai-shi marked this pull request as ready for review June 6, 2023 01:06
@dai-shi
Copy link
Member Author

dai-shi commented Jun 6, 2023

@arjunvegda Can you try if/how flushed set helps in devtools?

@dai-shi dai-shi added this to the v2.2.0 milestone Jun 6, 2023
@arjunvegda
Copy link
Collaborator

@arjunvegda Can you try if/how flushed set helps in devtools?

This new API and access to flushed is a great addition. I played around with this API in devtools this weekend. I don't plan to use it immediately, perhaps after Time Travel is released. But here are some ideas

  • Create atom based subscription model for time travel to update the diff map instead of re-creating one every time there is a change
  • Construct a map of atoms using these "atom level" subscriptions on the atom viewer tab and allow users to see diffs by atom
  • Maybe use this API to build a stack trace whenever there is a write event

This might be a stretch, but it'd be even nicer [in the future] to pre-hook for writing as well, maybe pre-write or before-write. We could use those events to save stack traces on dev tools before the write occurs.

@dai-shi
Copy link
Member Author

dai-shi commented Jun 6, 2023

tbh, I thought about pre-write but I'm not a big fan of it.
I would like to offload as much as possible to jotai-devtools and make jotai as slim as possible.

So, an ideal way would wrap store.

const store = devtools(createStore());

const App = () => {
  return (
    <Provider store={store}>
      <DevTools store={store} />
      {/* your app */}
    </Provider>
  );
};

// OR

const store = createStore();

const App = () => {
  return (
    <ProviderWithDevTools store={store}>
      {/* your app */}
    </ProviderWithDevTools>
  );
};

This doesn't work with the default store, so we would need a hook for it. 🤔 (Yeah, setDefaultStore may make sense.)

I wonder if we can eliminate mountedAtom in store.

Copy link
Collaborator

@arjunvegda arjunvegda left a comment

Choose a reason for hiding this comment

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

Thank you!!

@dai-shi dai-shi merged commit bb6006e into main Jun 12, 2023
29 checks passed
@dai-shi dai-shi deleted the revisit-store-listeners branch June 12, 2023 10:20
kodiakhq bot pushed a commit to sullivanpj/open-system that referenced this pull request Jun 26, 2023
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [jotai](https://togithub.com/pmndrs/jotai) | [`2.1.0` -> `2.2.1`](https://renovatebot.com/diffs/npm/jotai/2.1.0/2.2.1) | [![age](https://badges.renovateapi.com/packages/npm/jotai/2.2.1/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/npm/jotai/2.2.1/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/npm/jotai/2.2.1/compatibility-slim/2.1.0)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/npm/jotai/2.2.1/confidence-slim/2.1.0)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>pmndrs/jotai (jotai)</summary>

### [`v2.2.1`](https://togithub.com/pmndrs/jotai/releases/tag/v2.2.1)

[Compare Source](https://togithub.com/pmndrs/jotai/compare/v2.2.0...v2.2.1)

This includes some improvements in `jotai/utils`. Especially, `unstable_unwrap` is getting to be stable.

##### What's Changed

-   feat(utils/useHydrateAtoms) - Optionally rehydrate with force hydrate by [@&#8203;SariSabouh](https://togithub.com/SariSabouh) in [pmndrs/jotai#1990
-   fix(utils): revert atomWithStorage typing by [@&#8203;dai-shi](https://togithub.com/dai-shi) in [pmndrs/jotai#1994
-   fix(utils): improve selectAtom typing by [@&#8203;dai-shi](https://togithub.com/dai-shi) in [pmndrs/jotai#1995
-   fix(utils): improve unstable_unwrap for sometimes async atom by [@&#8203;dai-shi](https://togithub.com/dai-shi) in [pmndrs/jotai#1996
-   fix(utils): unstable_unwrap to support writable atom by [@&#8203;dai-shi](https://togithub.com/dai-shi) in [pmndrs/jotai#1997

##### New Contributors

-   [@&#8203;SariSabouh](https://togithub.com/SariSabouh) made their first contribution in [pmndrs/jotai#1990

**Full Changelog**: pmndrs/jotai@v2.2.0...v2.2.1

### [`v2.2.0`](https://togithub.com/pmndrs/jotai/releases/tag/v2.2.0)

[Compare Source](https://togithub.com/pmndrs/jotai/compare/v2.1.1...v2.2.0)

It includes a few improvements. Some utils are rewritten as there was a misconception when migrating from v1. ESM builds are optimized for Vite users.

#### Migration Guide for `jotai/utils`

##### `atomWithDefault`

```js
// suppose we have this
const asyncAtom = atom(() => Promise.resolve(1))
const countAtom = atomWithDefault((get) => get(asyncAtom))
// and in component
const setCount = useSetAtom(countAtom)

// previously,
setCount((c) => c + 1) // it worked, but it will no longer work

// instead, you need to do this
setCount((countPromise) => countPromise.then((c) => c + 1))
```

##### `atomWithStorage`

```js
// suppose we have async storage
const storage = createJSONStorage(() => AsyncStorage)
const countAtom = atomWithStorage('count-key', 0, storage)
  // in component
  const [count, setCount] = useAtom(countAom)

  // previously, countAtom is a sync atom, so you could update like this:
  setCount((c) => c + 1)

  // with the new version, it becomes async occasionally, so you need to resolve it:
  setCount(async (c) => (await c) + 1)
```

#### What's Changed

-   breaking(utils): predictable atomWithDefault by [@&#8203;dai-shi](https://togithub.com/dai-shi) in [pmndrs/jotai#1939
-   breaking(utils): improve atomWithStorage by [@&#8203;dai-shi](https://togithub.com/dai-shi) in [pmndrs/jotai#1958
-   feat(vanilla): new store listeners for devtools by [@&#8203;dai-shi](https://togithub.com/dai-shi) in [pmndrs/jotai#1966
-   fix(build): mode env for "import" condition" by [@&#8203;dai-shi](https://togithub.com/dai-shi) in [pmndrs/jotai#1978

#### New Contributors

-   [@&#8203;reinierkaper-carewell](https://togithub.com/reinierkaper-carewell) made their first contribution in [pmndrs/jotai#1980

**Full Changelog**: pmndrs/jotai@v2.1.1...v2.2.0

### [`v2.1.1`](https://togithub.com/pmndrs/jotai/releases/tag/v2.1.1)

[Compare Source](https://togithub.com/pmndrs/jotai/compare/v2.1.0...v2.1.1)

This version fixes some issues in edge cases.

#### What's Changed

-   fix(vanilla): Stable promise by [@&#8203;backbone87](https://togithub.com/backbone87) in [pmndrs/jotai#1933
-   fix(vanilla): update atoms with tree structure dependencies (regression from v1) by [@&#8203;dai-shi](https://togithub.com/dai-shi) in [pmndrs/jotai#1959
-   fix: prefer PromiseLike where appropriate by [@&#8203;dai-shi](https://togithub.com/dai-shi) in [pmndrs/jotai#1967

#### New Contributors

-   [@&#8203;blissdev](https://togithub.com/blissdev) made their first contribution in [pmndrs/jotai#1945
-   [@&#8203;hwanyoungChoi](https://togithub.com/hwanyoungChoi) made their first contribution in [pmndrs/jotai#1957
-   [@&#8203;alexhad6](https://togithub.com/alexhad6) made their first contribution in [pmndrs/jotai#1971
-   [@&#8203;backbone87](https://togithub.com/backbone87) made their first contribution in [pmndrs/jotai#1933

**Full Changelog**: pmndrs/jotai@v2.1.0...v2.1.1

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/sullivanpj/open-system).
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.

None yet

2 participants