import { SerializableParam, atom, selectorFamily, RecoilState, AtomOptions, } from 'recoil' export function initializableAtom( options: Omit, 'default'> ): (initialValue: T) => RecoilState { const baseAtom: RecoilState = atom({ ...options, default: undefined, }) const evaluationSelector = selectorFamily({ key: `${options.key}::withInitialValue`, get: (initialValue) => ({ get }) => get(baseAtom) ?? initialValue, set: () => ({ set }, newValue) => set(baseAtom, newValue), }) return (initialValue) => { return evaluationSelector(initialValue) } }