alinea
Version:
[](https://npmjs.org/package/alinea) [](https://packagephobia.com/result?p=alinea)
29 lines (27 loc) • 664 B
JavaScript
import {
atom
} from "../../chunks/chunk-OBOPLPUQ.js";
import "../../chunks/chunk-U5RRZUYZ.js";
// src/dashboard/util/KeepPreviousData.ts
function keepPreviousData(asynAtom, initalValue) {
let expected;
const currentAtom = atom(initalValue);
return atom(
(get, { setSelf }) => {
const next = get(asynAtom);
const current = get(currentAtom);
expected = next.then((value) => {
setSelf(expected, value);
return value;
});
return current ?? expected;
},
(get, set, forPromise, value) => {
if (forPromise === expected)
set(currentAtom, value);
}
);
}
export {
keepPreviousData
};