@codescouts/vue-store-beta
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("pinia"),l=(t,s)=>(Object.entries(s).forEach(e=>{if(typeof e[1]=="object"&&t[e[0]]){const r=t[e[0]];t[e[0]]=l(r,e[1])}else t[e[0]]=e[1]}),t),d=(t,s)=>{const e=new t;return l(e,s),e},g=(t,s)=>d(t,s),h=(t,s,e,r)=>{const i=Object.keys(t)[0],o=t[i],a=(r?()=>r(o,Object.assign):()=>g(e,o))(),f={[i]:a};return{...s,...f}},S=t=>{const s={withPersist:!1,entityType:void 0,factory:void 0},e=o=>typeof o=="string"?o:o.name,r=()=>{let o=new t;const{withPersist:c,entityType:a,factory:f}=s,u=e(a);if(c){const n=localStorage.getItem(u);o=h(n,o,a,f)}return y.defineStore(t.name,{state:()=>({state:o}),actions:{save(n){this.$patch({state:n}),c&&localStorage.setItem(u,JSON.stringify(n))},get(){const n=this.$state.state;return g(t,n)}}})};function i(o,c){return s.entityType=o,s.withPersist=!0,s.factory=c,{build:r}}return{build:r,withPersist:i}};exports.create=S; | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("pinia"),u=(t,s)=>(Object.entries(s).forEach(e=>{if(typeof e[1]=="object"&&t[e[0]]){const n=t[e[0]];t[e[0]]=u(n,e[1])}else t[e[0]]=e[1]}),t),S=(t,s)=>{const e=new t;return u(e,s),e},l=(t,s)=>S(t,s),d=(t,s,e)=>{const n=Object.keys(t)[0],c=t[n];return(e?()=>e(c,Object.assign):()=>l(s,c))()},h=t=>{const s={withPersist:!1,entityType:void 0,factory:void 0},e=r=>typeof r=="string"?r:r.name,n=()=>{let r=new t;const{withPersist:i,entityType:a,factory:g}=s,f=e(a);if(i){let o;try{o=JSON.stringify(localStorage.getItem(f))}catch{o={}}r=d(o,a,g)}return y.defineStore(t.name,{state:()=>({state:r}),actions:{save(o){this.$patch({state:o}),i&&localStorage.setItem(f,JSON.stringify(o))},get(){const o=this.$state.state;return l(t,o)}}})};function c(r,i){return s.entityType=r,s.withPersist=!0,s.factory=i,{build:n}}return{build:n,withPersist:c}};exports.create=h; |
import { defineStore as y } from "pinia"; | ||
const l = (t, s) => (Object.entries(s).forEach((e) => { | ||
if (typeof e[1] == "object" && t[e[0]]) { | ||
const r = t[e[0]]; | ||
t[e[0]] = l(r, e[1]); | ||
const n = t[e[0]]; | ||
t[e[0]] = l(n, e[1]); | ||
} else | ||
@@ -11,10 +11,5 @@ t[e[0]] = e[1]; | ||
return l(e, s), e; | ||
}, g = (t, s) => h(t, s), m = (t, s, e, r) => { | ||
const i = Object.keys(t)[0], o = t[i], a = (r ? () => r(o, Object.assign) : () => g(e, o))(), f = { | ||
[i]: a | ||
}; | ||
return { | ||
...s, | ||
...f | ||
}; | ||
}, u = (t, s) => h(t, s), S = (t, s, e) => { | ||
const n = Object.keys(t)[0], i = t[n]; | ||
return (e ? () => e(i, Object.assign) : () => u(s, i))(); | ||
}, w = (t) => { | ||
@@ -25,25 +20,25 @@ const s = { | ||
factory: void 0 | ||
}, e = (o) => typeof o == "string" ? o : o.name, r = () => { | ||
let o = new t(); | ||
const { withPersist: c, entityType: a, factory: f } = s, u = e(a); | ||
}, e = (r) => typeof r == "string" ? r : r.name, n = () => { | ||
let r = new t(); | ||
const { withPersist: c, entityType: a, factory: g } = s, f = e(a); | ||
if (c) { | ||
const n = localStorage.getItem(u); | ||
o = m( | ||
n, | ||
o, | ||
a, | ||
f | ||
); | ||
let o; | ||
try { | ||
o = JSON.stringify(localStorage.getItem(f)); | ||
} catch { | ||
o = {}; | ||
} | ||
r = S(o, a, g); | ||
} | ||
return y(t.name, { | ||
state: () => ({ | ||
state: o | ||
state: r | ||
}), | ||
actions: { | ||
save(n) { | ||
this.$patch({ state: n }), c && localStorage.setItem(u, JSON.stringify(n)); | ||
save(o) { | ||
this.$patch({ state: o }), c && localStorage.setItem(f, JSON.stringify(o)); | ||
}, | ||
get() { | ||
const n = this.$state.state; | ||
return g(t, n); | ||
const o = this.$state.state; | ||
return u(t, o); | ||
} | ||
@@ -53,6 +48,6 @@ } | ||
}; | ||
function i(o, c) { | ||
return s.entityType = o, s.withPersist = !0, s.factory = c, { build: r }; | ||
function i(r, c) { | ||
return s.entityType = r, s.withPersist = !0, s.factory = c, { build: n }; | ||
} | ||
return { build: r, withPersist: i }; | ||
return { build: n, withPersist: i }; | ||
}; | ||
@@ -59,0 +54,0 @@ export { |
import { Factory, Newly } from "./store.types"; | ||
export declare const reflectiveConstructor: <E>(EntityType: Newly<E>, persistedEntity: Partial<E>) => E; | ||
export declare const merge: <StateType, E>(persistedState: any, currentState: StateType, EntityType: Newly<E>, factory?: Factory) => StateType; | ||
export declare const merge: <E>(persistedState: any, EntityType: Newly<E>, factory?: Factory) => E; |
{ | ||
"name": "@codescouts/vue-store-beta", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.cjs.js", |
@@ -15,8 +15,7 @@ import { create } from "./auto-create"; | ||
export const merge = <StateType, E>( | ||
export const merge = <E>( | ||
persistedState: any, | ||
currentState: StateType, | ||
EntityType: Newly<E>, | ||
factory?: Factory | ||
) => { | ||
): E => { | ||
const entityKey = Object.keys(persistedState)[0]; | ||
@@ -29,12 +28,5 @@ const persistedEntity = persistedState[entityKey]; | ||
const entityData = factoryImpl(); | ||
const restoredState = factoryImpl(); | ||
const state = { | ||
[entityKey]: entityData, | ||
}; | ||
return { | ||
...currentState, | ||
...state, | ||
} as StateType; | ||
return restoredState as E; | ||
}; |
@@ -22,10 +22,10 @@ import { defineStore } from "pinia"; | ||
if (withPersist) { | ||
const persistedState = localStorage.getItem(storageKey); | ||
let persistedState; | ||
try { | ||
persistedState = JSON.stringify(localStorage.getItem(storageKey)); | ||
} catch { | ||
persistedState = {}; | ||
} | ||
const restoredState = merge( | ||
persistedState, | ||
initialState, | ||
entityType, | ||
factory | ||
); | ||
const restoredState = merge<T>(persistedState, entityType, factory); | ||
@@ -32,0 +32,0 @@ initialState = restoredState; |
8547
208