unstorage-pinia-plugin
Version:
Pinia store persistence and hydration using Unstorage
26 lines (25 loc) • 878 B
JavaScript
import { createStorage as f } from "unstorage";
const c = (e, t, i) => {
t.getItem(e.$id).then((r) => {
e.$patch(r);
});
const d = i ?? Object.keys(e.$state);
e.$subscribe(() => t.setItem(
e.$id,
JSON.stringify(Object.fromEntries(
Object.entries(e.$state).filter(([r]) => (d.indexOf(r) ?? 0) > -1)
))
)), t.setItem(
e.$id,
JSON.stringify(Object.fromEntries(
Object.entries(e.$state).filter(([r]) => (d.indexOf(r) ?? 0) > -1)
))
);
}, n = {}, $ = (e, t) => (n[e.$id] = t, e), a = ({ driver: e } = {}) => ({ options: t, store: i }) => {
t.unstorage ? c(i, f({ driver: t.unstorage.driver }), t.unstorage.filter) : n[i.$id] ? c(i, f({ driver: n[i.$id].driver }), n[i.$id].filter) : e && c(i, f({ driver: e }));
};
export {
a as createUnstoragePlugin,
$ as persistStore
};
//# sourceMappingURL=unstorage-pinia-plugin.mjs.map