Skip to content

Commit d6e7bd3

Browse files
committed
make BUILDING_BLOCK_atomOnInit pure again
1 parent 7c10e57 commit d6e7bd3

File tree

1 file changed

+52
-11
lines changed

1 file changed

+52
-11
lines changed

src/vanilla/internals.ts

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,21 @@ type ChangedAtoms = SetLike<AnyAtom>
8383
type Callbacks = SetLike<() => void>
8484

8585
type AtomRead = <Value>(
86+
buildingBlocks: Readonly<BuildingBlocks>,
8687
atom: Atom<Value>,
8788
...params: Parameters<Atom<Value>['read']>
8889
) => Value
8990
type AtomWrite = <Value, Args extends unknown[], Result>(
91+
buildingBlocks: Readonly<BuildingBlocks>,
9092
atom: WritableAtom<Value, Args, Result>,
9193
...params: Parameters<WritableAtom<Value, Args, Result>['write']>
9294
) => Result
93-
type AtomOnInit = <Value>(atom: Atom<Value>) => void
95+
type AtomOnInit = <Value>(
96+
buildingBlocks: Readonly<BuildingBlocks>,
97+
atom: Atom<Value>,
98+
) => void
9499
type AtomOnMount = <Value, Args extends unknown[], Result>(
100+
buildingBlocks: Readonly<BuildingBlocks>,
95101
atom: WritableAtom<Value, Args, Result>,
96102
setAtom: (...args: Args) => Result,
97103
) => OnUnmount | void
@@ -380,6 +386,36 @@ function initializeStoreHooks(storeHooks: StoreHooks): Required<StoreHooks> {
380386
// Main functions
381387
//
382388

389+
const BUILDING_BLOCK_atomRead: AtomRead = (_buildingBlocks, atom, ...params) =>
390+
atom.read(...params)
391+
const BUILDING_BLOCK_atomWrite: AtomWrite = (
392+
_buildingBlocks,
393+
atom,
394+
...params
395+
) => atom.write(...params)
396+
const BUILDING_BLOCK_atomOnInit: AtomOnInit = (buildingBlocks, atom) => {
397+
const storeGet = buildingBlocks[21]
398+
const storeSet = buildingBlocks[22]
399+
const storeSub = buildingBlocks[23]
400+
const store: Store = {
401+
get(atom) {
402+
return storeGet(buildingBlocks, atom)
403+
},
404+
set(atom, ...args) {
405+
return storeSet(buildingBlocks, atom, args)
406+
},
407+
sub(atom, listener) {
408+
return storeSub(buildingBlocks, atom, listener)
409+
},
410+
}
411+
return atom.INTERNAL_onInit?.(store)
412+
}
413+
const BUILDING_BLOCK_atomOnMount: AtomOnMount = (
414+
_buildingBlocks,
415+
atom,
416+
setAtom,
417+
) => atom.onMount?.(setAtom)
418+
383419
const BUILDING_BLOCK_ensureAtomState: EnsureAtomState = (
384420
buildingBlocks,
385421
atom,
@@ -395,7 +431,7 @@ const BUILDING_BLOCK_ensureAtomState: EnsureAtomState = (
395431
atomState = { d: new Map(), p: new Set(), n: 0 }
396432
atomStateMap.set(atom, atomState)
397433
storeHooks.i?.(atom)
398-
atomOnInit?.(atom)
434+
atomOnInit?.(buildingBlocks, atom)
399435
}
400436
return atomState as never
401437
}
@@ -657,7 +693,12 @@ const BUILDING_BLOCK_readAtomState: ReadAtomState = (buildingBlocks, atom) => {
657693
if (import.meta.env?.MODE !== 'production') {
658694
storeMutationSet.delete(buildingBlocks)
659695
}
660-
const valueOrPromise = atomRead(atom, getter, options as never)
696+
const valueOrPromise = atomRead(
697+
buildingBlocks,
698+
atom,
699+
getter,
700+
options as never,
701+
)
661702
if (
662703
import.meta.env?.MODE !== 'production' &&
663704
storeMutationSet.has(buildingBlocks)
@@ -775,7 +816,7 @@ const BUILDING_BLOCK_writeAtomState: WriteAtomState = (
775816
}
776817
}
777818
try {
778-
return atomWrite(atom, getter, setter, ...args)
819+
return atomWrite(buildingBlocks, atom, getter, setter, ...args)
779820
} finally {
780821
isSync = false
781822
}
@@ -860,7 +901,7 @@ const BUILDING_BLOCK_mountAtom: MountAtom = (buildingBlocks, atom) => {
860901
}
861902
}
862903
try {
863-
const onUnmount = atomOnMount(atom, setAtom)
904+
const onUnmount = atomOnMount(buildingBlocks, atom, setAtom)
864905
if (onUnmount) {
865906
mounted!.u = () => {
866907
isSync = true
@@ -1022,7 +1063,7 @@ function getBuildingBlocks(store: Store): Readonly<BuildingBlocks> {
10221063
}
10231064

10241065
function buildStore(...buildArgs: Partial<BuildingBlocks>): Store {
1025-
const store = {
1066+
const store: Store = {
10261067
get(atom) {
10271068
return storeGet(buildingBlocks, atom)
10281069
},
@@ -1032,7 +1073,7 @@ function buildStore(...buildArgs: Partial<BuildingBlocks>): Store {
10321073
sub(atom, listener) {
10331074
return storeSub(buildingBlocks, atom, listener)
10341075
},
1035-
} as Store
1076+
}
10361077

10371078
const buildingBlocks = (
10381079
[
@@ -1045,10 +1086,10 @@ function buildStore(...buildArgs: Partial<BuildingBlocks>): Store {
10451086
new Set(), // unmountCallbacks
10461087
{}, // storeHooks
10471088
// atom interceptors
1048-
(atom, ...params) => atom.read(...params),
1049-
(atom, ...params) => atom.write(...params),
1050-
(atom) => atom.INTERNAL_onInit?.(store),
1051-
(atom, setAtom) => atom.onMount?.(setAtom),
1089+
BUILDING_BLOCK_atomRead,
1090+
BUILDING_BLOCK_atomWrite,
1091+
BUILDING_BLOCK_atomOnInit,
1092+
BUILDING_BLOCK_atomOnMount,
10521093
// building-block functions
10531094
BUILDING_BLOCK_ensureAtomState,
10541095
BUILDING_BLOCK_flushCallbacks,

0 commit comments

Comments
 (0)