Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@rwsbillyang/usecache

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rwsbillyang/usecache - npm Package Compare versions

Comparing version 0.9.9 to 0.9.10

2

dist/esm/index.js

@@ -1,1 +0,1 @@

var g={OnlySessionStorage:1,OnlyLocalStorage:2,BothStorage:3,NONE:0};var n={EnableLog:!1,cacheKeyPrefix:()=>"",defaultIdentiyKey:"_id",defaultStorageType:g.OnlySessionStorage,PageSize:10},Q=()=>window.location.protocol+"//"+window.location.host;var f={getItem:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:g.OnlySessionStorage,o=arguments.length>2?arguments[2]:void 0;if(e===g.NONE)return o;let a,s=n.cacheKeyPrefix()+t;return e===g.OnlySessionStorage?a=sessionStorage.getItem(s):e===g.OnlyLocalStorage?a=localStorage.getItem(s):e===g.BothStorage&&(a=sessionStorage.getItem(s),a||(a=localStorage.getItem(s),a&&sessionStorage.setItem(s,a))),n.EnableLog&&console.log(`CacheStorage.getItem: key=${s}, defaultValue=${o}`),a||o},getObject:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:g.OnlySessionStorage,o=arguments.length>2?arguments[2]:void 0,a=f.getItem(t,e);return a?JSON.parse(a):o},saveItem:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:g.OnlySessionStorage;if(o===g.NONE)return;let a=n.cacheKeyPrefix()+t;o===g.OnlySessionStorage?sessionStorage.setItem(a,e):o===g.OnlyLocalStorage?localStorage.setItem(a,e):o===g.BothStorage&&(sessionStorage.setItem(a,e),localStorage.setItem(a,e)),n.EnableLog&&console.log(`CacheStorage.saveItem: key=${a}`)},saveObject:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:g.OnlySessionStorage;return f.saveItem(t,JSON.stringify(e),o)},remove:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:g.OnlySessionStorage;if(e===g.NONE)return;let o=n.cacheKeyPrefix()+t;e===g.OnlySessionStorage?sessionStorage.removeItem(o):e===g.OnlyLocalStorage?localStorage.removeItem(o):e===g.BothStorage&&(sessionStorage.removeItem(o),localStorage.removeItem(o))}};var j={findOne:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,l=f.getItem(t,a);if(l){let r=JSON.parse(l);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e)return r[i]}}},findMany:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,l=f.getItem(t,a);if(l){let r=JSON.parse(l);if(r&&r.length>0){for(let i=0;i<r.length;i++){let d=r[i];for(let u=0;u<e.length;u++)d[s]===e[u]&&r.push(d)}return r}}},onAddOne:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultStorageType;if(o===g.NONE)return;let a=f.getItem(t,o);if(a){let s=JSON.parse(a);s&&s.length>0?(s.unshift(e),f.saveObject(t,s)):f.saveObject(t,[e])}else f.saveObject(t,[e]);console.log("onAddOne done")},onEditOne:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,l=f.getItem(t,a);if(l){let r=JSON.parse(l);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e[s]){n.EnableLog&&console.log(`onEditOne, e[${s}]=${e[s]}, shortKey=${t}`),r[i]=e,f.saveObject(t,r);return}n.EnableLog&&console.log(`onEditOne\uFF1Anot found in list, key=${s}, shortKey=${t}`);return}}else n.EnableLog&&console.log("onEditOne\uFF1Anot found list: shortKey="+t)},onEditMany:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,l=f.getItem(t,a);if(l){let r=JSON.parse(l);if(r&&r.length>0){for(let i=0;i<e.length;i++){let d=e[i];for(let u=0;u<r.length;u++)r[u][s]===d[s]&&(r[u]=d)}f.saveItem(t,JSON.stringify(r));return}else{f.saveItem(t,JSON.stringify(e));return}}else n.EnableLog&&console.log("onEditMany: not found list, shortKey="+t)},onDelOneById:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType)===g.NONE)return;let s=o||n.defaultIdentiyKey,l=f.getItem(t);if(l){let r=JSON.parse(l);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e)return n.EnableLog&&console.log(`del one: ${s}=${e}`),r.splice(i,1),f.saveItem(t,JSON.stringify(r)),r}}},onDelOne:function(t,e){var o;let a=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(s===g.NONE)return;let l=a||n.defaultIdentiyKey,r=(o=e[l])===null||o===void 0?void 0:o.toString();r?j.onDelOneById(t,r,a,s):console.log("onDelOne: not found id by key="+l+"in entity="+JSON.stringify(e))},onDelManyByIds:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType)===g.NONE)return;let s=o||n.defaultIdentiyKey,l=f.getItem(t);if(l){let r=JSON.parse(l);if(r&&r.length>0){for(let i=0;i<r.length;i++)for(let d=0;d<e.length;d++){let u=e[d];r[i][s]===u&&(n.EnableLog&&console.log(`del one: ${s}=${u}`),r.splice(i,1))}return f.saveItem(t,JSON.stringify(r)),r}}},onDelMany:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,l=e.map(r=>{var i;return(i=r[s])===null||i===void 0?void 0:i.toString()}).filter(r=>!!r);l&&l.length>0?j.onDelManyByIds(t,l,o,a):console.log("onDelOne: not found id by key="+s+"in entity list="+JSON.stringify(e))},evictCache:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:n.defaultStorageType,o=n.cacheKeyPrefix()+t;e===g.OnlySessionStorage?sessionStorage.removeItem(o):e===g.OnlyLocalStorage?localStorage.removeItem(o):e===g.BothStorage&&(sessionStorage.removeItem(o),localStorage.removeItem(o))},evictAllCaches:function(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:n.defaultStorageType;t===g.OnlySessionStorage?sessionStorage.clear():t===g.OnlyLocalStorage?localStorage.clear():t===g.BothStorage&&(sessionStorage.clear(),localStorage.clear())}};var O;(function(t){t.OK="OK",t.KO="KO",t.NewUser="NewUser",t.TokenExpired="TokenExpired"})(O||(O={}));function x(t){if(t){if(t.code===O.OK)return t.data;console.warn("getDataFromBox:"+JSON.stringify(t));return}else{console.warn("\u51FA\u9519\u4E86\uFF0C\u8BF7\u6C42\u7ED3\u679C\u6CA1\u6709\u6570\u636E");return}}var D=t=>encodeURIComponent(JSON.stringify(t));import{useEffect as G,useState as K}from"react";function X(t,e,o){var a,s,l,r;let i=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType,d=arguments.length>4?arguments[4]:void 0,u=arguments.length>5?arguments[5]:void 0,m=arguments.length>6?arguments[6]:void 0,y=arguments.length>7?arguments[7]:void 0;q(t,e,o,i,d||((a=n.request)===null||a===void 0?void 0:a.showToast),u||((s=n.request)===null||s===void 0?void 0:s.showToast),m||((l=n.request)===null||l===void 0?void 0:l.showLoading),y||((r=n.request)===null||r===void 0?void 0:r.hideLoading))}function q(t,e,o){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType,s=arguments.length>4?arguments[4]:void 0,l=arguments.length>5?arguments[5]:void 0,r=arguments.length>6?arguments[6]:void 0,i=arguments.length>7?arguments[7]:void 0;if(o){let d=f.getObject(o,a);if(d)return n.EnableLog&&console.log("genericFetch: got value from cache, shortKey="+o),e(d),!0;n.EnableLog&&console.log("genericFetch: not found value from cache, shortKey="+o)}return r&&r(),n.EnableLog&&console.log("genericFetch: from remote server..."),t().then(d=>{i&&i();let u=d.data;if(u.code===O.OK){let m=x(u);return m===void 0?s&&(n.EnableLog&&console.log("genericFetch: no data from remote server"),s()):(o&&f.saveItem(o,JSON.stringify(m),a),e(m)),!1}else return l&&(n.EnableLog&&console.log("genericFetch: fail from remote server: code="+u.code+",msg="+u.msg),l(u.msg||u.code)),!1}).catch(d=>(i&&i(),l&&l(d.message),n.EnableLog&&console.log("genericFetch exception from remote server:",d),!1)),!1}function Y(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:!1,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!1,s=arguments.length>4&&arguments[4]!==void 0?arguments[4]:n.defaultStorageType,[l,r]=K(!1),[i,d]=K(),[u,m]=K();return G(()=>{var y,N;r(!0);let I=o?(y=n.request)===null||y===void 0?void 0:y.getWithoutAuth:(N=n.request)===null||N===void 0?void 0:N.get;if(!I)console.warn("not config request promise, please set ConfigRequest firstly");else{var T,S,h,C;a&&(!((T=n.request)!==null&&T!==void 0&&T.showLoading)||!((S=n.request)!==null&&S!==void 0&&S.hideLoading))&&console.warn("not config request showLoading/hideLoading, please set ConfigRequest firstly"),q(()=>I(t),p=>{r(!1),d(p)},e,s,()=>{r(!1),m("\u6570\u636E\u79FB\u6C11\u706B\u661F\u4E86^_^")},p=>{r(!1),m(p)},(h=n.request)===null||h===void 0?void 0:h.showLoading,(C=n.request)===null||C===void 0?void 0:C.hideLoading)}},[t]),{loading:l,entity:i,errMsg:u}}import{useEffect as Z,useRef as ee,useState as L}from"react";function te(t){if(!t)return"";t.pagination&&(t.umi=D(t.pagination)),t.pagination=void 0,n.EnableLog&&console.log("query2Params: newQuery="+JSON.stringify(t));let e=[];for(let o in t)if(o){let a=t[o];a==null||a===""?n.EnableLog&&console.log(`query2Params: no value for ${o}, ignore`):e.push(`${o}=${a}`)}return e.length>0?"?"+e.sort().join("&"):""}function ne(t,e,o){var a;let s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!0,l=arguments.length>4&&arguments[4]!==void 0?arguments[4]:n.defaultStorageType,[r,i]=L([]),[d,u]=L(!0),[m,y]=L(!1),[N,I]=L(),[T,S]=L(e?A(e):!0),[h,C]=L(),[p,W]=L(0);n.EnableLog&&console.log("call useCacheList2, wholeUrl="+h+", list="+r);let{current:v}=ee({pageSize:(o==null||(a=o.pagination)===null||a===void 0?void 0:a.pageSize)||n.PageSize,useCache:!!e,isLoadMore:!1}),J=c=>{v.useCache=c},$=c=>{v.isLoadMore=c},_=()=>{W(p+1)},V=c=>{n.EnableLog&&console.log("update query for wholeUrl...query="+JSON.stringify(c)),C(t+te(c))},F=(c,H,B,w)=>{var U;if(!B){console.warn("no whole url, please call setQuery firstly");return}n.EnableLog&&console.log("fetch from remote... url="+B);let P=(U=n.request)===null||U===void 0?void 0:U.get;if(!P){console.warn("please inject get request firstly");return}P(B).then(M=>{u(!1),J(!0);let E=M.data,b=x(E);if(E.code===O.OK)if(b){y(!1);let R=c&&r&&r.length>0?r.concat(b):b;if(i(R),e&&f.saveObject(e,R,l),s){let z=b.length>=H;S(z),e&&k(e,z)}}else y(!0),s&&(I("no data"),e&&k(e,!1));else y(!0),s&&(S(!1),e&&k(e,!1)),I(E.msg||E.code),n.EnableLog&&console.log("useCacheList: fail from remote server: code="+E.code+",msg="+E.msg);w&&w(b),$(!1)}).catch(M=>{J(!1),w&&w(),u(!1),y(!0),I(M.message),s&&(e&&k(e,!1),S(!1)),$(!1),n.EnableLog&&console.log("useCacheList exception from remote server:",M)})};return Z(()=>{if(n.EnableLog&&console.log("in useCacheList useEffect, try load from local or remote, refreshCount="+p+", wholeUrl="+h),u(!0),v.useCache&&e){let c=f.getObject(e,l);c?(n.EnableLog&&console.log("fetch from local cache... shortKey="+e),i(c),u(!1),S(A(e))):(n.EnableLog&&console.log("no local cache, try from remote..."),F(v.isLoadMore,v.pageSize,h))}else n.EnableLog&&console.log("useCache=false, try from remote..."),F(v.isLoadMore,v.pageSize,h)},[h,p]),{isLoading:d,isError:m,errMsg:N,loadMoreState:T,list:r,refreshCount:p,setList:i,fetchDataFromRemote:F,setQuery:V,setRefresh:_,setUseCache:J,setIsLoadMore:$}}function A(t){let e=n.cacheKeyPrefix()+t+"/loadMore",o=sessionStorage.getItem(e);return!(o&&o==="0")}function k(t,e){let o=n.cacheKeyPrefix()+t+"/loadMore";sessionStorage.setItem(o,e?"1":"0")}export{O as CODE,j as Cache,f as CacheStorage,g as StorageType,n as UseCacheConfig,Q as currentHost,D as encodeUmi,X as fetchWithLoading,q as genericFetch,x as getDataFromBox,Y as useCache,ne as useCacheList};
var Z=Object.defineProperty,ee=Object.defineProperties;var te=Object.getOwnPropertyDescriptors;var z=Object.getOwnPropertySymbols;var ne=Object.prototype.hasOwnProperty,oe=Object.prototype.propertyIsEnumerable;var Q=(e,t,o)=>t in e?Z(e,t,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[t]=o,W=(e,t)=>{for(var o in t||(t={}))ne.call(t,o)&&Q(e,o,t[o]);if(z)for(var o of z(t))oe.call(t,o)&&Q(e,o,t[o]);return e},_=(e,t)=>ee(e,te(t));var g={OnlySessionStorage:1,OnlyLocalStorage:2,BothStorage:3,NONE:0};var n={EnableLog:!1,cacheKeyPrefix:()=>"",defaultIdentiyKey:"_id",defaultStorageType:g.OnlySessionStorage,PageSize:10},re=()=>window.location.protocol+"//"+window.location.host;var u={getItem:function(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:g.OnlySessionStorage,o=arguments.length>2?arguments[2]:void 0;if(t===g.NONE)return o;let a,s=n.cacheKeyPrefix()+e;return t===g.OnlySessionStorage?a=sessionStorage.getItem(s):t===g.OnlyLocalStorage?a=localStorage.getItem(s):t===g.BothStorage&&(a=sessionStorage.getItem(s),a||(a=localStorage.getItem(s),a&&sessionStorage.setItem(s,a))),n.EnableLog&&console.log(`CacheStorage.getItem: key=${s}, defaultValue=${o}`),a||o},getObject:function(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:g.OnlySessionStorage,o=arguments.length>2?arguments[2]:void 0,a=u.getItem(e,t);return a?JSON.parse(a):o},saveItem:function(e,t){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:g.OnlySessionStorage;if(o===g.NONE)return;let a=n.cacheKeyPrefix()+e;o===g.OnlySessionStorage?sessionStorage.setItem(a,t):o===g.OnlyLocalStorage?localStorage.setItem(a,t):o===g.BothStorage&&(sessionStorage.setItem(a,t),localStorage.setItem(a,t)),n.EnableLog&&console.log(`CacheStorage.saveItem: key=${a}`)},saveObject:function(e,t){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:g.OnlySessionStorage;return u.saveItem(e,JSON.stringify(t),o)},remove:function(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:g.OnlySessionStorage;if(t===g.NONE)return;let o=n.cacheKeyPrefix()+e;t===g.OnlySessionStorage?sessionStorage.removeItem(o):t===g.OnlyLocalStorage?localStorage.removeItem(o):t===g.BothStorage&&(sessionStorage.removeItem(o),localStorage.removeItem(o))}};var j={findOne:function(e,t){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,i=u.getItem(e,a);if(i){let r=JSON.parse(i);if(r&&r.length>0){for(let l=0;l<r.length;l++)if(r[l][s]===t)return n.EnableLog&&console.log("Cache.findOne: found, shortKey: "+e),r[l]}}},findMany:function(e,t){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,i=u.getItem(e,a);if(i){let r=JSON.parse(i);if(r&&r.length>0){for(let l=0;l<r.length;l++){let d=r[l];for(let f=0;f<t.length;f++)d[s]===t[f]&&r.push(d)}return n.EnableLog&&console.log("Cache.findMany: found, shortKey: "+e),r}}},onAddOne:function(e,t){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultStorageType;if(o===g.NONE)return;let a=u.getItem(e,o);if(a){let s=JSON.parse(a);s&&s.length>0?(s.unshift(t),u.saveObject(e,s)):u.saveObject(e,[t])}else u.saveObject(e,[t]);n.EnableLog&&console.log("Cache.onAddOne: done, shortKey: "+e)},onEditOne:function(e,t){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,i=u.getItem(e,a);if(i){let r=JSON.parse(i);if(r&&r.length>0){for(let l=0;l<r.length;l++)if(r[l][s]===t[s]){n.EnableLog&&console.log(`Cache.onEditOne, e[${s}]=${t[s]}, shortKey: ${e}`),r[l]=t,u.saveObject(e,r);return}n.EnableLog&&console.log(`Cache.onEditOne\uFF1Anot found in list, key=${s}, shortKey: ${e}`);return}}else n.EnableLog&&console.log("Cache.onEditOne\uFF1Anot found list: shortKey: "+e)},onEditMany:function(e,t){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,i=u.getItem(e,a);if(i){let r=JSON.parse(i);if(r&&r.length>0){for(let l=0;l<t.length;l++){let d=t[l];for(let f=0;f<r.length;f++)r[f][s]===d[s]&&(r[f]=d)}u.saveItem(e,JSON.stringify(r)),n.EnableLog&&console.log("Cache.onEditMany: updateMany done, shortKey: "+e);return}else{u.saveItem(e,JSON.stringify(t)),n.EnableLog&&console.log("Cache.onEditMany: insert done, shortKey: "+e);return}}else n.EnableLog&&console.log("Cache.onEditMany: not found list, shortKey: "+e)},onDelOneById:function(e,t){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType)===g.NONE)return;let s=o||n.defaultIdentiyKey,i=u.getItem(e);if(i){let r=JSON.parse(i);if(r&&r.length>0){for(let l=0;l<r.length;l++)if(r[l][s]===t)return r.splice(l,1),u.saveItem(e,JSON.stringify(r)),n.EnableLog&&console.log(`Cache.onDelOneById: del done: ${s}=${t}, shortKey: ${e}`),r}}},onDelOne:function(e,t){var o;let a=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(s===g.NONE)return;let i=a||n.defaultIdentiyKey,r=(o=t[i])===null||o===void 0?void 0:o.toString();r&&j.onDelOneById(e,r,a,s),n.EnableLog?console.log(`Cache.onDelOne: del done: ${i}=${r}, shortKey: ${e}`):console.log("Cache.onDelOne: not found id by key="+i+"in entity="+JSON.stringify(t))},onDelManyByIds:function(e,t){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType)===g.NONE)return;let s=o||n.defaultIdentiyKey,i=u.getItem(e);if(i){let r=JSON.parse(i);if(r&&r.length>0){for(let l=0;l<r.length;l++)for(let d=0;d<t.length;d++){let f=t[d];r[l][s]===f&&(n.EnableLog&&console.log(`Cache.onDelManyByIds: del one: ${s}=${f}, shortKey: ${e}`),r.splice(l,1))}return u.saveItem(e,JSON.stringify(r)),n.EnableLog&&console.log(`Cache.onDelManyByIds: del done, shortKey: ${e}`),r}}},onDelMany:function(e,t){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,i=t.map(r=>{var l;return(l=r[s])===null||l===void 0?void 0:l.toString()}).filter(r=>!!r);i&&i.length>0?j.onDelManyByIds(e,i,o,a):n.EnableLog&&console.log("Cache.onDelOne: not found id by key="+s+"in entity list="+JSON.stringify(t))},evictCache:function(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:n.defaultStorageType,o=n.cacheKeyPrefix()+e;t===g.OnlySessionStorage?sessionStorage.removeItem(o):t===g.OnlyLocalStorage?localStorage.removeItem(o):t===g.BothStorage&&(sessionStorage.removeItem(o),localStorage.removeItem(o)),n.EnableLog&&console.log("Cache.evictCache done, shortKey: "+e)},evictAllCaches:function(){let e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:n.defaultStorageType;e===g.OnlySessionStorage?sessionStorage.clear():e===g.OnlyLocalStorage?localStorage.clear():e===g.BothStorage&&(sessionStorage.clear(),localStorage.clear()),n.EnableLog&&console.log("Cache.evictAllCaches done")}};var O;(function(e){e.OK="OK",e.KO="KO",e.NewUser="NewUser",e.TokenExpired="TokenExpired"})(O||(O={}));function x(e){if(e){if(e.code===O.OK)return e.data;console.warn("getDataFromBox:"+JSON.stringify(e));return}else{console.warn("\u51FA\u9519\u4E86\uFF0C\u8BF7\u6C42\u7ED3\u679C\u6CA1\u6709\u6570\u636E");return}}var K=e=>encodeURIComponent(JSON.stringify(e));import{useEffect as ae,useState as U}from"react";function se(e,t,o){var a,s,i,r;let l=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType,d=arguments.length>4?arguments[4]:void 0,f=arguments.length>5?arguments[5]:void 0,m=arguments.length>6?arguments[6]:void 0,y=arguments.length>7?arguments[7]:void 0;q(e,t,o,l,d||((a=n.request)===null||a===void 0?void 0:a.showToast),f||((s=n.request)===null||s===void 0?void 0:s.showToast),m||((i=n.request)===null||i===void 0?void 0:i.showLoading),y||((r=n.request)===null||r===void 0?void 0:r.hideLoading))}function q(e,t,o){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType,s=arguments.length>4?arguments[4]:void 0,i=arguments.length>5?arguments[5]:void 0,r=arguments.length>6?arguments[6]:void 0,l=arguments.length>7?arguments[7]:void 0;if(o){let d=u.getObject(o,a);if(d)return n.EnableLog&&console.log("genericFetch: got value from cache, shortKey="+o),t(d),!0;n.EnableLog&&console.log("genericFetch: not found value from cache, shortKey="+o)}return r&&r(),n.EnableLog&&console.log("genericFetch: from remote server..."),e().then(d=>{l&&l();let f=d.data;if(f.code===O.OK){let m=x(f);return m===void 0?s&&(n.EnableLog&&console.log("genericFetch: no data from remote server"),s()):(o&&u.saveItem(o,JSON.stringify(m),a),t(m)),!1}else return i&&(n.EnableLog&&console.log("genericFetch: fail from remote server: code="+f.code+",msg="+f.msg),i(f.msg||f.code)),!1}).catch(d=>(l&&l(),i&&i(d.message),n.EnableLog&&console.log("genericFetch exception from remote server:",d),!1)),!1}function le(e,t){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:!1,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!1,s=arguments.length>4&&arguments[4]!==void 0?arguments[4]:n.defaultStorageType,[i,r]=U(!1),[l,d]=U(),[f,m]=U();return ae(()=>{var y,N;r(!0);let E=o?(y=n.request)===null||y===void 0?void 0:y.getWithoutAuth:(N=n.request)===null||N===void 0?void 0:N.get;if(!E)console.warn("not config request promise, please set ConfigRequest firstly");else{var b,S,h,T;a&&(!((b=n.request)!==null&&b!==void 0&&b.showLoading)||!((S=n.request)!==null&&S!==void 0&&S.hideLoading))&&console.warn("not config request showLoading/hideLoading, please set ConfigRequest firstly"),q(()=>E(e),p=>{r(!1),d(p)},t,s,()=>{r(!1),m("\u6570\u636E\u79FB\u6C11\u706B\u661F\u4E86^_^")},p=>{r(!1),m(p)},(h=n.request)===null||h===void 0?void 0:h.showLoading,(T=n.request)===null||T===void 0?void 0:T.hideLoading)}},[e]),{loading:i,entity:l,errMsg:f}}import{useEffect as ie,useRef as ge,useState as v}from"react";function fe(e){if(!e)return"";let t=_(W({},e),{umi:e.pagination?K(e.pagination):void 0,pagination:void 0});n.EnableLog&&console.log("query2Params: newQuery="+JSON.stringify(e));let o=[];for(let a in t)if(a){let s=t[a];s==null||s===""?n.EnableLog&&console.log(`query2Params: no value for ${a}, ignore`):o.push(`${a}=${s}`)}return o.length>0?"?"+o.sort().join("&"):""}function ue(e,t,o){var a;let s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!0,i=arguments.length>4&&arguments[4]!==void 0?arguments[4]:n.defaultStorageType,[r,l]=v([]),[d,f]=v(!0),[m,y]=v(!1),[N,E]=v(),[b,S]=v(t?V(t):!0),[h,T]=v(),[p,H]=v(0);n.EnableLog&&console.log("call useCacheList, wholeUrl: "+h+", current list.length: "+r.length);let{current:L}=ge({pageSize:(o==null||(a=o.pagination)===null||a===void 0?void 0:a.pageSize)||n.PageSize,useCache:!!t,isLoadMore:!1}),k=c=>{L.useCache=c},J=c=>{L.isLoadMore=c},G=()=>{H(p+1)},X=c=>{n.EnableLog&&console.log("setQuery: "+JSON.stringify(c)),T(e+fe(c))},B=(c,Y,D,M)=>{var F;if(!D){console.warn("no whole url, please call setQuery firstly");return}n.EnableLog&&console.log("fetch from remote... url="+D);let P=(F=n.request)===null||F===void 0?void 0:F.get;if(!P){console.warn("please inject get request firstly");return}P(D).then(w=>{f(!1),k(!0);let I=w.data,C=x(I);if(I.code===O.OK)if(C){y(!1);let A=c&&r&&r.length>0?r.concat(C):C;if(l(A),t&&u.saveObject(t,A,i),s){let R=C.length>=Y;S(R),t&&$(t,R)}n.EnableLog&&console.log("return from remote server: list.length: "+C.length)}else y(!0),s&&(E("no data"),t&&$(t,!1)),n.EnableLog&&console.log("return from remote server: no data");else y(!0),s&&(S(!1),t&&$(t,!1)),E(I.msg||I.code),n.EnableLog&&console.log("remote server return err code="+I.code+",msg="+I.msg);M&&M(C),J(!1)}).catch(w=>{k(!1),M&&M(),f(!1),y(!0),E(w.message),s&&(t&&$(t,!1),S(!1)),J(!1),n.EnableLog&&console.log("useCacheList exception from remote server: ",w)})};return ie(()=>{if(n.EnableLog&&console.log("useCacheList useEffect, try load from local or remote, refreshCount="+p+", wholeUrl="+h),f(!0),L.useCache&&t){let c=u.getObject(t,i);c?(n.EnableLog&&console.log("fetch from local cache, shortKey:"+t+", list.length: "+c.length),l(c),f(!1),S(V(t))):(n.EnableLog&&console.log("no local cache, try from remote..."),B(L.isLoadMore,L.pageSize,h))}else n.EnableLog&&console.log("useCache=false, try from remote..."),B(L.isLoadMore,L.pageSize,h)},[h,p]),{isLoading:d,isError:m,errMsg:N,loadMoreState:b,list:r,refreshCount:p,setList:l,fetchDataFromRemote:B,setQuery:X,setRefresh:G,setUseCache:k,setIsLoadMore:J}}function V(e){let t=n.cacheKeyPrefix()+e+"/loadMore",o=sessionStorage.getItem(t);return!(o&&o==="0")}function $(e,t){let o=n.cacheKeyPrefix()+e+"/loadMore";sessionStorage.setItem(o,t?"1":"0")}export{O as CODE,j as Cache,u as CacheStorage,g as StorageType,n as UseCacheConfig,re as currentHost,K as encodeUmi,se as fetchWithLoading,q as genericFetch,x as getDataFromBox,le as useCache,ue as useCacheList};

@@ -1,1 +0,1 @@

"use strict";var P=Object.defineProperty;var ee=Object.getOwnPropertyDescriptor;var te=Object.getOwnPropertyNames;var ne=Object.prototype.hasOwnProperty;var oe=(t,e)=>{for(var n in e)P(t,n,{get:e[n],enumerable:!0})},re=(t,e,n,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of te(e))!ne.call(t,s)&&s!==n&&P(t,s,{get:()=>e[s],enumerable:!(a=ee(e,s))||a.enumerable});return t};var ae=t=>re(P({},"__esModule",{value:!0}),t);var ie={};oe(ie,{CODE:()=>p,Cache:()=>J,CacheStorage:()=>f,StorageType:()=>g,UseCacheConfig:()=>o,currentHost:()=>W,encodeUmi:()=>$,fetchWithLoading:()=>_,genericFetch:()=>F,getDataFromBox:()=>N,useCache:()=>V,useCacheList:()=>Q});module.exports=ae(ie);var g={OnlySessionStorage:1,OnlyLocalStorage:2,BothStorage:3,NONE:0};var o={EnableLog:!1,cacheKeyPrefix:()=>"",defaultIdentiyKey:"_id",defaultStorageType:g.OnlySessionStorage,PageSize:10},W=()=>window.location.protocol+"//"+window.location.host;var f={getItem:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:g.OnlySessionStorage,n=arguments.length>2?arguments[2]:void 0;if(e===g.NONE)return n;let a,s=o.cacheKeyPrefix()+t;return e===g.OnlySessionStorage?a=sessionStorage.getItem(s):e===g.OnlyLocalStorage?a=localStorage.getItem(s):e===g.BothStorage&&(a=sessionStorage.getItem(s),a||(a=localStorage.getItem(s),a&&sessionStorage.setItem(s,a))),o.EnableLog&&console.log(`CacheStorage.getItem: key=${s}, defaultValue=${n}`),a||n},getObject:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:g.OnlySessionStorage,n=arguments.length>2?arguments[2]:void 0,a=f.getItem(t,e);return a?JSON.parse(a):n},saveItem:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:g.OnlySessionStorage;if(n===g.NONE)return;let a=o.cacheKeyPrefix()+t;n===g.OnlySessionStorage?sessionStorage.setItem(a,e):n===g.OnlyLocalStorage?localStorage.setItem(a,e):n===g.BothStorage&&(sessionStorage.setItem(a,e),localStorage.setItem(a,e)),o.EnableLog&&console.log(`CacheStorage.saveItem: key=${a}`)},saveObject:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:g.OnlySessionStorage;return f.saveItem(t,JSON.stringify(e),n)},remove:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:g.OnlySessionStorage;if(e===g.NONE)return;let n=o.cacheKeyPrefix()+t;e===g.OnlySessionStorage?sessionStorage.removeItem(n):e===g.OnlyLocalStorage?localStorage.removeItem(n):e===g.BothStorage&&(sessionStorage.removeItem(n),localStorage.removeItem(n))}};var J={findOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===g.NONE)return;let s=n||o.defaultIdentiyKey,l=f.getItem(t,a);if(l){let r=JSON.parse(l);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e)return r[i]}}},findMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===g.NONE)return;let s=n||o.defaultIdentiyKey,l=f.getItem(t,a);if(l){let r=JSON.parse(l);if(r&&r.length>0){for(let i=0;i<r.length;i++){let d=r[i];for(let u=0;u<e.length;u++)d[s]===e[u]&&r.push(d)}return r}}},onAddOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultStorageType;if(n===g.NONE)return;let a=f.getItem(t,n);if(a){let s=JSON.parse(a);s&&s.length>0?(s.unshift(e),f.saveObject(t,s)):f.saveObject(t,[e])}else f.saveObject(t,[e]);console.log("onAddOne done")},onEditOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===g.NONE)return;let s=n||o.defaultIdentiyKey,l=f.getItem(t,a);if(l){let r=JSON.parse(l);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e[s]){o.EnableLog&&console.log(`onEditOne, e[${s}]=${e[s]}, shortKey=${t}`),r[i]=e,f.saveObject(t,r);return}o.EnableLog&&console.log(`onEditOne\uFF1Anot found in list, key=${s}, shortKey=${t}`);return}}else o.EnableLog&&console.log("onEditOne\uFF1Anot found list: shortKey="+t)},onEditMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===g.NONE)return;let s=n||o.defaultIdentiyKey,l=f.getItem(t,a);if(l){let r=JSON.parse(l);if(r&&r.length>0){for(let i=0;i<e.length;i++){let d=e[i];for(let u=0;u<r.length;u++)r[u][s]===d[s]&&(r[u]=d)}f.saveItem(t,JSON.stringify(r));return}else{f.saveItem(t,JSON.stringify(e));return}}else o.EnableLog&&console.log("onEditMany: not found list, shortKey="+t)},onDelOneById:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType)===g.NONE)return;let s=n||o.defaultIdentiyKey,l=f.getItem(t);if(l){let r=JSON.parse(l);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e)return o.EnableLog&&console.log(`del one: ${s}=${e}`),r.splice(i,1),f.saveItem(t,JSON.stringify(r)),r}}},onDelOne:function(t,e){var n;let a=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(s===g.NONE)return;let l=a||o.defaultIdentiyKey,r=(n=e[l])===null||n===void 0?void 0:n.toString();r?J.onDelOneById(t,r,a,s):console.log("onDelOne: not found id by key="+l+"in entity="+JSON.stringify(e))},onDelManyByIds:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType)===g.NONE)return;let s=n||o.defaultIdentiyKey,l=f.getItem(t);if(l){let r=JSON.parse(l);if(r&&r.length>0){for(let i=0;i<r.length;i++)for(let d=0;d<e.length;d++){let u=e[d];r[i][s]===u&&(o.EnableLog&&console.log(`del one: ${s}=${u}`),r.splice(i,1))}return f.saveItem(t,JSON.stringify(r)),r}}},onDelMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===g.NONE)return;let s=n||o.defaultIdentiyKey,l=e.map(r=>{var i;return(i=r[s])===null||i===void 0?void 0:i.toString()}).filter(r=>!!r);l&&l.length>0?J.onDelManyByIds(t,l,n,a):console.log("onDelOne: not found id by key="+s+"in entity list="+JSON.stringify(e))},evictCache:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:o.defaultStorageType,n=o.cacheKeyPrefix()+t;e===g.OnlySessionStorage?sessionStorage.removeItem(n):e===g.OnlyLocalStorage?localStorage.removeItem(n):e===g.BothStorage&&(sessionStorage.removeItem(n),localStorage.removeItem(n))},evictAllCaches:function(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:o.defaultStorageType;t===g.OnlySessionStorage?sessionStorage.clear():t===g.OnlyLocalStorage?localStorage.clear():t===g.BothStorage&&(sessionStorage.clear(),localStorage.clear())}};var p;(function(t){t.OK="OK",t.KO="KO",t.NewUser="NewUser",t.TokenExpired="TokenExpired"})(p||(p={}));function N(t){if(t){if(t.code===p.OK)return t.data;console.warn("getDataFromBox:"+JSON.stringify(t));return}else{console.warn("\u51FA\u9519\u4E86\uFF0C\u8BF7\u6C42\u7ED3\u679C\u6CA1\u6709\u6570\u636E");return}}var $=t=>encodeURIComponent(JSON.stringify(t));var T=require("react");function _(t,e,n){var a,s,l,r;let i=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType,d=arguments.length>4?arguments[4]:void 0,u=arguments.length>5?arguments[5]:void 0,y=arguments.length>6?arguments[6]:void 0,h=arguments.length>7?arguments[7]:void 0;F(t,e,n,i,d||((a=o.request)===null||a===void 0?void 0:a.showToast),u||((s=o.request)===null||s===void 0?void 0:s.showToast),y||((l=o.request)===null||l===void 0?void 0:l.showLoading),h||((r=o.request)===null||r===void 0?void 0:r.hideLoading))}function F(t,e,n){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType,s=arguments.length>4?arguments[4]:void 0,l=arguments.length>5?arguments[5]:void 0,r=arguments.length>6?arguments[6]:void 0,i=arguments.length>7?arguments[7]:void 0;if(n){let d=f.getObject(n,a);if(d)return o.EnableLog&&console.log("genericFetch: got value from cache, shortKey="+n),e(d),!0;o.EnableLog&&console.log("genericFetch: not found value from cache, shortKey="+n)}return r&&r(),o.EnableLog&&console.log("genericFetch: from remote server..."),t().then(d=>{i&&i();let u=d.data;if(u.code===p.OK){let y=N(u);return y===void 0?s&&(o.EnableLog&&console.log("genericFetch: no data from remote server"),s()):(n&&f.saveItem(n,JSON.stringify(y),a),e(y)),!1}else return l&&(o.EnableLog&&console.log("genericFetch: fail from remote server: code="+u.code+",msg="+u.msg),l(u.msg||u.code)),!1}).catch(d=>(i&&i(),l&&l(d.message),o.EnableLog&&console.log("genericFetch exception from remote server:",d),!1)),!1}function V(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:!1,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!1,s=arguments.length>4&&arguments[4]!==void 0?arguments[4]:o.defaultStorageType,[l,r]=(0,T.useState)(!1),[i,d]=(0,T.useState)(),[u,y]=(0,T.useState)();return(0,T.useEffect)(()=>{var h,C;r(!0);let I=n?(h=o.request)===null||h===void 0?void 0:h.getWithoutAuth:(C=o.request)===null||C===void 0?void 0:C.get;if(!I)console.warn("not config request promise, please set ConfigRequest firstly");else{var b,O,S,x;a&&(!((b=o.request)!==null&&b!==void 0&&b.showLoading)||!((O=o.request)!==null&&O!==void 0&&O.hideLoading))&&console.warn("not config request showLoading/hideLoading, please set ConfigRequest firstly"),F(()=>I(t),v=>{r(!1),d(v)},e,s,()=>{r(!1),y("\u6570\u636E\u79FB\u6C11\u706B\u661F\u4E86^_^")},v=>{r(!1),y(v)},(S=o.request)===null||S===void 0?void 0:S.showLoading,(x=o.request)===null||x===void 0?void 0:x.hideLoading)}},[t]),{loading:l,entity:i,errMsg:u}}var c=require("react");function se(t){if(!t)return"";t.pagination&&(t.umi=$(t.pagination)),t.pagination=void 0,o.EnableLog&&console.log("query2Params: newQuery="+JSON.stringify(t));let e=[];for(let n in t)if(n){let a=t[n];a==null||a===""?o.EnableLog&&console.log(`query2Params: no value for ${n}, ignore`):e.push(`${n}=${a}`)}return e.length>0?"?"+e.sort().join("&"):""}function Q(t,e,n){var a;let s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!0,l=arguments.length>4&&arguments[4]!==void 0?arguments[4]:o.defaultStorageType,[r,i]=(0,c.useState)([]),[d,u]=(0,c.useState)(!0),[y,h]=(0,c.useState)(!1),[C,I]=(0,c.useState)(),[b,O]=(0,c.useState)(e?H(e):!0),[S,x]=(0,c.useState)(),[v,G]=(0,c.useState)(0);o.EnableLog&&console.log("call useCacheList2, wholeUrl="+S+", list="+r);let{current:L}=(0,c.useRef)({pageSize:(n==null||(a=n.pagination)===null||a===void 0?void 0:a.pageSize)||o.PageSize,useCache:!!e,isLoadMore:!1}),U=m=>{L.useCache=m},j=m=>{L.isLoadMore=m},X=()=>{G(v+1)},Y=m=>{o.EnableLog&&console.log("update query for wholeUrl...query="+JSON.stringify(m)),x(t+se(m))},D=(m,Z,K,M)=>{var q;if(!K){console.warn("no whole url, please call setQuery firstly");return}o.EnableLog&&console.log("fetch from remote... url="+K);let R=(q=o.request)===null||q===void 0?void 0:q.get;if(!R){console.warn("please inject get request firstly");return}R(K).then(k=>{u(!1),U(!0);let E=k.data,w=N(E);if(E.code===p.OK)if(w){h(!1);let z=m&&r&&r.length>0?r.concat(w):w;if(i(z),e&&f.saveObject(e,z,l),s){let A=w.length>=Z;O(A),e&&B(e,A)}}else h(!0),s&&(I("no data"),e&&B(e,!1));else h(!0),s&&(O(!1),e&&B(e,!1)),I(E.msg||E.code),o.EnableLog&&console.log("useCacheList: fail from remote server: code="+E.code+",msg="+E.msg);M&&M(w),j(!1)}).catch(k=>{U(!1),M&&M(),u(!1),h(!0),I(k.message),s&&(e&&B(e,!1),O(!1)),j(!1),o.EnableLog&&console.log("useCacheList exception from remote server:",k)})};return(0,c.useEffect)(()=>{if(o.EnableLog&&console.log("in useCacheList useEffect, try load from local or remote, refreshCount="+v+", wholeUrl="+S),u(!0),L.useCache&&e){let m=f.getObject(e,l);m?(o.EnableLog&&console.log("fetch from local cache... shortKey="+e),i(m),u(!1),O(H(e))):(o.EnableLog&&console.log("no local cache, try from remote..."),D(L.isLoadMore,L.pageSize,S))}else o.EnableLog&&console.log("useCache=false, try from remote..."),D(L.isLoadMore,L.pageSize,S)},[S,v]),{isLoading:d,isError:y,errMsg:C,loadMoreState:b,list:r,refreshCount:v,setList:i,fetchDataFromRemote:D,setQuery:Y,setRefresh:X,setUseCache:U,setIsLoadMore:j}}function H(t){let e=o.cacheKeyPrefix()+t+"/loadMore",n=sessionStorage.getItem(e);return!(n&&n==="0")}function B(t,e){let n=o.cacheKeyPrefix()+t+"/loadMore";sessionStorage.setItem(n,e?"1":"0")}0&&(module.exports={CODE,Cache,CacheStorage,StorageType,UseCacheConfig,currentHost,encodeUmi,fetchWithLoading,genericFetch,getDataFromBox,useCache,useCacheList});
"use strict";var k=Object.defineProperty,ae=Object.defineProperties,se=Object.getOwnPropertyDescriptor,le=Object.getOwnPropertyDescriptors,ie=Object.getOwnPropertyNames,Q=Object.getOwnPropertySymbols;var _=Object.prototype.hasOwnProperty,ge=Object.prototype.propertyIsEnumerable;var W=(t,e,o)=>e in t?k(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o,V=(t,e)=>{for(var o in e||(e={}))_.call(e,o)&&W(t,o,e[o]);if(Q)for(var o of Q(e))ge.call(e,o)&&W(t,o,e[o]);return t},H=(t,e)=>ae(t,le(e));var fe=(t,e)=>{for(var o in e)k(t,o,{get:e[o],enumerable:!0})},ue=(t,e,o,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of ie(e))!_.call(t,s)&&s!==o&&k(t,s,{get:()=>e[s],enumerable:!(a=se(e,s))||a.enumerable});return t};var de=t=>ue(k({},"__esModule",{value:!0}),t);var me={};fe(me,{CODE:()=>p,Cache:()=>J,CacheStorage:()=>u,StorageType:()=>g,UseCacheConfig:()=>n,currentHost:()=>G,encodeUmi:()=>B,fetchWithLoading:()=>X,genericFetch:()=>D,getDataFromBox:()=>N,useCache:()=>Y,useCacheList:()=>ee});module.exports=de(me);var g={OnlySessionStorage:1,OnlyLocalStorage:2,BothStorage:3,NONE:0};var n={EnableLog:!1,cacheKeyPrefix:()=>"",defaultIdentiyKey:"_id",defaultStorageType:g.OnlySessionStorage,PageSize:10},G=()=>window.location.protocol+"//"+window.location.host;var u={getItem:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:g.OnlySessionStorage,o=arguments.length>2?arguments[2]:void 0;if(e===g.NONE)return o;let a,s=n.cacheKeyPrefix()+t;return e===g.OnlySessionStorage?a=sessionStorage.getItem(s):e===g.OnlyLocalStorage?a=localStorage.getItem(s):e===g.BothStorage&&(a=sessionStorage.getItem(s),a||(a=localStorage.getItem(s),a&&sessionStorage.setItem(s,a))),n.EnableLog&&console.log(`CacheStorage.getItem: key=${s}, defaultValue=${o}`),a||o},getObject:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:g.OnlySessionStorage,o=arguments.length>2?arguments[2]:void 0,a=u.getItem(t,e);return a?JSON.parse(a):o},saveItem:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:g.OnlySessionStorage;if(o===g.NONE)return;let a=n.cacheKeyPrefix()+t;o===g.OnlySessionStorage?sessionStorage.setItem(a,e):o===g.OnlyLocalStorage?localStorage.setItem(a,e):o===g.BothStorage&&(sessionStorage.setItem(a,e),localStorage.setItem(a,e)),n.EnableLog&&console.log(`CacheStorage.saveItem: key=${a}`)},saveObject:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:g.OnlySessionStorage;return u.saveItem(t,JSON.stringify(e),o)},remove:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:g.OnlySessionStorage;if(e===g.NONE)return;let o=n.cacheKeyPrefix()+t;e===g.OnlySessionStorage?sessionStorage.removeItem(o):e===g.OnlyLocalStorage?localStorage.removeItem(o):e===g.BothStorage&&(sessionStorage.removeItem(o),localStorage.removeItem(o))}};var J={findOne:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,i=u.getItem(t,a);if(i){let r=JSON.parse(i);if(r&&r.length>0){for(let l=0;l<r.length;l++)if(r[l][s]===e)return n.EnableLog&&console.log("Cache.findOne: found, shortKey: "+t),r[l]}}},findMany:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,i=u.getItem(t,a);if(i){let r=JSON.parse(i);if(r&&r.length>0){for(let l=0;l<r.length;l++){let d=r[l];for(let f=0;f<e.length;f++)d[s]===e[f]&&r.push(d)}return n.EnableLog&&console.log("Cache.findMany: found, shortKey: "+t),r}}},onAddOne:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultStorageType;if(o===g.NONE)return;let a=u.getItem(t,o);if(a){let s=JSON.parse(a);s&&s.length>0?(s.unshift(e),u.saveObject(t,s)):u.saveObject(t,[e])}else u.saveObject(t,[e]);n.EnableLog&&console.log("Cache.onAddOne: done, shortKey: "+t)},onEditOne:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,i=u.getItem(t,a);if(i){let r=JSON.parse(i);if(r&&r.length>0){for(let l=0;l<r.length;l++)if(r[l][s]===e[s]){n.EnableLog&&console.log(`Cache.onEditOne, e[${s}]=${e[s]}, shortKey: ${t}`),r[l]=e,u.saveObject(t,r);return}n.EnableLog&&console.log(`Cache.onEditOne\uFF1Anot found in list, key=${s}, shortKey: ${t}`);return}}else n.EnableLog&&console.log("Cache.onEditOne\uFF1Anot found list: shortKey: "+t)},onEditMany:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,i=u.getItem(t,a);if(i){let r=JSON.parse(i);if(r&&r.length>0){for(let l=0;l<e.length;l++){let d=e[l];for(let f=0;f<r.length;f++)r[f][s]===d[s]&&(r[f]=d)}u.saveItem(t,JSON.stringify(r)),n.EnableLog&&console.log("Cache.onEditMany: updateMany done, shortKey: "+t);return}else{u.saveItem(t,JSON.stringify(e)),n.EnableLog&&console.log("Cache.onEditMany: insert done, shortKey: "+t);return}}else n.EnableLog&&console.log("Cache.onEditMany: not found list, shortKey: "+t)},onDelOneById:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType)===g.NONE)return;let s=o||n.defaultIdentiyKey,i=u.getItem(t);if(i){let r=JSON.parse(i);if(r&&r.length>0){for(let l=0;l<r.length;l++)if(r[l][s]===e)return r.splice(l,1),u.saveItem(t,JSON.stringify(r)),n.EnableLog&&console.log(`Cache.onDelOneById: del done: ${s}=${e}, shortKey: ${t}`),r}}},onDelOne:function(t,e){var o;let a=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(s===g.NONE)return;let i=a||n.defaultIdentiyKey,r=(o=e[i])===null||o===void 0?void 0:o.toString();r&&J.onDelOneById(t,r,a,s),n.EnableLog?console.log(`Cache.onDelOne: del done: ${i}=${r}, shortKey: ${t}`):console.log("Cache.onDelOne: not found id by key="+i+"in entity="+JSON.stringify(e))},onDelManyByIds:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType)===g.NONE)return;let s=o||n.defaultIdentiyKey,i=u.getItem(t);if(i){let r=JSON.parse(i);if(r&&r.length>0){for(let l=0;l<r.length;l++)for(let d=0;d<e.length;d++){let f=e[d];r[l][s]===f&&(n.EnableLog&&console.log(`Cache.onDelManyByIds: del one: ${s}=${f}, shortKey: ${t}`),r.splice(l,1))}return u.saveItem(t,JSON.stringify(r)),n.EnableLog&&console.log(`Cache.onDelManyByIds: del done, shortKey: ${t}`),r}}},onDelMany:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===g.NONE)return;let s=o||n.defaultIdentiyKey,i=e.map(r=>{var l;return(l=r[s])===null||l===void 0?void 0:l.toString()}).filter(r=>!!r);i&&i.length>0?J.onDelManyByIds(t,i,o,a):n.EnableLog&&console.log("Cache.onDelOne: not found id by key="+s+"in entity list="+JSON.stringify(e))},evictCache:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:n.defaultStorageType,o=n.cacheKeyPrefix()+t;e===g.OnlySessionStorage?sessionStorage.removeItem(o):e===g.OnlyLocalStorage?localStorage.removeItem(o):e===g.BothStorage&&(sessionStorage.removeItem(o),localStorage.removeItem(o)),n.EnableLog&&console.log("Cache.evictCache done, shortKey: "+t)},evictAllCaches:function(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:n.defaultStorageType;t===g.OnlySessionStorage?sessionStorage.clear():t===g.OnlyLocalStorage?localStorage.clear():t===g.BothStorage&&(sessionStorage.clear(),localStorage.clear()),n.EnableLog&&console.log("Cache.evictAllCaches done")}};var p;(function(t){t.OK="OK",t.KO="KO",t.NewUser="NewUser",t.TokenExpired="TokenExpired"})(p||(p={}));function N(t){if(t){if(t.code===p.OK)return t.data;console.warn("getDataFromBox:"+JSON.stringify(t));return}else{console.warn("\u51FA\u9519\u4E86\uFF0C\u8BF7\u6C42\u7ED3\u679C\u6CA1\u6709\u6570\u636E");return}}var B=t=>encodeURIComponent(JSON.stringify(t));var b=require("react");function X(t,e,o){var a,s,i,r;let l=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType,d=arguments.length>4?arguments[4]:void 0,f=arguments.length>5?arguments[5]:void 0,y=arguments.length>6?arguments[6]:void 0,h=arguments.length>7?arguments[7]:void 0;D(t,e,o,l,d||((a=n.request)===null||a===void 0?void 0:a.showToast),f||((s=n.request)===null||s===void 0?void 0:s.showToast),y||((i=n.request)===null||i===void 0?void 0:i.showLoading),h||((r=n.request)===null||r===void 0?void 0:r.hideLoading))}function D(t,e,o){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType,s=arguments.length>4?arguments[4]:void 0,i=arguments.length>5?arguments[5]:void 0,r=arguments.length>6?arguments[6]:void 0,l=arguments.length>7?arguments[7]:void 0;if(o){let d=u.getObject(o,a);if(d)return n.EnableLog&&console.log("genericFetch: got value from cache, shortKey="+o),e(d),!0;n.EnableLog&&console.log("genericFetch: not found value from cache, shortKey="+o)}return r&&r(),n.EnableLog&&console.log("genericFetch: from remote server..."),t().then(d=>{l&&l();let f=d.data;if(f.code===p.OK){let y=N(f);return y===void 0?s&&(n.EnableLog&&console.log("genericFetch: no data from remote server"),s()):(o&&u.saveItem(o,JSON.stringify(y),a),e(y)),!1}else return i&&(n.EnableLog&&console.log("genericFetch: fail from remote server: code="+f.code+",msg="+f.msg),i(f.msg||f.code)),!1}).catch(d=>(l&&l(),i&&i(d.message),n.EnableLog&&console.log("genericFetch exception from remote server:",d),!1)),!1}function Y(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:!1,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!1,s=arguments.length>4&&arguments[4]!==void 0?arguments[4]:n.defaultStorageType,[i,r]=(0,b.useState)(!1),[l,d]=(0,b.useState)(),[f,y]=(0,b.useState)();return(0,b.useEffect)(()=>{var h,T;r(!0);let E=o?(h=n.request)===null||h===void 0?void 0:h.getWithoutAuth:(T=n.request)===null||T===void 0?void 0:T.get;if(!E)console.warn("not config request promise, please set ConfigRequest firstly");else{var x,O,S,M;a&&(!((x=n.request)!==null&&x!==void 0&&x.showLoading)||!((O=n.request)!==null&&O!==void 0&&O.hideLoading))&&console.warn("not config request showLoading/hideLoading, please set ConfigRequest firstly"),D(()=>E(t),L=>{r(!1),d(L)},e,s,()=>{r(!1),y("\u6570\u636E\u79FB\u6C11\u706B\u661F\u4E86^_^")},L=>{r(!1),y(L)},(S=n.request)===null||S===void 0?void 0:S.showLoading,(M=n.request)===null||M===void 0?void 0:M.hideLoading)}},[t]),{loading:i,entity:l,errMsg:f}}var m=require("react");function ce(t){if(!t)return"";let e=H(V({},t),{umi:t.pagination?B(t.pagination):void 0,pagination:void 0});n.EnableLog&&console.log("query2Params: newQuery="+JSON.stringify(t));let o=[];for(let a in e)if(a){let s=e[a];s==null||s===""?n.EnableLog&&console.log(`query2Params: no value for ${a}, ignore`):o.push(`${a}=${s}`)}return o.length>0?"?"+o.sort().join("&"):""}function ee(t,e,o){var a;let s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!0,i=arguments.length>4&&arguments[4]!==void 0?arguments[4]:n.defaultStorageType,[r,l]=(0,m.useState)([]),[d,f]=(0,m.useState)(!0),[y,h]=(0,m.useState)(!1),[T,E]=(0,m.useState)(),[x,O]=(0,m.useState)(e?Z(e):!0),[S,M]=(0,m.useState)(),[L,te]=(0,m.useState)(0);n.EnableLog&&console.log("call useCacheList, wholeUrl: "+S+", current list.length: "+r.length);let{current:v}=(0,m.useRef)({pageSize:(o==null||(a=o.pagination)===null||a===void 0?void 0:a.pageSize)||n.PageSize,useCache:!!e,isLoadMore:!1}),j=c=>{v.useCache=c},K=c=>{v.isLoadMore=c},ne=()=>{te(L+1)},oe=c=>{n.EnableLog&&console.log("setQuery: "+JSON.stringify(c)),M(t+ce(c))},U=(c,re,q,w)=>{var P;if(!q){console.warn("no whole url, please call setQuery firstly");return}n.EnableLog&&console.log("fetch from remote... url="+q);let A=(P=n.request)===null||P===void 0?void 0:P.get;if(!A){console.warn("please inject get request firstly");return}A(q).then($=>{f(!1),j(!0);let I=$.data,C=N(I);if(I.code===p.OK)if(C){h(!1);let R=c&&r&&r.length>0?r.concat(C):C;if(l(R),e&&u.saveObject(e,R,i),s){let z=C.length>=re;O(z),e&&F(e,z)}n.EnableLog&&console.log("return from remote server: list.length: "+C.length)}else h(!0),s&&(E("no data"),e&&F(e,!1)),n.EnableLog&&console.log("return from remote server: no data");else h(!0),s&&(O(!1),e&&F(e,!1)),E(I.msg||I.code),n.EnableLog&&console.log("remote server return err code="+I.code+",msg="+I.msg);w&&w(C),K(!1)}).catch($=>{j(!1),w&&w(),f(!1),h(!0),E($.message),s&&(e&&F(e,!1),O(!1)),K(!1),n.EnableLog&&console.log("useCacheList exception from remote server: ",$)})};return(0,m.useEffect)(()=>{if(n.EnableLog&&console.log("useCacheList useEffect, try load from local or remote, refreshCount="+L+", wholeUrl="+S),f(!0),v.useCache&&e){let c=u.getObject(e,i);c?(n.EnableLog&&console.log("fetch from local cache, shortKey:"+e+", list.length: "+c.length),l(c),f(!1),O(Z(e))):(n.EnableLog&&console.log("no local cache, try from remote..."),U(v.isLoadMore,v.pageSize,S))}else n.EnableLog&&console.log("useCache=false, try from remote..."),U(v.isLoadMore,v.pageSize,S)},[S,L]),{isLoading:d,isError:y,errMsg:T,loadMoreState:x,list:r,refreshCount:L,setList:l,fetchDataFromRemote:U,setQuery:oe,setRefresh:ne,setUseCache:j,setIsLoadMore:K}}function Z(t){let e=n.cacheKeyPrefix()+t+"/loadMore",o=sessionStorage.getItem(e);return!(o&&o==="0")}function F(t,e){let o=n.cacheKeyPrefix()+t+"/loadMore";sessionStorage.setItem(o,e?"1":"0")}0&&(module.exports={CODE,Cache,CacheStorage,StorageType,UseCacheConfig,currentHost,encodeUmi,fetchWithLoading,genericFetch,getDataFromBox,useCache,useCacheList});
{
"name": "@rwsbillyang/usecache",
"version": "0.9.9",
"version": "0.9.10",
"description": "react hooks use cache before client fetch data from remote server, likes cache in server side before database I/O",

@@ -5,0 +5,0 @@ "repository": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc