@codescouts/vue-store-beta
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("pinia"),l=(t,s)=>(Object.entries(s).forEach(e=>{if(typeof e[1]=="object"&&t[e[0]]){const n=t[e[0]];t[e[0]]=l(n,e[1])}else t[e[0]]=e[1]}),t),y=(t,s)=>{const e=new t;return l(e,s),e},S=(t,s)=>y(t,s),h=(t,s,e)=>{const n=Object.keys(t)[0],i=t[n];return(e?()=>e(i,Object.assign):()=>S(s,i))()},p=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:c,entityType:f,factory:d}=s,u=e(f);if(c){const o=localStorage.getItem(u);if(o){let a;try{a=JSON.parse(o)}catch{a={}}r=h(a,f,d)}}return g.defineStore(t.name,{state:()=>({state:r}),actions:{save(o){this.$patch({state:o}),c&&localStorage.setItem(u,JSON.stringify(o))},get(){const o=this.$state.state;return S(t,o)}}})};function i(r,c){return s.entityType=r,s.withPersist=!0,s.factory=c,{build:n}}return{build:n,withPersist:i}};exports.create=p; | ||
"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 n;r(o=>{n=o});const f=c(t);if(s){const o=localStorage.getItem(f);if(o){let i;try{i=JSON.parse(o)}catch{i={}}n=m(i,t,h)}}return d.defineStore(t.name,{state:()=>({state:n}),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; |
@@ -1,45 +0,48 @@ | ||
import { defineStore as g } from "pinia"; | ||
const u = (t, s) => (Object.entries(s).forEach((e) => { | ||
import { defineStore as m } from "pinia"; | ||
const u = (t, r) => (Object.entries(r).forEach((e) => { | ||
if (typeof e[1] == "object" && t[e[0]]) { | ||
const n = t[e[0]]; | ||
t[e[0]] = u(n, e[1]); | ||
const c = t[e[0]]; | ||
t[e[0]] = u(c, e[1]); | ||
} else | ||
t[e[0]] = e[1]; | ||
}), t), h = (t, s) => { | ||
}), t), l = (t, r) => { | ||
const e = new t(); | ||
return u(e, s), e; | ||
}, S = (t, s) => h(t, s), y = (t, s, e) => { | ||
const n = Object.keys(t)[0], i = t[n]; | ||
return (e ? () => e(i, Object.assign) : () => S(s, i))(); | ||
}, O = (t) => { | ||
const s = { | ||
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 = { | ||
withPersist: !1, | ||
entityType: void 0, | ||
factory: void 0 | ||
}, e = (r) => typeof r == "string" ? r : r.name, n = () => { | ||
let r = new t(); | ||
const { withPersist: c, entityType: f, factory: d } = s, l = e(f); | ||
if (c) { | ||
const o = localStorage.getItem(l); | ||
}, c = (s) => typeof s == "string" ? s : s.name, a = () => { | ||
const { withPersist: s, factory: S } = e; | ||
let n; | ||
r((o) => { | ||
n = o; | ||
}); | ||
const f = c(t); | ||
if (s) { | ||
const o = localStorage.getItem(f); | ||
if (o) { | ||
let a; | ||
let i; | ||
try { | ||
a = JSON.parse(o); | ||
i = JSON.parse(o); | ||
} catch { | ||
a = {}; | ||
i = {}; | ||
} | ||
r = y(a, f, d); | ||
n = w(i, t, S); | ||
} | ||
} | ||
return g(t.name, { | ||
return m(t.name, { | ||
state: () => ({ | ||
state: r | ||
state: n | ||
}), | ||
actions: { | ||
save(o) { | ||
this.$patch({ state: o }), c && localStorage.setItem(l, JSON.stringify(o)); | ||
this.$patch({ state: o }), s && localStorage.setItem(f, JSON.stringify(o)); | ||
}, | ||
get() { | ||
const o = this.$state.state; | ||
return S(t, o); | ||
return g(t, o); | ||
} | ||
@@ -49,9 +52,6 @@ } | ||
}; | ||
function i(r, c) { | ||
return s.entityType = r, s.withPersist = !0, s.factory = c, { build: n }; | ||
} | ||
return { build: n, withPersist: i }; | ||
return { build: a, withPersist: (s) => (e.withPersist = !0, e.factory = s, { build: a }) }; | ||
}; | ||
export { | ||
O as create | ||
j as create | ||
}; |
@@ -1,8 +0,5 @@ | ||
import { Factory, Newly, Return, Store } from "./store.types"; | ||
export declare const create: <T, S>(Ctor: Newly<T>) => { | ||
import { Factory, Newly, Return, StateCreator, Store } from "./store.types"; | ||
export declare const create: <T, S>(rootType: Newly<T>, stateCreator: StateCreator<T>) => { | ||
build: () => Store<T, S>; | ||
withPersist: { | ||
<E>(entityType: string | Newly<E>, factory: Factory): Return<T, S>; | ||
<E_1>(entityType: Newly<E_1>): Return<T, S>; | ||
}; | ||
withPersist: (factory?: Factory) => Return<T, S>; | ||
}; |
import { Factory, Newly } from "./store.types"; | ||
export declare const reflectiveConstructor: <E>(EntityType: Newly<E>, persistedEntity: Partial<E>) => E; | ||
export declare const reflectiveConstructor: <E>(EntityType: Newly<E>, persistedEntity: Partial<E>) => E | E[]; | ||
export declare const merge: <E>(persistedState: any, EntityType: Newly<E>, factory?: Factory) => E; |
{ | ||
"name": "@codescouts/vue-store-beta", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.cjs.js", |
@@ -7,8 +7,8 @@ import { create } from "./auto-create"; | ||
persistedEntity: Partial<E> | ||
): E => { | ||
// if (typeof EntityType === "string") return persistedEntity as E; | ||
): E | E[] => { | ||
if (typeof EntityType === "string") return persistedEntity as E; | ||
// return Array.isArray(persistedEntity) | ||
// ? persistedEntity.map((entity) => create(EntityType, entity)) | ||
return create(EntityType, persistedEntity); | ||
return Array.isArray(persistedEntity) | ||
? persistedEntity.map((entity) => create(EntityType, entity)) | ||
: create(EntityType, persistedEntity); | ||
}; | ||
@@ -20,3 +20,3 @@ | ||
factory?: Factory | ||
): E => { | ||
) => { | ||
const entityKey = Object.keys(persistedState)[0]; | ||
@@ -23,0 +23,0 @@ const persistedEntity = persistedState[entityKey]; |
import { defineStore } from "pinia"; | ||
import { Factory, Newly, Return, Store } from "./store.types"; | ||
import { Factory, Newly, Return, StateCreator, Store } from "./store.types"; | ||
import { merge, reflectiveConstructor } from "./store.extensions"; | ||
export const create = <T, S>(Ctor: Newly<T>) => { | ||
export const create = <T, S>( | ||
rootType: Newly<T>, | ||
stateCreator: StateCreator<T> | ||
) => { | ||
const configuration = { | ||
withPersist: false, | ||
entityType: undefined as any, | ||
factory: undefined as any, | ||
@@ -16,7 +18,11 @@ }; | ||
const build = () => { | ||
let initialState = new Ctor() as T; | ||
const { withPersist, factory } = configuration; | ||
const { withPersist, entityType, factory } = configuration; | ||
let initialState: Partial<T>; | ||
const storageKey = getStoreKey(entityType); | ||
stateCreator((value) => { | ||
initialState = value; | ||
}); | ||
const storageKey = getStoreKey(rootType); | ||
if (withPersist) { | ||
@@ -33,3 +39,3 @@ const restoredRawState = localStorage.getItem(storageKey); | ||
const restoredState = merge<T>(persistedState, entityType, factory); | ||
const restoredState = merge(persistedState, rootType, factory); | ||
@@ -40,3 +46,3 @@ initialState = restoredState; | ||
const store = defineStore(Ctor.name, { | ||
const store = defineStore(rootType.name, { | ||
state: () => ({ | ||
@@ -53,6 +59,6 @@ state: initialState, | ||
}, | ||
get(): T { | ||
get(): T | T[] { | ||
const state = this.$state.state as T; | ||
return reflectiveConstructor(Ctor, state); | ||
return reflectiveConstructor(rootType, state); | ||
}, | ||
@@ -65,12 +71,3 @@ }, | ||
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; | ||
const withPersist = (factory?: Factory): Return<T, S> => { | ||
configuration.withPersist = true; | ||
@@ -80,5 +77,5 @@ configuration.factory = factory; | ||
return { build }; | ||
} | ||
}; | ||
return { build, withPersist }; | ||
}; |
8513
207