Skip to content

Commit

Permalink
try add new store impl
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Jan 20, 2025
1 parent b48545d commit 705004d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
18 changes: 18 additions & 0 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,24 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
atomState.h?.()
if (isActuallyWritableAtom(atom)) {
const mounted = atomState.m
let setAtom: (...args: unknown[]) => unknown
const createInvocationContext = <T>(fn: () => T) => {
let isSync = true
setAtom = (...args: unknown[]) => {
try {
return writeAtomState(atom, ...args)
} finally {
if (!isSync) {
flushCallbacks()
}
}
}
try {
return fn()
} finally {
isSync = false
}
}
const processOnMount = () => {
let isSync = true
const setAtom = (...args: unknown[]) => {
Expand Down
1 change: 0 additions & 1 deletion tests/syncEffect/syncEffect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ it('should disallow synchronous set.recurse in cleanup', () => {
})
const store = createDebugStore()
store.sub(effectAtom, () => {})
store.set(anotherAtom, increment)
expect(() => store.set(anotherAtom, increment)).toThrowError(
'set.recurse is not allowed in cleanup',
)
Expand Down
15 changes: 6 additions & 9 deletions tests/syncEffect/syncEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function syncEffect(effect: Effect): Atom<void> & { effect: Effect } {
},
() => {},
)
internalAtom.onMount = () => () => {}

internalAtom.unstable_onInit = (store) => {
const ref = store.get(refAtom)
Expand Down Expand Up @@ -116,19 +117,16 @@ export function syncEffect(effect: Effect): Atom<void> & { effect: Effect } {
try {
fromCleanup = true
return cleanup()
} /* catch (error) {
ref.error = error
refresh()
} */ finally {
} finally {
fromCleanup = false
runCleanup = undefined
}
}
}
} /* catch (error) {
} catch (error) {
ref.error = error
refresh()
} */ finally {
throw error
} finally {
Array.from(deps.keys(), ref.get!)
--inProgress
}
Expand Down Expand Up @@ -169,7 +167,6 @@ export function syncEffect(effect: Effect): Atom<void> & { effect: Effect } {
syncEffectChannel.add(runEffect)
})
}
internalAtom.onMount = () => () => {}

if (process.env.NODE_ENV !== 'production') {
function setLabel(atom: Atom<unknown>, label: string) {
Expand Down Expand Up @@ -209,7 +206,7 @@ function ensureSyncEffectChannel(store: Store) {
() => void
>()
hookInto(storeWithHooks, INTERNAL_flushStoreHook, () => {
syncEffectChannel!.forEach((fn: () => void) => fn())
syncEffectChannel!.forEach((fn) => fn())
syncEffectChannel!.clear()
})
}
Expand Down

0 comments on commit 705004d

Please sign in to comment.