@codescouts/vue-store-beta
Advanced tools
Comparing version 1.0.10 to 1.0.11
import { Newly } from "./store.types"; | ||
export declare const create: <T>(ctor: Newly<T>, data: any) => T; | ||
export declare const autoCreate: <T>(ctor: Newly<T>, data: any) => T; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("pinia"),l=(e,r)=>(Object.entries(r).forEach(t=>{if(typeof t[1]=="object"&&e[t[0]]){const s=e[t[0]];e[t[0]]=l(s,t[1])}else e[t[0]]=t[1]}),e),g=(e,r)=>{const t=new e;return l(t,r),t},S=(e,r)=>typeof e=="string"?r:Array.isArray(r)?r.map(t=>g(e,t)):g(e,r),m=(e,r,t)=>{const s=e.data;return(t?()=>t(s,Object.assign):()=>S(r,s))()},p=(e,r)=>{const t={withPersist:!1,entityType:void 0,factory:void 0},s=o=>typeof o=="string"?o:o.name,i=()=>{const{withPersist:o,entityType:n,factory:d}=t;if(o){const c=s(n),a=localStorage.getItem(c);if(a){let f;try{f=JSON.parse(a)}catch{f={}}r=m(f,n,d)}}return h.defineStore(e,{state:()=>({state:r}),actions:{save(c){if(this.$patch({state:c}),o){const a=s(n);localStorage.setItem(a,JSON.stringify({data:c}))}},get(){const{state:c}=this.$state;return c}}})};function u(o,n){return t.entityType=o,t.withPersist=!0,t.factory=n,{build:i}}return{build:i,withPersist:u}};exports.create=p; | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("pinia"),w=(e,r)=>(Object.entries(r).forEach(t=>{if(typeof t[1]=="object"&&e[t[0]]){const c=e[t[0]];e[t[0]]=w(c,t[1])}else e[t[0]]=t[1]}),e),S=(e,r)=>{const t=new e;return w(t,r),t},h=(e,r)=>typeof e=="string"?r:Array.isArray(r)?r.map(t=>S(e,t)):S(e,r),j=(e,r,t)=>(t?()=>t(e,h):()=>h(r,e))(),y=(e,r)=>{const t={withPersist:!1,entityType:void 0,factory:void 0,merge:void 0},c=o=>o?typeof o=="string"?o:o.name:e,a=()=>{const{withPersist:o,entityType:n,factory:f,merge:g}=t;let m=s=>r(s);if(o){const s=c(n),i=localStorage.getItem(s);if(i){let u;try{u=JSON.parse(i)}catch{u={}}const P=f&&n?j(u,n,f):g(u,h);m=O=>({...r(O),...P})}}const l={set(s){if(this.$patch(s),o){const i=c(n);localStorage.setItem(i,JSON.stringify(s))}},get(){return this.$state}},v=d.defineStore(e,{state:()=>m(l),actions:l});return()=>{const s=v();return Object.assign(l,s),s}};function p(o,n){return t.withPersist=!0,t.entityType=o,t.factory=n,{andRestore:g=>(t.merge=g,{build:a}),build:a}}return{build:a,withPersist:p}},I=e=>d.defineStore(e.name,{state:()=>new e,actions:{save(t){this.$patch(t)},get(){return S(e,this.$state)}}});exports.create=y;exports.useStoreFor=I; |
import { defineStore as d } from "pinia"; | ||
const h = (e, r) => (Object.entries(r).forEach((t) => { | ||
const w = (e, r) => (Object.entries(r).forEach((t) => { | ||
if (typeof t[1] == "object" && e[t[0]]) { | ||
const s = e[t[0]]; | ||
e[t[0]] = h(s, t[1]); | ||
const c = e[t[0]]; | ||
e[t[0]] = w(c, t[1]); | ||
} else | ||
e[t[0]] = t[1]; | ||
}), e), g = (e, r) => { | ||
}), e), l = (e, r) => { | ||
const t = new e(); | ||
return h(t, r), t; | ||
}, m = (e, r) => typeof e == "string" ? r : Array.isArray(r) ? r.map((t) => g(e, t)) : g(e, r), S = (e, r, t) => { | ||
const s = e.data; | ||
return (t ? () => t(s, Object.assign) : () => m(r, s))(); | ||
}, y = (e, r) => { | ||
return w(t, r), t; | ||
}, m = (e, r) => typeof e == "string" ? r : Array.isArray(r) ? r.map((t) => l(e, t)) : l(e, r), O = (e, r, t) => (t ? () => t(e, m) : () => m(r, e))(), j = (e, r) => { | ||
const t = { | ||
withPersist: !1, | ||
entityType: void 0, | ||
factory: void 0 | ||
}, s = (o) => typeof o == "string" ? o : o.name, i = () => { | ||
const { withPersist: o, entityType: n, factory: l } = t; | ||
factory: void 0, | ||
merge: void 0 | ||
}, c = (o) => o ? typeof o == "string" ? o : o.name : e, a = () => { | ||
const { withPersist: o, entityType: n, factory: f, merge: g } = t; | ||
let S = (s) => r(s); | ||
if (o) { | ||
const c = s(n), a = localStorage.getItem(c); | ||
if (a) { | ||
let f; | ||
const s = c(n), i = localStorage.getItem(s); | ||
if (i) { | ||
let u; | ||
try { | ||
f = JSON.parse(a); | ||
u = JSON.parse(i); | ||
} catch { | ||
f = {}; | ||
u = {}; | ||
} | ||
r = S(f, n, l); | ||
const P = f && n ? O(u, n, f) : g(u, m); | ||
S = (I) => ({ | ||
...r(I), | ||
...P | ||
}); | ||
} | ||
} | ||
return d(e, { | ||
state: () => ({ | ||
state: r | ||
}), | ||
actions: { | ||
save(c) { | ||
if (this.$patch({ state: c }), o) { | ||
const a = s(n); | ||
localStorage.setItem(a, JSON.stringify({ data: c })); | ||
} | ||
}, | ||
get() { | ||
const { state: c } = this.$state; | ||
return c; | ||
const h = { | ||
set(s) { | ||
if (this.$patch(s), o) { | ||
const i = c(n); | ||
localStorage.setItem(i, JSON.stringify(s)); | ||
} | ||
}, | ||
get() { | ||
return this.$state; | ||
} | ||
}, v = d(e, { | ||
state: () => S(h), | ||
actions: h | ||
}); | ||
return () => { | ||
const s = v(); | ||
return Object.assign(h, s), s; | ||
}; | ||
}; | ||
function u(o, n) { | ||
return t.entityType = o, t.withPersist = !0, t.factory = n, { build: i }; | ||
function p(o, n) { | ||
return t.withPersist = !0, t.entityType = o, t.factory = n, { andRestore: (g) => (t.merge = g, { build: a }), build: a }; | ||
} | ||
return { build: i, withPersist: u }; | ||
}; | ||
return { build: a, withPersist: p }; | ||
}, K = (e) => d(e.name, { | ||
state: () => new e(), | ||
actions: { | ||
save(t) { | ||
this.$patch(t); | ||
}, | ||
get() { | ||
return l(e, this.$state); | ||
} | ||
} | ||
}); | ||
export { | ||
y as create | ||
j as create, | ||
K as useStoreFor | ||
}; |
@@ -1,8 +0,11 @@ | ||
import { Factory, Newly, Return, Store } from "./store.types"; | ||
export declare const create: <T, S>(id: string, initialState: T) => { | ||
build: () => Store<T, S>; | ||
import { StateTree } from "pinia"; | ||
import { Factory, Newly, Return, StateCreator, StoreWithService } from "./store.types"; | ||
export declare const create: <S>(id: string, stateCreator: StateCreator<S>) => { | ||
build: () => () => import("pinia").Store<string, StateTree, {}, any>; | ||
withPersist: { | ||
<E>(entityType: string | Newly<E>, factory: Factory): Return<T, S>; | ||
<E_1>(entityType: Newly<E_1>): Return<T, S>; | ||
(): Return<S>; | ||
<E>(entityType: Newly<E>): Return<S>; | ||
<E_1>(entityType: string | Newly<E_1>, factory: Factory): Return<S>; | ||
}; | ||
}; | ||
export declare const useStoreFor: <T, S>(Ctor: Newly<T>) => StoreWithService<T, S>; |
import { Factory, Newly } from "./store.types"; | ||
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; | ||
export declare const autoMerge: <E>(persistedState: any, EntityType: Newly<E>, factory?: Factory) => E; |
@@ -1,17 +0,17 @@ | ||
export type StateCreator<T> = (set: (value: Partial<T>) => void) => any; | ||
export type Factory = (persistedEntity: any, assign: <T extends {}, U>(target: T, source: U) => T & U) => unknown; | ||
interface ImplicitStorage<S> { | ||
set(state: Partial<S>): void; | ||
get(): S; | ||
} | ||
export type Store<S> = () => S; | ||
export type StoreWithService<T, S> = () => Store<T> & S & ImplicitStorage<T>; | ||
export type StateCreator<S> = (store: ImplicitStorage<S>) => S; | ||
export type Newly<T> = { | ||
new (...args: any[]): T; | ||
}; | ||
export type Return<T, S> = { | ||
build: () => Store<T, S>; | ||
export type Factory = (persistedEntity: any, reflectiveConstructor: <E>(EntityType: Newly<E>, persistedEntity: E) => E | E[]) => unknown; | ||
export type Merge<S> = (persistedEntity: S, reflectiveConstructor: <E>(EntityType: Newly<unknown>, persistedEntity: E) => E) => Partial<S>; | ||
export type Return<S> = { | ||
build: () => Store<S>; | ||
andRestore: (merge: Merge<S>) => Omit<Return<S>, "andRestore">; | ||
}; | ||
interface ImplicitStorage<T> { | ||
save(state: T): void; | ||
get(): T; | ||
} | ||
type State<T> = { | ||
state: T; | ||
}; | ||
export type Store<T, S> = () => State<T> & S & ImplicitStorage<T>; | ||
export {}; |
{ | ||
"name": "@codescouts/vue-store-beta", | ||
"version": "1.0.10", | ||
"version": "1.0.11", | ||
"type": "module", | ||
@@ -26,3 +26,4 @@ "main": "./dist/index.cjs.js", | ||
"dependencies": { | ||
"@vue/composition-api": "^1.7.1" | ||
"@vue/composition-api": "^1.7.1", | ||
"pinia": "2.1.4" | ||
}, | ||
@@ -29,0 +30,0 @@ "devDependencies": { |
@@ -17,3 +17,3 @@ import { Newly } from "./store.types"; | ||
export const create = <T>(ctor: Newly<T>, data: any): T => { | ||
export const autoCreate = <T>(ctor: Newly<T>, data: any): T => { | ||
const base = new ctor() as T; | ||
@@ -20,0 +20,0 @@ |
@@ -1,1 +0,1 @@ | ||
export * from "./store"; | ||
export * from "./store"; |
@@ -1,2 +0,2 @@ | ||
import { create } from "./auto-create"; | ||
import { autoCreate } from "./auto-create"; | ||
import { Factory, Newly } from "./store.types"; | ||
@@ -11,7 +11,7 @@ | ||
return Array.isArray(persistedEntity) | ||
? persistedEntity.map((entity) => create(EntityType, entity)) | ||
: create(EntityType, persistedEntity); | ||
? persistedEntity.map((entity) => autoCreate(EntityType, entity)) | ||
: autoCreate(EntityType, persistedEntity); | ||
}; | ||
export const merge = <E>( | ||
export const autoMerge = <E>( | ||
persistedState: any, | ||
@@ -21,8 +21,6 @@ EntityType: Newly<E>, | ||
) => { | ||
const persistedEntity = persistedState.data; | ||
const factoryImpl = factory | ||
? () => factory(persistedState, reflectiveConstructor) | ||
: () => reflectiveConstructor(EntityType, persistedState); | ||
const factoryImpl = !!factory | ||
? () => factory(persistedEntity, Object.assign) | ||
: () => reflectiveConstructor(EntityType, persistedEntity); | ||
const restoredState = factoryImpl() as E; | ||
@@ -29,0 +27,0 @@ |
110
src/store.ts
@@ -1,6 +0,14 @@ | ||
import { defineStore } from "pinia"; | ||
import { Factory, Newly, Return, Store } from "./store.types"; | ||
import { merge } from "./store.extensions"; | ||
import { StateTree, _ActionsTree, defineStore } from "pinia"; | ||
import { | ||
Factory, | ||
Merge, | ||
Newly, | ||
Return, | ||
StateCreator, | ||
StoreWithService, | ||
} from "./store.types"; | ||
import { autoMerge, reflectiveConstructor } from "./store.extensions"; | ||
import { autoCreate } from "./auto-create"; | ||
export const create = <T, S>(id: string, initialState: T) => { | ||
export const create = <S>(id: string, stateCreator: StateCreator<S>) => { | ||
const configuration = { | ||
@@ -10,9 +18,14 @@ withPersist: false, | ||
factory: undefined as any, | ||
merge: undefined as any, | ||
}; | ||
const getStoreKey = (entityType: any) => | ||
typeof entityType === "string" ? entityType : entityType.name; | ||
const getStoreKey = (entityType: any) => { | ||
if (!entityType) return id; | ||
return typeof entityType === "string" ? entityType : entityType.name; | ||
}; | ||
const build = () => { | ||
const { withPersist, entityType, factory } = configuration; | ||
const { withPersist, entityType, factory, merge } = configuration; | ||
let initialState = (instance: any) => stateCreator(instance); | ||
@@ -31,45 +44,62 @@ if (withPersist) { | ||
initialState = merge(persistedState, entityType, factory); | ||
const mergedState = | ||
factory && entityType | ||
? autoMerge(persistedState, entityType, factory) | ||
: merge(persistedState, reflectiveConstructor); | ||
initialState = (instance) => ({ | ||
...stateCreator(instance), | ||
...mergedState, | ||
}); | ||
} | ||
} | ||
const store = defineStore(id, { | ||
state: () => ({ | ||
state: initialState, | ||
}), | ||
actions: { | ||
save(state: T) { | ||
this.$patch({ state }); | ||
const actions: any = { | ||
set(state: Partial<S>) { | ||
this.$patch(state); | ||
if (withPersist) { | ||
const storageKey = getStoreKey(entityType); | ||
if (withPersist) { | ||
const storageKey = getStoreKey(entityType); | ||
localStorage.setItem(storageKey, JSON.stringify({ data: state })); | ||
} | ||
}, | ||
get(): T | T[] { | ||
const { state } = this.$state; | ||
return state as T | T[]; | ||
}, | ||
localStorage.setItem(storageKey, JSON.stringify(state)); | ||
} | ||
}, | ||
get(): S { | ||
return this.$state; | ||
}, | ||
}; | ||
const store = defineStore(id, { | ||
state: () => initialState(actions) as StateTree, | ||
actions, | ||
}); | ||
return store as Store<T, S>; | ||
return () => { | ||
const instance = store(); | ||
Object.assign(actions, instance); | ||
return instance; | ||
}; | ||
}; | ||
function withPersist(): Return<S>; | ||
function withPersist<E>(entityType: Newly<E>): Return<S>; | ||
function withPersist<E>( | ||
entityType: string | Newly<E>, | ||
factory: Factory | ||
): Return<T, S>; | ||
function withPersist<E>(entityType: Newly<E>): Return<T, S>; | ||
): Return<S>; | ||
function withPersist<E>( | ||
entityType: string | Newly<E>, | ||
entityType?: string | Newly<E>, | ||
factory?: Factory | ||
): Return<T, S> { | ||
): Return<S> { | ||
configuration.withPersist = true; | ||
configuration.entityType = entityType; | ||
configuration.withPersist = true; | ||
configuration.factory = factory; | ||
return { build }; | ||
const andRestore = (merge: Merge<S>) => { | ||
configuration.merge = merge; | ||
return { build }; | ||
}; | ||
return { andRestore, build } as Return<S>; | ||
} | ||
@@ -79,1 +109,17 @@ | ||
}; | ||
export const useStoreFor = <T, S>(Ctor: Newly<T>) => { | ||
const store = defineStore(Ctor.name, { | ||
state: () => new Ctor() as any, | ||
actions: { | ||
save(state: T) { | ||
this.$patch(state); | ||
}, | ||
get(): T { | ||
return autoCreate(Ctor, this.$state); | ||
}, | ||
}, | ||
}); | ||
return store as StoreWithService<T, S>; | ||
}; |
@@ -1,7 +0,10 @@ | ||
export type StateCreator<T> = (set: (value: Partial<T>) => void) => any; | ||
export type Factory = ( | ||
persistedEntity: any, | ||
assign: <T extends {}, U>(target: T, source: U) => T & U | ||
) => unknown; | ||
interface ImplicitStorage<S> { | ||
set(state: Partial<S>): void; | ||
get(): S; | ||
} | ||
export type Store<S> = () => S; | ||
export type StoreWithService<T, S> = () => Store<T> & S & ImplicitStorage<T>; | ||
export type StateCreator<S> = (store: ImplicitStorage<S>) => S; | ||
export type Newly<T> = { | ||
@@ -11,15 +14,21 @@ new (...args: any[]): T; | ||
export type Return<T, S> = { | ||
build: () => Store<T, S>; | ||
}; | ||
export type Factory = ( | ||
persistedEntity: any, | ||
reflectiveConstructor: <E>( | ||
EntityType: Newly<E>, | ||
persistedEntity: E | ||
) => E | E[] | ||
) => unknown; | ||
interface ImplicitStorage<T> { | ||
save(state: T): void; | ||
get(): T; | ||
} | ||
export type Merge<S> = ( | ||
persistedEntity: S, | ||
reflectiveConstructor: <E>( | ||
EntityType: Newly<unknown>, | ||
persistedEntity: E | ||
) => E | ||
) => Partial<S>; | ||
type State<T> = { | ||
state: T; | ||
export type Return<S> = { | ||
build: () => Store<S>; | ||
andRestore: (merge: Merge<S>) => Omit<Return<S>, "andRestore">; | ||
}; | ||
export type Store<T, S> = () => State<T> & S & ImplicitStorage<T>; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
11133
282
5
1
+ Addedpinia@2.1.4
+ Addedpinia@2.1.4(transitive)
- Removedpinia@2.3.0(transitive)