UNPKG

easy-peasy

Version:

Vegetarian friendly state for React

3 lines (2 loc) 14.9 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("immer-peasy"),require("react"),require("redux"),require("redux-thunk"),require("memoizerific"),require("is-plain-object"),require("debounce")):"function"==typeof define&&define.amd?define(["exports","immer-peasy","react","redux","redux-thunk","memoizerific","is-plain-object","debounce"],t):t((e=e||self).EasyPeasy={},e.produce,e.React,e.Redux,e.ReduxThunk,e.memoizerific,e.isPlainObject,e.debounce)}(this,function(e,d,s,W,Y,U,Z,$){"use strict";var a="default"in s?s.default:s;Y=Y&&Y.hasOwnProperty("default")?Y.default:Y,U=U&&U.hasOwnProperty("default")?U.default:U,Z=Z&&Z.hasOwnProperty("default")?Z.default:Z,$=$&&$.hasOwnProperty("default")?$.default:$;var r=s.createContext(),f="undefined"!=typeof window?s.useLayoutEffect:s.useEffect;function t(c){return function(e){var n=s.useContext(c),r=s.useRef(e),o=s.useRef(),i=s.useRef(!0),u=s.useRef(),a=s.useReducer(function(e){return e+1},0)[1];if(u.current||r.current!==e||void 0===o.current)try{o.current=e(n.getState())}catch(e){var t="An error occurred trying to map state in a useStoreState hook: "+e.message+".";throw u.current&&(t+="\nThis error may be related to the following error:\n"+u.current.stack+"\n\nOriginal stack trace:"),new Error(t)}return f(function(){r.current=e,u.current=void 0}),f(function(){var e=function(){try{var e=r.current(n.getState());if(e===o.current)return;o.current=e}catch(e){u.current=e}i.current&&a({})},t=n.subscribe(e);return e(),function(){i.current=!1,t()}},[]),o.current}}var n=t(r);function c(t){return function(e){return e(s.useContext(t).getActions())}}var o=c(r);function l(e){return function(){return s.useContext(e).dispatch}}var i=l(r);function u(){return s.useContext(r)}function p(o){return function(){var e=s.useContext(o),t=s.useState(!1),n=t[0],r=t[1];return s.useEffect(function(){e.persist.resolveRehydration().then(function(){return r(!0)})},[]),n}}var v=p(r);function ee(){return(ee=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}var V="🙈action🙈",F="🙈actionOn🙈",H="🙈computedSymbol🙈",B="🙈persistSymbol🙈",G="🙈reducer🙈",K="🙈thunkOn🙈",Q="🙈thunk🙈",te=function(e){return e[V]={},e},h=[function(e){return e}],ne=function(e){return function t(n){var r=Object.keys(n).reduce(function(e,t){return null==Object.getOwnPropertyDescriptor(n,t).get&&(e[t]=n[t]),e},{});return Object.keys(r).forEach(function(e){Z(r[e])&&(r[e]=t(r[e]))}),r}(e)},re=function(e){return null!=e&&"object"==typeof e&&"function"==typeof e.then};function oe(e,t){return e.reduce(function(e,t){return Z(e)?e[t]:void 0},t)}var ie=function(r,t,o){0!==r.length?r.reduce(function(e,t,n){return n+1===r.length?e[t]=o:e[t]=e[t]||{},e[t]},t):"object"==typeof o&&(Object.keys(t).forEach(function(e){delete t[e]}),Object.keys(o).forEach(function(e){t[e]=o[e]}))};function y(l){return void 0===l&&(l=!1),function(e,t,n){if(l){var r=oe(e,t),o=n(r);return r!==o?function e(t,n,r){if(0===t.length)return r;var o=ee({},n),i=t[0];return 1===t.length?o[i]=r:o[i]=e(t.slice(1),o[i],r),o}(e,t,o):t}if(0===e.length){var i=d.createDraft(t),u=n(i);return u?d.isDraft(u)?d.finishDraft(u):u:d.finishDraft(i)}var a=e.slice(0,e.length-1),c=d.createDraft(t),s=oe(a,t),f=n(oe(e,c));return f&&(s[e[e.length-1]]=f),d.finishDraft(c)}}function ue(e,c,u,a){var s=y(e),f=function(e,t){var n,r,o,i,u=c[t.type];if(u){var a=u[V]||u[F];return n=e,r=t,o=u,i=a.parent,s(i,n,function(e){return o(e,r.payload)})}return e};return function(e,t){var n,i,r=f(e,t),o=0<u.length?(n=r,i=t,u.reduce(function(e,t){var n=t.parentPath,r=t.key,o=t.reducer;return s(n,e,function(e){return e[r]=o(e[r],i),e})},n)):r;return e!==o&&a.forEach(function(e){var t=e.parentPath;(0,e.bindComputedProperty)(oe(t,o))}),o}}var g={getItem:function(){},setItem:function(){},removeItem:function(){}},m="undefined"!=typeof window&&void 0!==window.localStorage?window.localStorage:g,S="undefined"!=typeof window&&void 0!==window.sessionStorage?window.sessionStorage:g;function ae(u,a){void 0===u&&(u=S),void 0===a&&(a=[]),"string"==typeof u&&(u="localStorage"===u?m:"sessionStorage"===u?S:g);var o=a.reverse(),n=function(e,t){var n=t.substr(t.indexOf("@")+1),r=u===m||u===S?JSON.parse(e).data:e;return o.reduce(function(e,t){return t.out(e,n)},r)},r=re(u.getItem("_"));return{isAsync:r,getItem:function(t){if(r)return u.getItem(t).then(function(e){return null!=e?n(e,t):void 0});var e=u.getItem(t);return null!=e?n(e,t):void 0},setItem:function(e,t){return u.setItem(e,(n=t,o=(r=e).substr(r.indexOf("@")+1),i=a.reduce(function(e,t){return t.in(e,o)},n),u===m||u===S?JSON.stringify({data:i}):i));var n,r,o,i},removeItem:function(e){return u.removeItem(e)}}}function ce(e,n,r){var t=Object.keys(e);return 0<n.length&&(t=t.reduce(function(e,t){return-1!==n.findIndex(function(e){return e===t})?[].concat(e,[t]):e},[])),0<r.length&&(t=t.reduce(function(e,t){return-1!==r.findIndex(function(e){return e===t})?e:[].concat(e,[t])},[])),t}function se(e,j,A,I){var o,i,u,x=j,D={},T={},_={},M={},N=[],q=[],z={},t={},L=[],X=[],J={isInReducer:!1,currentState:x};return function C(P,R){return Object.keys(P).forEach(function(t){var e,r,n,o,i,u,a,c,s,f,l,d,p,v,h=P[t],y=[].concat(R,[t]),g={parent:R,path:y,key:t},m=function(){var e=oe(R,j);ie(y,x,e&&t in e?e[t]:h)};if(t!==B)if("function"==typeof h)if(h[V]||h[F]){var S=h,b=function(n,e,r){var o=(n[V]?"@action":"@actionOn")+"."+e.path.join("."),i=n[V]||n[F];i.actionName=e.key,i.type=o,i.parent=e.parent,i.path=e.path;var t=function(e){var t={type:o,payload:e};return n[F]&&i.resolvedTargets&&(e.resolvedTargets=[].concat(i.resolvedTargets)),r.dispatch(t)};return t.type=o,t}(h,g,I);D[b.type]=b,_[b.type]=S,"easyPeasyReplaceState"!==g.key&&(h[F]?(L.push(h),ie(y,z,b)):ie(y,T,b))}else if(h[Q]||h[K]){var O=(f=g,l=I,d=A,p=T,v=(s=h)[Q]||s[K],function(e){var t={dispatch:l.dispatch,getState:function(){return oe(f.parent,l.getState())},getStoreActions:function(){return p},getStoreState:l.getState,injections:d,meta:f};return s[K]&&v.resolvedTargets&&(e.resolvedTargets=[].concat(v.resolvedTargets)),s(oe(f.parent,p),e,t)}),k=function(e,t,o,i){var u=(e[Q]?"@thunk":"@thunkOn")+"."+t.path.join("."),a=u+"(start)",c=u+"(success)",s=u+"(fail)",n=e[Q]||e[K];n.type=u,n.actionName=t.key,n.parent=t.parent,n.path=t.path;var r=function(t){var n=function(e){o.dispatch({type:s,payload:t,error:e}),o.dispatch({type:u,payload:t,error:e})},r=function(e){o.dispatch({type:c,payload:t,result:e}),o.dispatch({type:u,payload:t,result:e})};o.dispatch({type:a,payload:t});try{var e=o.dispatch(function(){return i(t)});return"object"==typeof e&&"function"==typeof e.then?e.then(function(e){return r(e),e}).catch(function(e){throw n(e),e}):(r(e),e)}catch(e){throw n(e),e}};return r.type=u,r.startType=a,r.successType=c,r.failType=s,r}(h,g,I,O);ie(y,M,O),D[k.type]=k,h[K]?(L.push(h),ie(y,z,k)):ie(y,T,k)}else if(h[H]){var w=oe(R,x),E=(r=R,n=t,i=J,u=I,a=(o=h)[H],c=U(1)(o),function(e){Object.defineProperty(e,n,{configurable:!0,enumerable:!0,get:function(){var t;if(i.isInReducer)t=i.currentState;else{if(null==u.getState)return;try{t=u.getState()}catch(e){return}}var n=oe(r,t),e=a.stateResolvers.map(function(e){return e(n,t)});return c.apply(void 0,e)}})});E(w),N.push({key:t,parentPath:R,bindComputedProperty:E})}else h[G]?q.push({key:t,parentPath:R,reducer:h}):m();else Z(h)?(null==oe(y,x)&&ie(y,x,{}),C(h,y)):m();else X.push((void 0===(e=h)&&(e={}),{path:R,config:{blacklist:e.blacklist||[],mergeStrategy:e.mergeStrategy||"merge",storage:ae(e.storage,e.transformers),whitelist:e.whitelist||[]}}))})}(e,[]),o=T,i=D,u=t,L.forEach(function(e){var n=e[F]||e[K],t=n.targetResolver(oe(n.parent,o),o),r=(Array.isArray(t)?t:[t]).reduce(function(e,t){return"function"==typeof t&&t.type&&i[t.type]?e.push(t.type):"string"==typeof t&&e.push(t),e},[]);(n.resolvedTargets=r).forEach(function(e){var t=u[e]||[];t.push(i[n.type]),u[e]=t})}),{actionCreatorDict:D,actionCreators:T,actionReducersDict:_,computedProperties:N,customReducers:q,computedState:J,defaultState:x,listenerActionCreators:z,listenerActionMap:t,persistenceConfig:X}}function b(e,t){void 0===t&&(t={});var a,c,r,o,s,f,n=ne(e),i=t,u=i.compose,l=i.devTools,d=void 0===l||l,p=i.disableImmer,v=void 0!==p&&p,h=i.enhancers,y=void 0===h?[]:h,g=i.initialState,m=void 0===g?{}:g,S=i.injections,b=i.middleware,O=void 0===b?[]:b,k=i.mockActions,w=void 0!==k&&k,E=i.name,C=void 0===E?"EasyPeasyStore":E,P=i.reducerEnhancer,R=void 0===P?function(e){return e}:P,j=function(e){return ee({},e,{easyPeasyReplaceState:te(function(e,t){return t})})},A={},I=j(n),x=[],D=function(e){return"["+C+"]@"+e.join(".")},T=(a=D,c=A,$(function(){c.internals.persistenceConfig.forEach(function(e){var n=e.path,t=e.config,r=t.storage,o=t.whitelist,i=t.blacklist,u=c.getState();ce(ne(oe(n,u)),o,i).forEach(function(e){var t=[].concat(n,[e]);r.setItem(a(t),oe(t,u))})})},1e3)),_=(r=T,o=A,function(){return function(n){return function(e){var t=n(e);return e&&"@action.easyPeasyReplaceState"!==e.type&&0<o.internals.persistenceConfig.length&&r(t),t}}}),M=(s=D,f=A,function(){return new Promise(function(a,c){f.internals.persistenceConfig.forEach(function(e){var n=e.path,t=e.config,r=t.storage,o=t.whitelist,i=t.blacklist,u=ce(oe(n,f.getState()),o,i);0<u.length?Promise.all(u.map(function(e){var t=[].concat(n,[e]);return r.removeItem(s(t))})).then(function(){return a()},c):a()})})}),N=function(e){return A.internals.actionCreatorDict["@action.easyPeasyReplaceState"](e)},q=function(e){var t,n,r,o,i,u,a,c,s,f,l,d,p;void 0===e&&(e={}),A.internals=(n=(t={disableImmer:v,initialState:e,injections:S,model:I,reducerEnhancer:R,references:A}).disableImmer,r=t.initialState,o=t.injections,i=t.model,u=t.reducerEnhancer,a=se(i,r,o,t.references),c=a.actionCreatorDict,s=a.actionCreators,f=a.actionReducersDict,l=a.computedState,d=a.computedProperties,p=a.customReducers,{actionCreatorDict:c,actionCreators:s,computedProperties:d,computedState:l,defaultState:a.defaultState,listenerActionCreators:a.listenerActionCreators,listenerActionMap:a.listenerActionMap,persistenceConfig:a.persistenceConfig,reducer:u(ue(n,f,p,d))})},z=u||(d&&"undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__?window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({name:C}):W.compose);q(m);var L,X,J=[(X=A,function(n){return function(t){return function(e){return X.internals.computedState.currentState=n.getState(),X.internals.computedState.isInReducer=!0,t(e)}}}),Y].concat(O,[(L=A,function(){return function(r){return function(t){var e=r(t);if(t&&L.internals.listenerActionMap[t.type]&&0<L.internals.listenerActionMap[t.type].length){var n=L.internals.actionCreatorDict[t.type];L.internals.listenerActionMap[t.type].forEach(function(e){e({type:n?n.type:t.type,payload:t.payload,error:t.error,result:t.result})})}return e}}}),_]);w&&J.push(function(){return function(){return function(e){null!=e&&x.push(e)}}});var U=W.createStore(A.internals.reducer,A.internals.defaultState,z.apply(void 0,[W.applyMiddleware.apply(void 0,J)].concat(y)));U.subscribe(function(){A.internals.computedState.isInReducer=!1}),A.dispatch=U.dispatch,A.getState=U.getState;var V=function(){Object.keys(U.dispatch).forEach(function(e){delete U.dispatch[e]}),Object.keys(A.internals.actionCreators).forEach(function(e){U.dispatch[e]=A.internals.actionCreators[e]})};V();var F,H,B,G,K=function(e){var t=U.getState();e&&delete t[e],q(t),U.replaceReducer(A.internals.reducer),N(A.internals.defaultState),V()},Q=(F=D,H=N,B=A,G=Promise.resolve(),0<B.internals.persistenceConfig.length&&B.internals.persistenceConfig.forEach(function(e){var o=e.path,t=e.config,n=t.blacklist,r=t.mergeStrategy,i=t.storage,u=t.whitelist,a=B.internals.defaultState,c=ce(ne(oe(o,a)),u,n),s=function(e,t){if("overwrite"===r)ie(o,e,t);else if("merge"===r){var n=oe(o,e);Object.keys(t).forEach(function(e){n[e]=t[e]})}else"mergeDeep"===r&&function n(r,o){Object.keys(o).forEach(function(e){var t=o[e];Z(t)?(Z(r[e])||(r[e]={}),n(r[e],t)):r[e]=t})}(oe(o,e),t)};if(i.isAsync){var f=c.reduce(function(e,t){var n=[].concat(o,[t]),r=i.getItem(F(n));return re(r)&&e.push({key:t,dataPromise:r}),e},[]);0<f.length&&(G=Promise.all(f.map(function(e){return e.dataPromise})).then(function(e){var t=e.reduce(function(e,t,n){var r=f[n].key;return void 0!==t&&(e[r]=t),e},{});0!==Object.keys(t).length&&(s(a,t),H(a))}))}else{var l=c.reduce(function(e,t){var n=[].concat(o,[t]),r=i.getItem(F(n));return void 0!==r&&(e[t]=r),e},{});s(a,l),H(a)}}),G);return Object.assign(U,{addModel:function(e,t){I[e],I[e]=t,K()},clearMockedActions:function(){x=[]},getActions:function(){return A.internals.actionCreators},getListeners:function(){return A.internals.listenerActionCreators},getMockedActions:function(){return[].concat(x)},persist:{clear:M,flush:function(){return T.flush()},resolveRehydration:function(){return Q}},reconfigure:function(e){I=j(e),K()},removeModel:function(e){I[e]&&(delete I[e],K(e))}})}d.setAutoFreeze(!1),e.StoreProvider=function(e){var t=e.children,n=e.store;return a.createElement(r.Provider,{value:n},t)},e.action=te,e.actionOn=function(e,t){return t[F]={targetResolver:e},t},e.computed=function(e,t){return"function"==typeof t?(t[H]={stateResolvers:e},t):(e[H]={stateResolvers:h},e)},e.createComponentStore=function(u,a){return function(e){var t=s.useMemo(function(){return b("function"==typeof u?u(e):u,a)},[]),n=s.useRef(t.getState()),r=s.useState(function(){return t.getState()}),o=r[0],i=r[1];return s.useEffect(function(){return t.subscribe(function(){var e=t.getState();n.current!==e&&(n.current=e,i(e))})},[t]),[o,t.getActions()]}},e.createContextStore=function(o,i){var u=s.createContext();return{Provider:function(e){var t=e.children,n=e.initialData,r=s.useMemo(function(){return b("function"==typeof o?o(n):o,i)},[]);return a.createElement(u.Provider,{value:r},t)},useStore:function(){return s.useContext(u)},useStoreState:t(u),useStoreActions:c(u),useStoreDispatch:l(u),useStoreRehydrated:p(u)}},e.createStore=b,e.createTransform=function(r,o,e){void 0===e&&(e={});var t=e.whitelist||null,n=e.blacklist||null;function i(e){return!(!t||-1!==t.indexOf(e))||!(!n||-1===n.indexOf(e))}return{in:function(e,t,n){return!i(t)&&r?r(e,t,n):e},out:function(e,t,n){return!i(t)&&o?o(e,t,n):e}}},e.createTypedHooks=function(){return{useStoreActions:o,useStoreDispatch:i,useStoreState:n,useStoreRehydrated:v,useStore:u}},e.debug=function(e){return d.isDraft(e)?d.original(e):e},e.memo=function(e,t){return U(t)(e)},e.persist=function(e,t){var n;return ee({},e,((n={})[B]=t,n))},e.reducer=function(e){return e[G]={},e},e.thunk=function(e){return e[Q]={},e},e.thunkOn=function(e,t){return t[K]={targetResolver:e},t},e.useStore=u,e.useStoreActions=o,e.useStoreDispatch=i,e.useStoreRehydrated=v,e.useStoreState=n,Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=easy-peasy.umd.js.map