Skip to content

breaking(internals): pass buildingBlockContext directly to internals#3280

Closed
dmaskasky wants to merge 11 commits intopmndrs:mainfrom
dmaskasky:pr-buildingblocks-first
Closed

breaking(internals): pass buildingBlockContext directly to internals#3280
dmaskasky wants to merge 11 commits intopmndrs:mainfrom
dmaskasky:pr-buildingblocks-first

Conversation

@dmaskasky
Copy link
Copy Markdown
Collaborator

@dmaskasky dmaskasky commented Mar 30, 2026

Summary

Since 2.14.0, the building block architecture relies on passing the store as the first parameter to internal functions. This store was used as a WeakMap key to access building blocks. WeakMap access is not super performant so in certain heavy applications, performance was impacted.

This PR reworks the building block architecture to directly pass buildingBlocksContext readonly [...buildingBlocks, store] instead of store. This eliminates the WeakMap access per call site.

The breaking change is that BUILDING_BLOCK functions now accept buildingBlocksContext instead of store as the first parameter.

What was done

  • switch internal function signatures and call sites from store-first to buildingBlocksContext-first.
  • update useAtomValue and internals tests to pass buildingBlocksContext instead of store as the first parameter.

Next Steps

  • We should drop TS3.8.3 and TS3.9.7. These versions of TS do not support spreading tuples.

Other Changes

  • WriteAtomState now uses args instead of ...args
  • Reworked types for WithOnInit and WithOnMount
  • added hasOnInit for conditionally calling AtomOnInit
  • dropped getInternalBuildingBlocks

@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 30, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
jotai Ready Ready Preview, Comment Apr 18, 2026 10:29am

Request Review

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci Bot commented Mar 30, 2026

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.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Mar 30, 2026

More templates

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

commit: 990820e

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 30, 2026

LiveCodes Preview in LiveCodes

Latest commit: 990820e
Last updated: Apr 18, 2026 10:29am (UTC)

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

See documentations for usage instructions.

@dmaskasky dmaskasky force-pushed the pr-buildingblocks-first branch from ab0cc38 to f59e61d Compare March 30, 2026 05:34
@dmaskasky dmaskasky changed the title refactor internals to pass building blocks explicitly Breaking: refactor internals to pass building blocks explicitly Mar 30, 2026
@dmaskasky dmaskasky force-pushed the pr-buildingblocks-first branch from f59e61d to 8d2c768 Compare March 30, 2026 06:33
@dmaskasky dmaskasky force-pushed the pr-buildingblocks-first branch from 8d2c768 to 116e6b4 Compare March 30, 2026 06:39
@dmaskasky dmaskasky changed the title Breaking: refactor internals to pass building blocks explicitly breaking: refactor internals to pass building blocks explicitly Mar 30, 2026
@dmaskasky dmaskasky changed the title breaking: refactor internals to pass building blocks explicitly breaking(internals): refactor internals to pass building blocks explicitly Apr 3, 2026
@dmaskasky dmaskasky changed the title breaking(internals): refactor internals to pass building blocks explicitly breaking(internals): pass building blocks directly to internal functions Apr 3, 2026
Comment thread src/vanilla/internals.ts Outdated
Switch internal building-block function signatures and call sites from store-first to buildingBlocks-first, and update buildStore to wire store APIs from frozen building blocks while keeping weak map storage.

Made-with: Cursor
@dmaskasky dmaskasky force-pushed the pr-buildingblocks-first branch from 6a7dcc0 to d968676 Compare April 18, 2026 00:29
@dmaskasky dmaskasky force-pushed the pr-buildingblocks-first branch from e69e971 to 990820e Compare April 18, 2026 10:29
@dmaskasky dmaskasky requested a review from dai-shi April 18, 2026 10:31
@dmaskasky dmaskasky changed the title breaking(internals): pass building blocks directly to internal functions breaking(internals): pass buildingBlockContext directly to internal functions Apr 18, 2026
@dmaskasky dmaskasky changed the title breaking(internals): pass buildingBlockContext directly to internal functions breaking(internals): pass buildingBlockContext directly to internals Apr 18, 2026
@dai-shi
Copy link
Copy Markdown
Member

dai-shi commented Apr 21, 2026

#3293 (comment)

Thanks so much for your experiments and insights!

@dai-shi dai-shi closed this Apr 21, 2026
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