Skip to content

Commit

Permalink
refactor atomWithLazy (#2485)
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi authored Apr 6, 2024
1 parent 1049ae1 commit 93a6fb9
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/vanilla/utils/atomWithLazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { PrimitiveAtom, atom } from '../../vanilla.ts'
export function atomWithLazy<Value>(
makeInitial: () => Value,
): PrimitiveAtom<Value> {
return {
...atom(undefined as unknown as Value),
get init() {
const a = atom(undefined as unknown as Value)
delete (a as { init?: Value }).init
Object.defineProperty(a, 'init', {
get() {
return makeInitial()
},
}
})
return a
}

0 comments on commit 93a6fb9

Please sign in to comment.