alinea
Version:
Headless git-based CMS
33 lines (31 loc) • 922 B
JavaScript
import {
atom
} from "../../chunks/chunk-WJ67RR7S.js";
import "../../chunks/chunk-NZLE2WMY.js";
// src/dashboard/util/KeepPreviousData.ts
import { assign } from "alinea/core/util/Objects";
function keepPreviousData(asyncAtom, options = {}) {
let expected;
const currentAtom = atom(options.initialValue);
return assign(
atom(
(get, { setSelf }) => {
const next = get(asyncAtom);
const current = get(currentAtom);
expected = next.then((value) => {
const isSame = options.compare && current !== void 0 ? options.compare(value, current) : value === current;
if (!isSame) setSelf(expected, value);
return value;
});
return current ?? expected;
},
(get, set, forPromise, value) => {
if (forPromise === expected) set(currentAtom, value);
}
),
{ current: currentAtom }
);
}
export {
keepPreviousData
};