vuex-cache
Version:
Cache dispatched actions and prevent repeated requests and heavy actions.
3 lines (2 loc) • 2.95 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).VuexCache={})}(this,(function(e){"use strict";var t,r=function(e){return!!e&&"object"==typeof e},n=function(e){return r(e[0])?[e[0].type,e[0],e[1]]:e},a=new Error("Can't generate key from parameters."),c=function(e){try{var t=n(e),c=t[0],o=t[1];return c+":"+(r(i=o)?JSON.stringify(i):String(i))}catch(e){return a}var i},o=function(e){return r(e)&&"number"==typeof e.timeout},i=function(e,t){var r="string"==typeof e[0]?e[2]:e[0];return o(r)?r.timeout:o(t)?t.timeout:0},u=function(e){return!!e&&Date.now()>e},f=new Map,l=function(e,t){var o={dispatch:function(){for(var r=[],n=arguments.length;n--;)r[n]=arguments[n];var o=c(r);if(o===a)return e.dispatch.apply(e,r);var l=f.get(o)||{},p=l.value,s=l.expiresIn;if(p&&!u(s))return p;var h=i(r,t),v={expiresIn:h?Date.now()+h:void 0,value:e.dispatch.apply(e,r)};return f.set(o,v),v.value.catch((function(e){return f.delete(o),Promise.reject(e)}))},has:function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];var n=c(e);if(n===a)return!1;var o=f.get(n);return r(o)&&!u(o.expiresIn)},clear:function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];var r=n(e),a=r[0];return a?Array.from(f.keys()).filter((function(e){return e.split(":")[0]===a})).reduce((function(e,t){return e+f.delete(t)}),0):!!f.clear()},delete:function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];var r=c(e);return r!==a&&f.delete(r)},state:function(){return f}};for(var l in Object.defineProperty(e,"cache",{value:o,writable:!1,enumerable:!0,configurable:!1}),e._modulesNamespaceMap){var s=p(e,"mapCacheActions",l);Object.defineProperty(s.context,"cache",{value:o,writable:!1,enumerable:!0,configurable:!1})}},p=function(e,t,r){return e._modulesNamespaceMap[r]},s=(t=function(e,t){var r,n={};return(r=t,Array.isArray(r)?r.map((function(e){return{key:e,val:e}})):Object.keys(r).map((function(e){return{key:e,val:r[e]}}))).forEach((function(t){var r=t.key,a=t.val;n[r]=function(){for(var t=this,r=[],n=arguments.length;n--;)r[n]=arguments[n];var c=this.$store.cache.dispatch;if(e){var o=p(this.$store,"mapCacheActions",e);if(!o)return;c="function"==typeof a?function(r){for(var n,a=[],c=arguments.length-1;c-- >0;)a[c]=arguments[c+1];(n=o.context.cache.dispatch).call.apply(n,[t.$store.cache,""+e+r].concat(a))}:o.context.cache.dispatch}return"function"==typeof a?a.call.apply(a,[this,c].concat(r)):c.call.apply(c,[this.$store.cache,""+e+a].concat(r))}})),n},function(e,r){return"string"!=typeof e?(r=e,e=""):"/"!==e.charAt(e.length-1)&&(e+="/"),t(e,r)});e.cacheAction=function(e,t){return function(r,n){return l(r,t),e.call(this,r,n)}},e.default=function(e){return function(t){return l(t,e)}},e.mapCacheActions=s,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=vuex-cache.umd.min.js.map