@lucianojd/recoil-sync-next
Version:
recoil-sync stores for Next.js
20 lines (19 loc) • 739 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.initializableAtom = void 0;
const recoil_1 = require("recoil");
function initializableAtom(options) {
const baseAtom = (0, recoil_1.atom)({
...options,
default: undefined,
});
const evaluationSelector = (0, recoil_1.selectorFamily)({
key: `${options.key}::withInitialValue`,
get: (initialValue) => ({ get }) => { var _a; return (_a = get(baseAtom)) !== null && _a !== void 0 ? _a : initialValue; },
set: () => ({ set }, newValue) => set(baseAtom, newValue),
});
return (initialValue) => {
return evaluationSelector(initialValue);
};
}
exports.initializableAtom = initializableAtom;