!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t("undefined"==typeof angular?require("angular"):angular):"function"==typeof define&&define.amd?define("angular-cache",["angular"],t):e.angularCacheModuleName=t(e.angular)}(this,function(e){"use strict";function t(e,t){if(e||(e=function(e){return e}),t||(t=function(e,t){return e===t}),"function"!=typeof e)throw new Error('BinaryHeap([weightFunc][, compareFunc]): "weightFunc" must be a function!');if("function"!=typeof t)throw new Error('BinaryHeap([weightFunc][, compareFunc]): "compareFunc" must be a function!');this.weightFunc=e,this.compareFunc=t,this.heap=[]}function r(e){var t=[],r=void 0;if(!d.isObject(e))return t;for(r in e)e.hasOwnProperty(r)&&t.push(r);return t}function i(e){return e&&"function"==typeof e.then}function s(e){return d.isNumber(e)?e.toString():e}function n(e){var t={},r=void 0;if(!d.isObject(e))return t;for(r in e)e.hasOwnProperty(r)&&(t[r]=r);return t}function o(e,o){if(e in g)throw new Error(e+" already exists!");if(!d.isString(e))throw new Error("cacheId must be a string!");var a={},c={},l=null,h=new t(function(e){return e.expires},d.equals),f=new t(function(e){return e.accessed},d.equals),p=g[e]={$$id:e,destroy:function(){clearInterval(this.$$cacheFlushIntervalId),clearInterval(this.$$recycleFreqId),this.removeAll(),l&&(l().removeItem(this.$$prefix+".keys"),l().removeItem(this.$$prefix)),l=null,a=null,f=null,h=null,this.$$prefix=null,delete g[this.$$id]},disable:function(){this.$$disabled=!0},enable:function(){delete this.$$disabled},get:function(e,t){var r=this;if(Array.isArray(e)){var i=function(){var i=e,s=[];return i.forEach(function(e){var i=r.get(e,t);null!==i&&void 0!==i&&s.push(i)}),{v:s}}();if("object"===("undefined"==typeof i?"undefined":u["typeof"](i)))return i.v}else if(e=s(e),this.$$disabled)return;if(t=t||{},!d.isString(e))throw new Error("key must be a string!");if(t&&!d.isObject(t))throw new Error("options must be an object!");if(t.onExpire&&!d.isFunction(t.onExpire))throw new Error("options.onExpire must be a function!");var n=void 0;if(l){if(c[e])return c[e];var o=l().getItem(this.$$prefix+".data."+e);if(!o)return;n=d.fromJson(o)}else if(d.isObject(a)){if(!(e in a))return;n=a[e]}var h=n.value,p=(new Date).getTime();return l?(f.remove({key:e,accessed:n.accessed}),n.accessed=p,f.push({key:e,accessed:p})):(f.remove(n),n.accessed=p,f.push(n)),"passive"===this.$$deleteOnExpire&&"expires"in n&&n.expires
(t.maxAge||this.$$maxAge)}):void 0}return d.isObject(a)&&e in a?(t=a[e],{created:t.created,accessed:t.accessed,expires:t.expires,isExpired:(new Date).getTime()-t.created>(t.maxAge||this.$$maxAge)}):void 0}return{id:this.$$id,capacity:this.$$capacity,maxAge:this.$$maxAge,deleteOnExpire:this.$$deleteOnExpire,onExpire:this.$$onExpire,cacheFlushInterval:this.$$cacheFlushInterval,recycleFreq:this.$$recycleFreq,storageMode:this.$$storageMode,storageImpl:l?l():void 0,disabled:!!this.$$disabled,size:f&&f.size()||0}},keys:function(){if(l){var e=l().getItem(this.$$prefix+".keys");return e?d.fromJson(e):[]}return r(a)},keySet:function(){if(l){var e=l().getItem(this.$$prefix+".keys"),t={};if(e)for(var r=d.fromJson(e),i=0;ithis.$$capacity&&this.remove(f.peek().key),t}},remove:function(e){if(e+="",delete c[e],l){var t=l().getItem(this.$$prefix+".data."+e);if(t){var r=d.fromJson(t);f.remove({key:e,accessed:r.accessed}),h.remove({key:e,expires:r.expires}),l().removeItem(this.$$prefix+".data."+e);var i=l().getItem(this.$$prefix+".keys"),s=i?d.fromJson(i):[],n=s.indexOf(e);return n>=0&&s.splice(n,1),l().setItem(this.$$prefix+".keys",JSON.stringify(s)),r.value}}else if(d.isObject(a)){var o=a[e]?a[e].value:void 0;return f.remove(a[e]),h.remove(a[e]),a[e]=null,delete a[e],o}},removeAll:function(){if(l){f.removeAll(),h.removeAll();var e=l().getItem(this.$$prefix+".keys");if(e)for(var t=d.fromJson(e),r=0;re)throw new Error("cacheFlushInterval must be greater than zero!");e!==t.$$cacheFlushInterval&&(t.$$cacheFlushInterval=e,clearInterval(t.$$cacheFlushIntervalId),t.$$cacheFlushIntervalId=setInterval(function(){t.removeAll()},t.$$cacheFlushInterval))}},setCapacity:function(e){if(null===e)delete this.$$capacity;else{if(!d.isNumber(e))throw new Error("capacity must be a number!");if(0>e)throw new Error("capacity must be greater than zero!");this.$$capacity=e}for(var t={};f.size()>this.$$capacity;)t[f.peek().key]=this.remove(f.peek().key);return t},setDeleteOnExpire:function(e,t){if(null===e)delete this.$$deleteOnExpire;else{if(!d.isString(e))throw new Error("deleteOnExpire must be a string!");if("none"!==e&&"passive"!==e&&"aggressive"!==e)throw new Error('deleteOnExpire must be "none", "passive" or "aggressive"!');this.$$deleteOnExpire=e}t!==!1&&this.setRecycleFreq(this.$$recycleFreq)},setMaxAge:function(e){if(null===e)this.$$maxAge=Number.MAX_VALUE;else{if(!d.isNumber(e))throw new Error("maxAge must be a number!");if(0>e)throw new Error("maxAge must be greater than zero!");this.$$maxAge=e}var t=void 0,i=void 0,s=void 0;if(h.removeAll(),l){var n=l().getItem(this.$$prefix+".keys");for(i=n?d.fromJson(n):[],t=0;te)throw new Error("recycleFreq must be greater than zero!");this.$$recycleFreq=e}clearInterval(this.$$recycleFreqId),"aggressive"===this.$$deleteOnExpire?!function(e){e.$$recycleFreqId=setInterval(function(){e.removeExpired()},e.$$recycleFreq)}(this):delete this.$$recycleFreqId},setStorageMode:function(e,t){function r(e,t){var r=this.keys(),i=r.length;if(i){for(var s=void 0,a=d.isObject(t),c=0;i>c;c++){if(s=r[c],e){var l=e().getItem(this.$$prefix+".data."+s);l&&(o[s]=d.fromJson(l))}else a&&(o[s]=t[s]);this.remove(s)}n=!0}}if(!d.isString(e))throw new Error("storageMode must be a string!");if("memory"!==e&&"localStorage"!==e&&"sessionStorage"!==e)throw new Error('storageMode must be "memory", "localStorage" or "sessionStorage"!');var i=l,s=a,n=!1,o={};if(this.$$initializing||r.call(this,i,s),this.$$storageMode=e,t){if(!d.isObject(t))throw new Error("storageImpl must be an object!");if(!("setItem"in t&&"function"==typeof t.setItem))throw new Error('storageImpl must implement "setItem(key, value)"!');if(!("getItem"in t&&"function"==typeof t.getItem))throw new Error('storageImpl must implement "getItem(key)"!');if(!("removeItem"in t)||"function"!=typeof t.removeItem)throw new Error('storageImpl must implement "removeItem(key)"!');l=function(){return t}}else if("localStorage"===this.$$storageMode)try{localStorage.setItem("cachefactory","cachefactory"),localStorage.removeItem("cachefactory"),l=function(){return localStorage}}catch(c){l=null,this.$$storageMode="memory"}else if("sessionStorage"===this.$$storageMode)try{sessionStorage.setItem("cachefactory","cachefactory"),sessionStorage.removeItem("cachefactory"),l=function(){return sessionStorage}}catch(c){l=null,this.$$storageMode="memory"}else l=null,this.$$storageMode="memory";if(this.$$initializing&&r.call(this,l,a),n){var u=void 0;for(var h in o)u=o[h],this.put(h,u.value,{created:u.created,accessed:u.accessed,expires:u.expires})}},touch:function(e,t){var r=this;if(e){var i=this.get(e,{onExpire:function(e,t){return r.put(e,t)}});i&&this.put(e,i,t)}else for(var s=this.keys(),n=0;n0;){var n=Math.floor((r+1)/2)-1,o=e[n];if(s>=t(o))break;e[n]=i,e[r]=o,r=n}},f=function(e,t,r){for(var i=e.length,s=e[r],n=t(s);;){var o=2*(r+1),a=o-1,c=null;if(i>a){var l=e[a],u=t(l);n>u&&(c=a)}if(i>o){var h=e[o],f=t(h);f<(null===c?n:t(e[a]))&&(c=o)}if(null===c)break;e[r]=e[c],e[c]=s,r=c}},p=t.prototype;p.push=function(e){this.heap.push(e),h(this.heap,this.weightFunc,this.heap.length-1)},p.peek=function(){return this.heap[0]},p.pop=function(){var e=this.heap[0],t=this.heap.pop();return this.heap.length>0&&(this.heap[0]=t,f(this.heap,this.weightFunc,0)),e},p.remove=function(e){for(var t=this.heap.length,r=0;t>r;r++)if(this.compareFunc(this.heap[r],e)){var i=this.heap[r],s=this.heap.pop();return r!==t-1&&(this.heap[r]=s,h(this.heap,this.weightFunc,r),f(this.heap,this.weightFunc,r)),i}return null},p.removeAll=function(){this.heap=[]},p.size=function(){return this.heap.length};var m=null;try{m=window.Promise}catch($){}var d={isNumber:function(e){return"number"==typeof e},isString:function(e){return"string"==typeof e},isObject:function(e){return null!==e&&"object"===("undefined"==typeof e?"undefined":u["typeof"](e))},isFunction:function(e){return"function"==typeof e},fromJson:function(e){return JSON.parse(e)},equals:function(e,t){return e===t},Promise:m},v={capacity:Number.MAX_VALUE,maxAge:Number.MAX_VALUE,deleteOnExpire:"none",onExpire:null,cacheFlushInterval:null,recycleFreq:1e3,storageMode:"memory",storageImpl:null,disabled:!1,storagePrefix:"cachefactory.caches.",storeOnResolve:!1,storeOnReject:!1},g={};a.createCache=o,a.defaults=v,a.info=function(){var e=r(g),t={size:e.length,caches:{}};for(var i in v)v.hasOwnProperty(i)&&(t[i]=v[i]);for(var s=0;s