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

chore: bump up jotai-effect version to v2 #10262

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from

Conversation

renovate[bot]
Copy link
Contributor

@renovate renovate bot commented Feb 18, 2025

This PR contains the following updates:

Package Change Age Adoption Passing Confidence
jotai-effect (source) ^1.0.5 -> ^2.0.0 age adoption passing confidence

Release Notes

jotaijs/jotai-effect (jotai-effect)

v2.0.1

Compare Source

v2.0.0

Compare Source

We’re excited to announce the release of jotai-effect v2, which brings a single but significant change to the core API: atomEffect now runs synchronously whenever it mounts or its dependencies change. This update improves consistency, helps avoid race conditions, and keeps related state changes in sync.


What’s New?

Synchronous atomEffect
  • In v1, atomEffect would run asynchronously in the next microtask.
  • In v2, atomEffect runs synchronously on mount and whenever the dependencies it uses have changed.
  • Batching is still supported when you update multiple dependencies in a single writable atom. The effect runs only after that writable atom has finished all its updates, preventing partial updates or intermediate states.

Example:

const syncEffect = atomEffect((get, set) => {
  get(someAtom)
  set(anotherAtom)
})

const store = createStore()
store.set(someAtom, (v) => v + 1)
// The effect above runs immediately, so anotherAtom is updated in the same microtask
console.log(store.get(anotherAtom)) // Updated by atomEffect synchronously

When someAtom is updated, the effect runs immediately, updating anotherAtom in the same turn. If you update multiple atoms in the same writable atom, these changes are batched together, and atomEffect runs after those updates complete.


Migration Guide

For most users, no change is required. If you depended on the old microtask delay or cross-atom batching, read on.

1. Adding back the microtask delay

If your logic explicitly relied on atomEffect running in a separate microtask, you can reintroduce the delay yourself:

Before (v1)

const effect = atomEffect((get, set) => {
  console.log('effect')
  return () => {
    console.log('cleanup')
  }
})

After (v2)

const effect = atomEffect((get, set) => {
  queueMicrotask(() => {
    console.log('effect')
  })
  return () => {
    queueMicrotask(() => {
      console.log('cleanup')
    })
  }
})
2. Batching updates

In v1, updates to separate atoms were implicitly batched in the next microtask. In v2, batching only occurs within a single writable atom update:

Before (v1)

store.set(atomA, (v) => v + 1)
store.set(atomB, (v) => v + 1)
// atomEffect would 'see' both changes together in the next microtask

After (v2)

const actionAtom = atom(null, (get, set) => {
  set(atomA, (v) => v + 1)
  set(atomB, (v) => v + 1)
})

store.set(actionAtom)
// atomEffect now runs after both updates, in one batch

A Special Thanks to Daishi Kato

I’d like to extend my deepest gratitude to Daishi Kato, author of Jotai. Daishi dedicated months of tireless work to rework and rewrite significant parts of the Jotai core—primarily to empower community library authors such as myself to implement features such as synchronous effects in jotai-effect. His willingness to refine Jotai’s internals and his thoughtfulness in API design made this effort possible. Thank you.

Final Thoughts
  • Most code will just work without any changes.
  • If you have specialized scenarios relying on microtask delays or separate updates to multiple atoms, you’ll need to wrap them in a single writable atom or manually queue the microtask.

We hope these improvements make your state management more predictable and easier to reason about. If you have any issues, please feel free to open a GitHub Discussion. Happy coding!


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 was generated by Mend Renovate. View the repository job log.

@renovate renovate bot added the dependencies Pull requests that update a dependency file label Feb 18, 2025
@graphite-app graphite-app bot requested review from a team February 18, 2025 15:31
Copy link

graphite-app bot commented Feb 18, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@github-actions github-actions bot added the mod:infra Environment related issues and discussions label Feb 18, 2025
Copy link

New, updated, and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@affine-tools/[email protected] None 0 0 B
npm/@affine/[email protected] None 0 0 B
npm/@affine/[email protected] None 0 0 B
npm/@affine/[email protected] None 0 0 B
npm/@capacitor/[email protected] None 0 347 kB dtarnawsky, ionicjs, it_mike_s, ...5 more
npm/@toeverything/[email protected] None 0 0 B
npm/@vanilla-extract/[email protected] environment 0 11.4 kB vanilla-extract-css
npm/[email protected] None 0 901 kB jackworks
npm/[email protected] None 0 8.55 kB lukeed
npm/[email protected] None 0 670 kB iamkun
npm/[email protected] None 0 20.2 kB avoidwork
npm/[email protected] None 0 968 kB tonybrix
npm/[email protected] filesystem, network, unsafe 0 19.9 kB menci

🚮 Removed packages: npm/@atlaskit/[email protected], npm/@atlaskit/[email protected], npm/@atlaskit/[email protected], npm/@blocksuite/[email protected], npm/@floating-ui/[email protected], npm/@lit/[email protected], npm/@lottiefiles/[email protected], npm/@types/[email protected], npm/@types/[email protected], npm/@types/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected]

View full report↗︎

Copy link

codecov bot commented Feb 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 54.21%. Comparing base (dc7e7cf) to head (6726beb).
Report is 1 commits behind head on canary.

Additional details and impacted files
@@           Coverage Diff           @@
##           canary   #10262   +/-   ##
=======================================
  Coverage   54.21%   54.21%           
=======================================
  Files        2332     2332           
  Lines      107675   107675           
  Branches    17799    17799           
=======================================
  Hits        58377    58377           
- Misses      47964    47971    +7     
+ Partials     1334     1327    -7     
Flag Coverage Δ
server-test 78.41% <ø> (ø)
unittest 31.40% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file mod:infra Environment related issues and discussions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants