Comparing version 0.0.0-alpha.3 to 0.0.0-alpha.4
@@ -1,47 +0,57 @@ | ||
declare class StoreCore<T, A> { | ||
private subs; | ||
private inner; | ||
private trigger; | ||
private actions; | ||
readonly actionsProxy: any; | ||
constructor(inner: T, actions: A); | ||
snap(): Readonly<T>; | ||
getActionFn(action_name: string | symbol): Function; | ||
declare class StoreCore<T, A, AA> { | ||
private id; | ||
private c; | ||
private i; | ||
private t; | ||
private a; | ||
readonly ap: any; | ||
private aa; | ||
readonly aap: any; | ||
readonly sp: SmalProxy<T, A>; | ||
constructor(inner: T, actions: A, aactions: AA); | ||
aaFn(action_name: string | symbol): Function; | ||
aFn(action_name: string | symbol): Function; | ||
mutFn(fn: (v: T) => void): void; | ||
update(newState: Partial<T>): void; | ||
updateValue(key: any, value: any): void; | ||
notifySubs(): void; | ||
u(newState: Partial<T>): void; | ||
uv(key: any, value: any): void; | ||
ns(): void; | ||
s(): Readonly<T>; | ||
sub(fn: SubFn<T>): Sub; | ||
} | ||
interface StoreProxy<T, A> { | ||
interface SmalProxy<T, A> { | ||
snap(): T; | ||
sub(fn: SubFn<T>): Sub; | ||
set mut(v: Partial<T>); | ||
get mut(): (fn: (v: T) => void) => void; | ||
actions: A; | ||
actions: Readonly<A>; | ||
} | ||
interface ProxyExtension<T, A> { | ||
interface StoreProxy<T, A, AA> extends SmalProxy<T, A> { | ||
sub(fn: SubFn<T>): Sub; | ||
async: Readonly<AA>; | ||
} | ||
interface ProxyExtension<T, A, AA> { | ||
name: string; | ||
init(core: StoreCore<T, A>): object; | ||
init(core: StoreCore<T, A, AA>): object; | ||
} | ||
declare const newStoreProxy: <T, A>(state: StoreDefiniton<T, A>, ext?: ProxyExtension<T, A>) => any; | ||
type Store<T, A> = StoreProxy<T, A> & T; | ||
declare const newStoreProxy: <T, A, AA>(state: StoreDefiniton<T, A, AA>, ext?: ProxyExtension<T, A, AA>) => any; | ||
type ExcludeKeys = { | ||
async?: never; | ||
actions?: never; | ||
snap?: never; | ||
sub?: never; | ||
mut?: never; | ||
}; | ||
type Store<T, A, AA> = StoreProxy<T, A, AA> & T; | ||
type SubFn<T> = (value: Readonly<T>) => void; | ||
interface Sub { | ||
type Sub = { | ||
destroy(): void; | ||
}; | ||
type VoidFn = ((...args: any) => undefined); | ||
type AsyncFn = ((...args: any) => Promise<void>); | ||
interface StoreDefiniton<T, A, AA> { | ||
value: T & object & ExcludeKeys; | ||
actions?: A & ThisType<T> & Record<string, VoidFn>; | ||
async?: AA & ThisType<SmalProxy<T, A>> & Record<string, AsyncFn>; | ||
} | ||
type VoidFn = ((...args: any) => Promise<void>); | ||
interface StoreDefiniton<T, A> { | ||
value: T & object & { | ||
actions?: never; | ||
snap?: never; | ||
sub?: never; | ||
mut?: never; | ||
}; | ||
actions?: A & ThisType<T & Readonly<A>> & { | ||
[key: string]: VoidFn; | ||
}; | ||
} | ||
declare const newStore: <T, A>(state: StoreDefiniton<T, A>) => Store<T, A>; | ||
declare const newStore: <T, A, AA>(state: StoreDefiniton<T, A, AA>) => Store<T, A, AA>; | ||
export { type ProxyExtension, type Store, type StoreDefiniton, type StoreProxy, type Sub, type SubFn, type VoidFn, newStore, newStoreProxy }; | ||
export { type AsyncFn, type ProxyExtension, type Store, type StoreDefiniton, type StoreProxy, type Sub, type SubFn, type VoidFn, newStore, newStoreProxy }; |
@@ -1,1 +0,1 @@ | ||
var i=Object,T=JSON,a=n=>{for(let e of i.keys(n)){let t=n[e];typeof t=="object"&&a(t)}return i.freeze(n)},u=T.stringify,d=n=>T.parse(u(n)),c=class{subs={};inner;trigger;actions;actionsProxy;constructor(e,t){this.inner=a(i.assign({},e)),this.actions=i.freeze(t),this.actionsProxy=i.freeze(f(this))}snap(){return this.inner}getActionFn(e){let t=this.actions[e];return async(...r)=>{let o=d(this.snap());await t.bind(o)(...r),this.update(o)}}mutFn(e){let t=d(this.snap());e(t),this.update(t)}update(e){let t=a(i.assign(i.assign({},this.inner),e));u(this.inner)!==u(t)&&(this.inner=t,clearTimeout(this.trigger),this.trigger=setTimeout(this.notifySubs.bind(this),0))}updateValue(e,t){this.update({[e]:t})}notifySubs(){for(let e of i.getOwnPropertySymbols(this.subs))this.subs[e](this.inner)}sub(e){let t=Symbol();return this.subs[t]=e,{destroy:()=>delete this.subs[t]}}},f=n=>new Proxy({},{get(e,t){return n.getActionFn(t)}}),l=(n,e)=>{let t=new c(n.value,n.actions??{}),r=e?.init(t),o=e?.name??null;return new Proxy(n.value,{get(b,s){return s==="sub"?t.sub.bind(t):s==="snap"?t.snap.bind(t):s==="mut"?t.mutFn.bind(t):s==="actions"?t.actionsProxy:o&&s===o?r:t.snap()[s]},set(b,s,y){return s==="mut"?t.update(y):t.updateValue(s,y),!0}})},p=n=>l(n);export{p as newStore,l as newStoreProxy}; | ||
var u=Object,l=JSON,A=l.stringify,p=e=>l.parse(A(e)),c=u.freeze,y=u.assign,d=e=>{for(let n of u.keys(e)){let t=e[n];typeof t=="object"&&d(t)}return c(e)},T=class{id=0;c={};i;t;a;ap;aa;aap;sp;constructor(n,t,o){this.i=d(y({},n)),this.a=c(t),this.ap=c(h(i=>this.aFn(i))),this.aa=c(o),this.aap=c(h(i=>this.aaFn(i)));let s=this;this.sp={actions:this.ap,snap(){return s.s()},get mut(){return s.mutFn.bind(s)},set mut(i){s.u(i)}}}aaFn(n){return this.aa[n].bind(this.sp)}aFn(n){let t=this.a[n];return(...o)=>{let s=p(this.s());t.bind(s)(...o),this.u(s)}}mutFn(n){let t=p(this.s());n(t),this.u(t)}u(n){let t=d(y(y({},this.i),n));A(this.i)!==A(t)&&(this.i=t,clearTimeout(this.t),this.t=setTimeout(this.ns.bind(this),0))}uv(n,t){this.u({[n]:t})}ns(){for(let n of u.keys(this.c))this.c[n](this.i)}s(){return this.i}sub(n){let t=this.id++;return this.c[t]=n,{destroy:()=>delete this.c[t]}}},h=e=>new Proxy({},{get(n,t){return e(t)}}),v=(e,n)=>{let t=new T(e.value,e.actions??{},e.async??{}),o=n?{[n.name]:n.init(t)}:{},s=y({sub:t.sub.bind(t),async:t.aap},y(o,t.sp));return new Proxy({},{get(i,r){let a=s[r];return a||t.s()[r]},set(i,r,a){return r==="mut"?t.u(a):t.uv(r,a),!0}})},b=e=>v(e);export{b as newStore,v as newStoreProxy}; |
{ | ||
"name": "mute8", | ||
"description": "JS State Container", | ||
"version": "0.0.0-alpha.3", | ||
"version": "0.0.0-alpha.4", | ||
"type": "module", | ||
@@ -6,0 +6,0 @@ "author": "Paweł Jastrzębski", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
7293
68