Skip to content

Commit ac38969

Browse files
committed
fix: make bb functions to receive the pair of (buildingBlocks, store) consistently
1 parent 96f5dbb commit ac38969

3 files changed

Lines changed: 50 additions & 18 deletions

File tree

src/react/useAtomValue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const createContinuablePromise = <T>(
8585
continuablePromiseMap.set(nextValue, continuablePromise!)
8686
curr = nextValue
8787
nextValue.then(onFulfilled(nextValue), onRejected(nextValue))
88-
registerAbortHandler(buildingBlocks, nextValue, onAbort)
88+
registerAbortHandler(buildingBlocks, store, nextValue, onAbort)
8989
} else {
9090
resolve(nextValue)
9191
}
@@ -94,7 +94,7 @@ const createContinuablePromise = <T>(
9494
}
9595
}
9696
promise.then(onFulfilled(promise), onRejected(promise))
97-
registerAbortHandler(buildingBlocks, promise, onAbort)
97+
registerAbortHandler(buildingBlocks, store, promise, onAbort)
9898
})
9999
continuablePromiseMap.set(promise, continuablePromise)
100100
}

src/vanilla/internals.ts

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,24 @@ type ChangedAtoms = SetLike<AnyAtom>
8989
type Callbacks = SetLike<() => void>
9090

9191
type AtomRead = <Value>(
92+
buildingBlocks: Readonly<BuildingBlocks>,
9293
store: Store,
9394
atom: Atom<Value>,
9495
...params: Parameters<Atom<Value>['read']>
9596
) => Value
9697
type AtomWrite = <Value, Args extends unknown[], Result>(
98+
buildingBlocks: Readonly<BuildingBlocks>,
9799
store: Store,
98100
atom: WritableAtom<Value, Args, Result>,
99101
...params: Parameters<WritableAtom<Value, Args, Result>['write']>
100102
) => Result
101-
type AtomOnInit = <Value>(store: Store, atom: Atom<Value>) => void
103+
type AtomOnInit = <Value>(
104+
buildingBlocks: Readonly<BuildingBlocks>,
105+
store: Store,
106+
atom: Atom<Value>,
107+
) => void
102108
type AtomOnMount = <Value, Args extends unknown[], Result>(
109+
buildingBlocks: Readonly<BuildingBlocks>,
103110
store: Store,
104111
atom: WritableAtomWithOnMount<Value, Args, Result>,
105112
setAtom: (...args: Args) => Result,
@@ -179,11 +186,13 @@ type EnhanceBuildingBlocks = (
179186
type AbortHandlersMap = WeakMapLike<PromiseLike<unknown>, Set<() => void>>
180187
type RegisterAbortHandler = <T>(
181188
buildingBlocks: Readonly<BuildingBlocks>,
189+
store: Store,
182190
promise: PromiseLike<T>,
183191
abortHandler: () => void,
184192
) => void
185193
type AbortPromise = <T>(
186194
buildingBlocks: Readonly<BuildingBlocks>,
195+
store: Store,
187196
promise: PromiseLike<T>,
188197
) => void
189198
type StoreEpochHolder = [n: EpochNumber]
@@ -417,14 +426,26 @@ function initializeStoreHooks(storeHooks: StoreHooks): Required<StoreHooks> {
417426
// Main functions
418427
//
419428

420-
const BUILDING_BLOCK_atomRead: AtomRead = (_store, atom, ...params) =>
421-
atom.read(...params)
422-
const BUILDING_BLOCK_atomWrite: AtomWrite = (_store, atom, ...params) =>
423-
atom.write(...params)
424-
const BUILDING_BLOCK_atomOnInit: AtomOnInit = (store, atom) =>
429+
const BUILDING_BLOCK_atomRead: AtomRead = (
430+
_buildingBlocks,
431+
_store,
432+
atom,
433+
...params
434+
) => atom.read(...params)
435+
const BUILDING_BLOCK_atomWrite: AtomWrite = (
436+
_buildingBlocks,
437+
_store,
438+
atom,
439+
...params
440+
) => atom.write(...params)
441+
const BUILDING_BLOCK_atomOnInit: AtomOnInit = (_buildingBlocks, store, atom) =>
425442
atom.INTERNAL_onInit?.(store)
426-
const BUILDING_BLOCK_atomOnMount: AtomOnMount = (_store, atom, setAtom) =>
427-
atom.onMount?.(setAtom)
443+
const BUILDING_BLOCK_atomOnMount: AtomOnMount = (
444+
_buildingBlocks,
445+
_store,
446+
atom,
447+
setAtom,
448+
) => atom.onMount?.(setAtom)
428449

429450
const BUILDING_BLOCK_ensureAtomState: EnsureAtomState = (
430451
buildingBlocks,
@@ -442,7 +463,7 @@ const BUILDING_BLOCK_ensureAtomState: EnsureAtomState = (
442463
atomState = { d: new Map(), p: new Set(), n: 0 }
443464
atomStateMap.set(atom, atomState)
444465
storeHooks.i?.(atom)
445-
atomOnInit?.(store, atom)
466+
atomOnInit?.(buildingBlocks, store, atom)
446467
}
447468
return atomState as never
448469
}
@@ -744,15 +765,21 @@ const BUILDING_BLOCK_readAtomState: ReadAtomState = (
744765
if (import.meta.env?.MODE !== 'production') {
745766
storeMutationSet.delete(store)
746767
}
747-
const valueOrPromise = atomRead(store, atom, getter, options as never)
768+
const valueOrPromise = atomRead(
769+
buildingBlocks,
770+
store,
771+
atom,
772+
getter,
773+
options as never,
774+
)
748775
if (import.meta.env?.MODE !== 'production' && storeMutationSet.has(store)) {
749776
console.warn(
750777
'Detected store mutation during atom read. This is not supported.',
751778
)
752779
}
753780
setAtomStateValueOrPromise(buildingBlocks, store, atom, valueOrPromise)
754781
if (isPromiseLike(valueOrPromise)) {
755-
registerAbortHandler(buildingBlocks, valueOrPromise, () =>
782+
registerAbortHandler(buildingBlocks, store, valueOrPromise, () =>
756783
controller?.abort(),
757784
)
758785
const settle = () => {
@@ -861,7 +888,7 @@ const BUILDING_BLOCK_writeAtomState: WriteAtomState = (
861888
}
862889
}
863890
try {
864-
return atomWrite(store, atom, getter, setter, ...args)
891+
return atomWrite(buildingBlocks, store, atom, getter, setter, ...args)
865892
} finally {
866893
isSync = false
867894
}
@@ -947,7 +974,7 @@ const BUILDING_BLOCK_mountAtom: MountAtom = (buildingBlocks, store, atom) => {
947974
}
948975
}
949976
try {
950-
const onUnmount = atomOnMount(store, atom, setAtom)
977+
const onUnmount = atomOnMount(buildingBlocks, store, atom, setAtom)
951978
if (onUnmount) {
952979
mounted!.u = () => {
953980
isSync = true
@@ -1034,7 +1061,7 @@ const BUILDING_BLOCK_setAtomStateValueOrPromise: SetAtomStateValueOrPromise = (
10341061
if (!hasPrevValue || !Object.is(prevValue, atomState.v)) {
10351062
++atomState.n
10361063
if (isPromiseLike(prevValue)) {
1037-
abortPromise(buildingBlocks, prevValue)
1064+
abortPromise(buildingBlocks, store, prevValue)
10381065
}
10391066
}
10401067
}
@@ -1087,6 +1114,7 @@ const BUILDING_BLOCK_storeSub: StoreSub = (
10871114

10881115
const BUILDING_BLOCK_registerAbortHandler: RegisterAbortHandler = (
10891116
buildingBlocks,
1117+
_store,
10901118
promise,
10911119
abortHandler,
10921120
) => {
@@ -1101,7 +1129,11 @@ const BUILDING_BLOCK_registerAbortHandler: RegisterAbortHandler = (
11011129
abortHandlers.add(abortHandler)
11021130
}
11031131

1104-
const BUILDING_BLOCK_abortPromise: AbortPromise = (buildingBlocks, promise) => {
1132+
const BUILDING_BLOCK_abortPromise: AbortPromise = (
1133+
buildingBlocks,
1134+
_store,
1135+
promise,
1136+
) => {
11051137
const abortHandlersMap = buildingBlocks[25]
11061138
const abortHandlers = abortHandlersMap.get(promise)
11071139
abortHandlers?.forEach((fn) => fn())

tests/vanilla/storedev.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const createDevStore = (): INTERNAL_Store & DevStore => {
3535
undefined,
3636
storeHooks,
3737
undefined,
38-
(_store, atom, get, set, ...args) => {
38+
(_buildingBlocks, _store, atom, get, set, ...args) => {
3939
if (inRestoreAtom) {
4040
return set(atom, ...(args as any))
4141
}

0 commit comments

Comments
 (0)