Skip to content

Commit

Permalink
expose building blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Feb 4, 2025
1 parent 80f0472 commit 81fb3b3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-multiple-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
sed -i~ "s/resolve('\.\/src\(.*\)\.ts')/resolve('\.\/dist\1.js')/" vitest.config.mts
sed -i~ "s/import { useResetAtom } from 'jotai\/react\/utils'/const { useResetAtom } = require('..\/..\/..\/dist\/react\/utils.js')/" tests/react/utils/useResetAtom.test.tsx
sed -i~ "s/import { RESET, atomWithReducer, atomWithReset } from 'jotai\/vanilla\/utils'/const { RESET, atomWithReducer, atomWithReset } = require('..\/..\/..\/dist\/vanilla\/utils.js')/" tests/react/utils/useResetAtom.test.tsx
perl -i~ -0777 -pe "s/import {[^}]+} from 'jotai\/vanilla\/internals'/const { INTERNAL_buildStore, INTERNAL_createStoreArgs, INTERNAL_initializeStoreHooks, INTERNAL_getStoreArgsRev1: INTERNAL_getStoreArgs } = require('..\/..\/dist\/vanilla\/internals.js')/g" tests/vanilla/store.test.tsx tests/vanilla/derive.test.tsx tests/vanilla/effect.test.ts
perl -i~ -0777 -pe "s/import {[^}]+} from 'jotai\/vanilla\/internals'/const { INTERNAL_buildStore, INTERNAL_createStoreArgs, INTERNAL_initializeStoreHooks, INTERNAL_getStoreArgsRev1: INTERNAL_getStoreArgs, INTERNAL_createStoreArgsRev1: INTERNAL_createStoreArgs } = require('..\/..\/dist\/vanilla\/internals.js')/g" tests/vanilla/store.test.tsx tests/vanilla/derive.test.tsx tests/vanilla/effect.test.ts
- name: Patch for ESM
if: ${{ matrix.build == 'esm' }}
run: |
Expand Down
14 changes: 8 additions & 6 deletions src/vanilla/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,16 +822,17 @@ const createStoreArgs: (...storeArgs: Partial<StoreArgs>) => StoreArgs = (
atomOnMount,
]

const buildStore = (...storeArgs: StoreArgs): Store => {
const [
const buildStore = (
storeArgs: StoreArgs,
[
flushCallbacks,
recomputeInvalidatedAtoms,
readAtomState,
writeAtomState,
mountAtom,
unmountAtom,
] = createBuildingBlocks(storeArgs, () => store as Store)

]: BuildingBlocks,
): Store => {
const readAtom = <Value>(atom: Atom<Value>): Value =>
returnAtomValue(readAtomState(atom))

Expand Down Expand Up @@ -872,11 +873,12 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
// Export internal functions
//

export const INTERNAL_createStoreArgs: typeof createStoreArgs = createStoreArgs
export const INTERNAL_buildStore: typeof buildStore = buildStore
export const INTERNAL_getStoreArgsRev1: typeof getStoreArgs = getStoreArgs
export const INTERNAL_createBuildingBlocksRev1: typeof createBuildingBlocks =
createBuildingBlocks
export const INTERNAL_createStoreArgsRev1: typeof createStoreArgs =
createStoreArgs
export const INTERNAL_getStoreArgsRev1: typeof getStoreArgs = getStoreArgs
export const INTERNAL_initializeStoreHooks: typeof initializeStoreHooks =
initializeStoreHooks

Expand Down
11 changes: 8 additions & 3 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Atom, WritableAtom } from './atom.ts'
import {
INTERNAL_buildStore,
INTERNAL_createStoreArgs,
INTERNAL_createBuildingBlocksRev1 as INTERNAL_createBuildingBlocks,
INTERNAL_createStoreArgsRev1 as INTERNAL_createStoreArgs,
INTERNAL_initializeStoreHooks,
} from './internals.ts'
import type { INTERNAL_AtomState } from './internals.ts'
Expand Down Expand Up @@ -33,7 +34,7 @@ const createDevStoreRev4 = (): INTERNAL_PrdStore & INTERNAL_DevStoreRev4 => {
const storeHooks = INTERNAL_initializeStoreHooks({})
const atomStateMap = new WeakMap()
const mountedAtoms = new WeakMap()
const store = INTERNAL_buildStore(
const storeArgs = INTERNAL_createStoreArgs(
atomStateMap,
mountedAtoms,
new WeakMap(),
Expand All @@ -51,6 +52,8 @@ const createDevStoreRev4 = (): INTERNAL_PrdStore & INTERNAL_DevStoreRev4 => {
(atom, ...params) => atom.unstable_onInit?.(...params),
(atom, ...params) => atom.onMount?.(...params),
)
const buildingBlocks = INTERNAL_createBuildingBlocks(storeArgs, () => store)
const store = INTERNAL_buildStore(storeArgs, buildingBlocks)
const debugMountedAtoms = new Set<Atom<unknown>>()
storeHooks.m.add(undefined, (atom) => {
debugMountedAtoms.add(atom)
Expand Down Expand Up @@ -98,7 +101,9 @@ export const createStore = (): PrdOrDevStore => {
if (import.meta.env?.MODE !== 'production') {
return createDevStoreRev4()
}
const store = INTERNAL_buildStore(...INTERNAL_createStoreArgs())
const storeArgs = INTERNAL_createStoreArgs()
const buildingBlocks = INTERNAL_createBuildingBlocks(storeArgs, () => store)
const store = INTERNAL_buildStore(storeArgs, buildingBlocks)
return store
}

Expand Down
9 changes: 7 additions & 2 deletions tests/vanilla/derive.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { atom, createStore } from 'jotai/vanilla'
import type { Atom } from 'jotai/vanilla'
import {
INTERNAL_buildStore,
INTERNAL_createStoreArgs,
INTERNAL_createBuildingBlocksRev1 as INTERNAL_createBuildingBlocks,
INTERNAL_createStoreArgsRev1 as INTERNAL_createStoreArgs,
INTERNAL_getStoreArgsRev1 as INTERNAL_getStoreArgs,
} from 'jotai/vanilla/internals'

Expand All @@ -18,7 +19,11 @@ const deriveStore = (
const newStoreArgs = INTERNAL_createStoreArgs(
enhanceAtomStateMap(atomStateMap),
)
const derivedStore = (INTERNAL_buildStore as any)(...newStoreArgs)
const buildingBlocks = INTERNAL_createBuildingBlocks(
newStoreArgs,
() => derivedStore,
)
const derivedStore = INTERNAL_buildStore(newStoreArgs, buildingBlocks)
return derivedStore
}

Expand Down
9 changes: 7 additions & 2 deletions tests/vanilla/store.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { atom, createStore } from 'jotai/vanilla'
import type { Atom, Getter, PrimitiveAtom } from 'jotai/vanilla'
import {
INTERNAL_buildStore,
INTERNAL_createStoreArgs,
INTERNAL_createBuildingBlocksRev1 as INTERNAL_createBuildingBlocks,
INTERNAL_createStoreArgsRev1 as INTERNAL_createStoreArgs,
INTERNAL_getStoreArgsRev1 as INTERNAL_getStoreArgs,
} from 'jotai/vanilla/internals'

Expand All @@ -19,7 +20,11 @@ const deriveStore = (
const newStoreArgs = INTERNAL_createStoreArgs(
enhanceAtomStateMap(atomStateMap),
)
const derivedStore = (INTERNAL_buildStore as any)(...newStoreArgs)
const buildingBlocks = INTERNAL_createBuildingBlocks(
newStoreArgs,
() => derivedStore,
)
const derivedStore = INTERNAL_buildStore(newStoreArgs, buildingBlocks)
return derivedStore
}

Expand Down

0 comments on commit 81fb3b3

Please sign in to comment.