@codescouts/vue-store-beta
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("pinia"),l=(t,r)=>(Object.entries(r).forEach(e=>{if(typeof e[1]=="object"&&t[e[0]]){const c=t[e[0]];t[e[0]]=l(c,e[1])}else t[e[0]]=e[1]}),t),u=(t,r)=>{const e=new t;return l(e,r),e},g=(t,r)=>typeof t=="string"?r:Array.isArray(r)?r.map(e=>u(t,e)):u(t,r),m=(t,r,e)=>{const c=Object.keys(t)[0],a=t[c];return(e?()=>e(a,Object.assign):()=>g(r,a))()},w=(t,r)=>{const e={withPersist:!1,factory:void 0},c=s=>typeof s=="string"?s:s.name,a=()=>{const{withPersist:s,factory:h}=e;let i=r();const f=c(t);if(s){const o=localStorage.getItem(f);if(o){let n;try{n=JSON.parse(o)}catch{n={}}i=m(n,t,h)}}return d.defineStore(t.name,{state:()=>({state:i}),actions:{save(o){this.$patch({state:o}),s&&localStorage.setItem(f,JSON.stringify(o))},get(){const o=this.$state.state;return g(t,o)}}})};return{build:a,withPersist:s=>(e.withPersist=!0,e.factory=s,{build:a})}};exports.create=w; | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("pinia"),S=(e,r)=>(Object.entries(r).forEach(t=>{if(typeof t[1]=="object"&&e[t[0]]){const s=e[t[0]];e[t[0]]=S(s,t[1])}else e[t[0]]=t[1]}),e),g=(e,r)=>{const t=new e;return S(t,r),t},y=(e,r)=>typeof e=="string"?r:Array.isArray(r)?r.map(t=>g(e,t)):g(e,r),m=(e,r,t)=>{const s=Object.keys(e)[0],c=e[s];return(t?()=>t(c,Object.assign):()=>y(r,c))()},p=(e,r)=>{const t={withPersist:!1,entityType:void 0,factory:void 0},s=o=>typeof o=="string"?o:o.name,c=()=>{const{withPersist:o,entityType:i,factory:h}=t;let u=r();const l=s(i);if(o){const n=localStorage.getItem(l);if(n){let a;try{a=JSON.parse(n)}catch{a={}}u=m(a,i,h)}}return d.defineStore(e,{state:()=>({state:u}),actions:{save(n){this.$patch({state:n}),o&&localStorage.setItem(l,JSON.stringify(n))},get(){return this.$state.state}}})};function f(o,i){return t.entityType=o,t.withPersist=!0,t.factory=i,{build:c}}return{build:c,withPersist:f}};exports.create=p; |
import { defineStore as m } from "pinia"; | ||
const u = (t, r) => (Object.entries(r).forEach((e) => { | ||
if (typeof e[1] == "object" && t[e[0]]) { | ||
const c = t[e[0]]; | ||
t[e[0]] = u(c, e[1]); | ||
const S = (e, r) => (Object.entries(r).forEach((t) => { | ||
if (typeof t[1] == "object" && e[t[0]]) { | ||
const s = e[t[0]]; | ||
e[t[0]] = S(s, t[1]); | ||
} else | ||
t[e[0]] = e[1]; | ||
}), t), l = (t, r) => { | ||
const e = new t(); | ||
return u(e, r), e; | ||
}, g = (t, r) => typeof t == "string" ? r : Array.isArray(r) ? r.map((e) => l(t, e)) : l(t, r), w = (t, r, e) => { | ||
const c = Object.keys(t)[0], a = t[c]; | ||
return (e ? () => e(a, Object.assign) : () => g(r, a))(); | ||
}, j = (t, r) => { | ||
const e = { | ||
e[t[0]] = t[1]; | ||
}), e), g = (e, r) => { | ||
const t = new e(); | ||
return S(t, r), t; | ||
}, d = (e, r) => typeof e == "string" ? r : Array.isArray(r) ? r.map((t) => g(e, t)) : g(e, r), p = (e, r, t) => { | ||
const s = Object.keys(e)[0], c = e[s]; | ||
return (t ? () => t(c, Object.assign) : () => d(r, c))(); | ||
}, P = (e, r) => { | ||
const t = { | ||
withPersist: !1, | ||
entityType: void 0, | ||
factory: void 0 | ||
}, c = (s) => typeof s == "string" ? s : s.name, a = () => { | ||
const { withPersist: s, factory: S } = e; | ||
let i = r(); | ||
const f = c(t); | ||
if (s) { | ||
const o = localStorage.getItem(f); | ||
if (o) { | ||
let n; | ||
}, s = (o) => typeof o == "string" ? o : o.name, c = () => { | ||
const { withPersist: o, entityType: i, factory: h } = t; | ||
let u = r(); | ||
const l = s(i); | ||
if (o) { | ||
const n = localStorage.getItem(l); | ||
if (n) { | ||
let a; | ||
try { | ||
n = JSON.parse(o); | ||
a = JSON.parse(n); | ||
} catch { | ||
n = {}; | ||
a = {}; | ||
} | ||
i = w(n, t, S); | ||
u = p(a, i, h); | ||
} | ||
} | ||
return m(t.name, { | ||
return m(e, { | ||
state: () => ({ | ||
state: i | ||
state: u | ||
}), | ||
actions: { | ||
save(o) { | ||
this.$patch({ state: o }), s && localStorage.setItem(f, JSON.stringify(o)); | ||
save(n) { | ||
this.$patch({ state: n }), o && localStorage.setItem(l, JSON.stringify(n)); | ||
}, | ||
get() { | ||
const o = this.$state.state; | ||
return g(t, o); | ||
return this.$state.state; | ||
} | ||
@@ -49,6 +49,9 @@ } | ||
}; | ||
return { build: a, withPersist: (s) => (e.withPersist = !0, e.factory = s, { build: a }) }; | ||
function f(o, i) { | ||
return t.entityType = o, t.withPersist = !0, t.factory = i, { build: c }; | ||
} | ||
return { build: c, withPersist: f }; | ||
}; | ||
export { | ||
j as create | ||
P as create | ||
}; |
@@ -1,5 +0,8 @@ | ||
import { Factory, Newly, Return, StateCreator, Store } from "./store.types"; | ||
export declare const create: <T, S>(rootType: Newly<T>, stateCreator: StateCreator<T>) => { | ||
import { Factory, Newly, Return, Store } from "./store.types"; | ||
export declare const create: <T, S>(id: string, stateCreator: () => Partial<T>) => { | ||
build: () => Store<T, S>; | ||
withPersist: (factory?: Factory) => Return<T, S>; | ||
withPersist: { | ||
<E>(entityType: string | Newly<E>, factory: Factory): Return<T, S>; | ||
<E_1>(entityType: Newly<E_1>): Return<T, S>; | ||
}; | ||
}; |
{ | ||
"name": "@codescouts/vue-store-beta", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.cjs.js", |
import { defineStore } from "pinia"; | ||
import { Factory, Newly, Return, StateCreator, Store } from "./store.types"; | ||
import { merge, reflectiveConstructor } from "./store.extensions"; | ||
import { Factory, Newly, Return, Store } from "./store.types"; | ||
import { merge } from "./store.extensions"; | ||
export const create = <T, S>( | ||
rootType: Newly<T>, | ||
stateCreator: StateCreator<T> | ||
) => { | ||
export const create = <T, S>(id: string, stateCreator: () => Partial<T>) => { | ||
const configuration = { | ||
withPersist: false, | ||
entityType: undefined as any, | ||
factory: undefined as any, | ||
@@ -18,7 +16,8 @@ }; | ||
const build = () => { | ||
const { withPersist, factory } = configuration; | ||
const { withPersist, entityType, factory } = configuration; | ||
let initialState: Partial<T> = stateCreator(); | ||
const storageKey = getStoreKey(rootType); | ||
const storageKey = getStoreKey(entityType); | ||
if (withPersist) { | ||
@@ -35,9 +34,9 @@ const restoredRawState = localStorage.getItem(storageKey); | ||
const restoredState = merge(persistedState, rootType, factory); | ||
const restoredState = merge(persistedState, entityType, factory); | ||
initialState = restoredState; | ||
initialState = restoredState as T; | ||
} | ||
} | ||
const store = defineStore(rootType.name, { | ||
const store = defineStore(id, { | ||
state: () => ({ | ||
@@ -55,5 +54,3 @@ state: initialState, | ||
get(): T | T[] { | ||
const state = this.$state.state as T; | ||
return reflectiveConstructor(rootType, state); | ||
return this.$state.state as T | T[]; | ||
}, | ||
@@ -66,3 +63,12 @@ }, | ||
const withPersist = (factory?: Factory): Return<T, S> => { | ||
function withPersist<E>( | ||
entityType: string | Newly<E>, | ||
factory: Factory | ||
): Return<T, S>; | ||
function withPersist<E>(entityType: Newly<E>): Return<T, S>; | ||
function withPersist<E>( | ||
entityType: string | Newly<E>, | ||
factory?: Factory | ||
): Return<T, S> { | ||
configuration.entityType = entityType; | ||
configuration.withPersist = true; | ||
@@ -72,5 +78,5 @@ configuration.factory = factory; | ||
return { build }; | ||
}; | ||
} | ||
return { build, withPersist }; | ||
}; |
8740
213