UNPKG

@zedux/core

Version:

A high-level, declarative, composable form of Redux

3 lines (2 loc) 6.99 kB
(function(i,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(i=typeof globalThis<"u"?globalThis:i||self,f(i.Zedux={}))})(this,function(i){"use strict";const f=t=>{const e=Object.getPrototypeOf(t);return e?Object.getPrototypeOf(e)?"complex object":"object":"prototype-less object"},I=t=>{const e=typeof t;return e!=="object"?e:t?Array.isArray(t)?"array":f(t):"null"},v=t=>{const e=r=>{const s={type:e.type};return typeof r<"u"&&(s.payload=r),s};return e.type=t,e},O=(t,e)=>typeof t=="string"?t:t.type,B=(t,e)=>t.map(r=>O(r)),F=t=>{const e={},r=(s=t,n)=>{const o=e[n.type]||[];return K(o,s,n)};return r.reduce=(s,n)=>{const o=Array.isArray(s)?B(s):[O(s)];return k(e,o,n),r},r},k=(t,e,r)=>{e.forEach(s=>{t[s]||(t[s]=[]),t[s].push(r)})},K=(t,e,r)=>t.reduce((s,n)=>n(s,r.payload,r),e),j=(t,e)=>{var r;return((r=t==null?void 0:t.constructor)==null?void 0:r.$$typeof)===e.$$typeof},g=t=>{if(typeof t!="object"||!t)return!1;const e=Object.getPrototypeOf(t);return e?Object.getPrototypeOf(e)===null:!1},p="@@zedux/",c={batch:`${p}batch`,delegate:`${p}delegate`,hydrate:`${p}hydrate`,ignore:`${p}ignore`,inherit:`${p}inherit`,merge:`${p}merge`,prime:`${p}prime`},l=1,_=2,N=3,w=4,X=Symbol.for("zedux.store"),Z=(t,e,r)=>{const s={};return Object.entries(t).forEach(([n,o])=>{const h=[...r,n];s[n]=T(o,e,h)}),s},q=(t,e,r,s)=>t===_?{type:t}:t===N?{type:t,reducer:e}:{type:t,destroy:r(s,e),reducer:J(e),store:e},G=t=>typeof t=="function"?N:t&&j(t,y)?w:g(t)?l:_,T=(t,e,r=[])=>{const s=G(t);return s!==l?q(s,t,e,r):{type:s,children:Z(t,e,r)}},J=t=>(r,s)=>{(s.type===c.hydrate||s.type===c.merge)&&(s={type:c.hydrate,payload:r});const n={metaType:c.inherit,payload:s};return t.dispatch(n)},L=(t,{create:e,get:r,isNode:s,set:n,size:o})=>(h=e(),d)=>{let u=e(),a=!1;return Object.keys(t).forEach(b=>{const{reducer:et}=t[b],z=s(h)?r(h,b):void 0,C=et(z,d);u=n(u,b,C),a||(a=C!==z)}),a||(a=!s(h)||!Object.keys(t).length&&!!o(h)),a?u:h},m=t=>{if(!t)return;const{children:e,destroy:r}=t;r&&r(),e&&Object.values(e).forEach(m)},A=(t,e,r)=>{const s={...t.children};return Object.keys(e.children).forEach(n=>{var u;const o=e.children[n],h=(u=t.children)==null?void 0:u[n],d=R(h,o,r);if(d.type===_){delete s[n];return}s[n]=d}),{children:s,reducer:L(s,r),type:l}},R=(t,e,r)=>e.type!==l?(m(t),e):!t||t.type!==l?(m(t),A({type:_},e,r)):A(t,e,r),E=(t,e,r)=>{if(!r.isNode(t)||!r.isNode(e))return[e,e!==t];let s=!1;const n=r.clone(t);return r.iterate(e,(o,h)=>{const d=r.get(n,o),[u,a]=r.isNode(h)?E(d,h,r):[h,h!==d];a&&(s||(s=a),r.set(n,o,u))}),[s?n:t,s]},Q=(t,e,r)=>!e||!r?t.payload:(e.payload=t.payload,r),$=(t,e)=>{for(;t.metaType;){if(t.metaType===e)return t.metaData;t=t.payload}},D=t=>{for(;t.metaType;)t=t.payload;return t},H=(t,e)=>{let r=t,s=null,n=null;for(;r.metaType;){if(r.metaType===e)return Q(r,s,n);const o={...r};s&&(s.payload=o),s=o,r=r.payload,n||(n=s)}return t},S="Minified Error",U=(t,e)=>{for(const r of e){if(t.type!==l)throw new ReferenceError(S);if(t=t.children[r],!t)throw new ReferenceError(S)}return t},W=(t,e)=>{const r=$(e,c.delegate);if(!r||!t)return!1;const s=U(t,r);if(s.type!==w)throw new TypeError(S);s.store.dispatch(H(e,c.delegate))},M=(t,e,r,s)=>{if(!e.length)return r;const n=s.clone(t),o=e[0];return s.set(n,o,M(s.get(t,o),e.slice(1),r,s))},Y=Object.freeze(Object.defineProperty({__proto__:null,clone:t=>({...t}),create:()=>({}),get:(t,e)=>t[e],isNode:g,iterate:(t,e)=>{Object.entries(t).forEach(([r,s])=>e(r,s))},set:(t,e,r)=>(t[e]=r,t),size:t=>Object.keys(t).length},Symbol.toStringTag,{value:"Module"})),V={scheduleNow:t=>t.task()},P={type:c.prime},tt=(t,e)=>new y(t,e),x=class{constructor(t,e){this._isSolo=!0,this._subscribers=[],this.dispatch=r=>this._isSolo?this._dispatch(r):(this._scheduler.scheduleNow({task:()=>this._dispatch(r),type:0}),this._state),this.setState=(r,s)=>this._isSolo?this._setState(r,s):(this._scheduler.scheduleNow({task:()=>this._setState(r,s),type:0}),this._state),this._state=e,this._scheduler=x._scheduler||V,t&&this.use(t)}actionStream(){return{[Symbol.observable](){return this},"@@observable"(){return this},subscribe:t=>this.subscribe({effects:({action:e,error:r})=>{var s,n;r&&typeof t!="function"?(s=t.error)==null||s.call(t,r):e&&(typeof t=="function"?t(e):(n=t.next)==null||n.call(t,e))}})}}getState(){return this._state}setStateDeep(t,e){return this._isSolo?this._setState(t,e,!0):(this._scheduler.scheduleNow({task:()=>this._setState(t,e,!0),type:0}),this._state)}subscribe(t){const e=typeof t=="function"?{next:t}:t;return this._subscribers.push(e),{unsubscribe:()=>{this._subscribers=this._subscribers.filter(s=>s!==e)}}}use(t){const e=T(t,(s,n)=>this._registerChildStore(s,n)),r=this._tree=R(this._tree,e,this.constructor.hierarchyConfig);return(this._rootReducer=r.reducer)&&this._dispatchAction(P,P,this._state),this}_register(t){const e=this._parents||(this._parents=[]);return e.push(t),this._isSolo=!1,()=>{const r=e.indexOf(t);r>-1&&e.splice(r,1)}}[Symbol.observable](){return this}"@@observable"(){return this}_dispatch(t){return W(this._tree,t)!==!1?this._state:this._routeAction(t)}_dispatchAction(t,e,r){this._isDispatching=!0;let s,n=r;try{this._rootReducer&&(n=this._rootReducer(r,e))}catch(o){throw s=o,o}finally{this._isDispatching=!1,this._notify(n,t,s)}return n}_dispatchHydration(t,e,r){const s=e===c.hydrate?t:E(this._state,t,this.constructor.hierarchyConfig)[0];if(s===this._state&&!r)return this._state;const n={meta:r,payload:s,type:e};return this._dispatchAction(n,n,s)}_dispatchStateSetter(t,e,r){let s;try{s=t(this._state)}catch(n){throw n}return this._dispatchHydration(s,r?c.merge:c.hydrate,e)}_doNotify(t){const{_subscribers:e}=this,r=t.newState!==t.oldState;for(const s of e)t.error&&s.error&&s.error(t.error),r&&s.next&&s.next(t.newState,t.oldState,t.action),s.effects&&s.effects(t)}_notify(t,e,r){var n;const s={action:e,error:r,newState:t,oldState:this._state,store:this};if(this._state=t,this._isSolo)return this._doNotify(s);this._scheduler.scheduleNow({task:()=>this._doNotify(s),type:1}),(n=this._parents)==null||n.forEach(o=>o(s))}_registerChildStore(t,e){this._isSolo=!1;const r=({action:s,error:n,newState:o,oldState:h})=>{if(this._isDispatching)return;const d=o===h?this._state:M(this._state,t,o,this.constructor.hierarchyConfig),u={metaType:c.delegate,metaData:t,payload:s};this._notify(d,u,n)};return e._register(r)}_routeAction(t){const e=D(t);return e.type===c.hydrate||e.type===c.merge?this._dispatchHydration(e.payload,e.type,e.meta):this._dispatchAction(t,e,this._state)}_setState(t,e,r=!1){return typeof t=="function"?this._dispatchStateSetter(t,e,r):this._dispatchHydration(t,r?c.merge:c.hydrate,e)}};let y=x;y.hierarchyConfig=Y,y.$$typeof=X,i.Store=y,i.actionFactory=v,i.createReducer=F,i.createStore=tt,i.detailedTypeof=I,i.getMetaData=$,i.is=j,i.isPlainObject=g,i.removeAllMeta=D,i.removeMeta=H,i.zeduxTypes=c,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}); //# sourceMappingURL=zedux.umd.min.js.map