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

refactor(store): Replace runWithTransactions to flushCallbacks #2946

Merged
merged 4 commits into from
Jan 19, 2025

Conversation

dmaskasky
Copy link
Collaborator

@dmaskasky dmaskasky commented Jan 17, 2025

Summary

Current implementation with runWithTransactions uses a single synchronous callback (fn) invocation followed by conditionally running store hooks. For some flows, this code reduces to simply calling fn. The states in which hooks should be called are well defined so refactoring to synchronously calling a finalizer (flushCallbacks) is introduced in this PR.

A side-effect of this PR is it resolves several incompatibilities with jotai-effect.

Details

flushCallbacks is called at the end of 7 flows:

  1. writeAtom
  2. writeAtomState setter async
  3. subscribe
  4. unsubscribe
  5. readAtomState getter async
  6. promise complete
  7. onMount setAtom async

Check List

  • pnpm run fix:format for formatting code and docs

@dmaskasky dmaskasky requested a review from dai-shi January 17, 2025 19:37
Copy link

vercel bot commented Jan 17, 2025

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 Jan 19, 2025 10:22am

Copy link

codesandbox-ci bot commented Jan 17, 2025

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

pkg-pr-new bot commented Jan 17, 2025

Open in Stackblitz

More templates

npm i https://pkg.pr.new/jotai@2946

commit: b059877

Copy link

github-actions bot commented Jan 17, 2025

LiveCodes Preview in LiveCodes

Latest commit: b059877
Last updated: Jan 19, 2025 10:21am (UTC)

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

See documentations for usage instructions.

@dmaskasky dmaskasky changed the title simplify store Replace runWithTransactions to flushCallbacks finalizer for Improved Flow Control and Compatibility Jan 17, 2025
@dai-shi dai-shi added this to the v2.11.1 milestone Jan 18, 2025
@dai-shi dai-shi changed the title Replace runWithTransactions to flushCallbacks finalizer for Improved Flow Control and Compatibility refactor(store): Replace runWithTransactions to flushCallbacks Jan 18, 2025
const visited = new WeakSet<AtomState>()
const stack: AtomState[] = [atomState]
while (stack.length) {
const aState = stack.pop()!
if (!visited.has(aState)) {
visited.add(aState)
for (const [d, s] of getMountedOrPendingDependents(aState)) {
invalidatedAtoms.set(d, s.n)
stack.push(s)
if (!invalidatedAtoms.has(d)) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you create a test that fails without this condition?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Its a performance optimization. Transitive, dependents of dependents of A are dependents of A.

Copy link
Member

Choose a reason for hiding this comment

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

Hm, if it's a performance optimization, it will be difficult to write a test.

@dai-shi
Copy link
Member

dai-shi commented Jan 19, 2025

This looks almost good to me, but for my preference, #2950 is a follow-up PR.

@dai-shi dai-shi merged commit 81131dc into pmndrs:main Jan 19, 2025
44 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