@rwsbillyang/usecache
Advanced tools
Comparing version 0.9.8 to 0.9.9
@@ -1,1 +0,1 @@ | ||
var l={OnlySessionStorage:1,OnlyLocalStorage:2,BothStorage:3,NONE:0};var n={EnableLog:!1,cacheKeyPrefix:()=>"",defaultIdentiyKey:"_id",defaultStorageType:l.OnlySessionStorage,PageSize:10},Q=()=>window.location.protocol+"//"+window.location.host;var u={getItem:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage,o=arguments.length>2?arguments[2]:void 0;if(e===l.NONE)return o;let a,s=n.cacheKeyPrefix()+t;return e===l.OnlySessionStorage?a=sessionStorage.getItem(s):e===l.OnlyLocalStorage?a=localStorage.getItem(s):e===l.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]:l.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]:l.OnlySessionStorage;if(o===l.NONE)return;let a=n.cacheKeyPrefix()+t;o===l.OnlySessionStorage?sessionStorage.setItem(a,e):o===l.OnlyLocalStorage?localStorage.setItem(a,e):o===l.BothStorage&&(sessionStorage.setItem(a,e),localStorage.setItem(a,e)),n.EnableLog&&console.log(`CacheStorage.saveItem: key=${a}, v=${e}`)},saveObject:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:l.OnlySessionStorage;return u.saveItem(t,JSON.stringify(e),o)},remove:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage;if(e===l.NONE)return;let o=n.cacheKeyPrefix()+t;e===l.OnlySessionStorage?sessionStorage.removeItem(o):e===l.OnlyLocalStorage?localStorage.removeItem(o):e===l.BothStorage&&(sessionStorage.removeItem(o),localStorage.removeItem(o))}};var U={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===l.NONE)return;let s=o||n.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);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===l.NONE)return;let s=o||n.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++){let d=r[i];for(let f=0;f<e.length;f++)d[s]===e[f]&&r.push(d)}return r}}},onAddOne:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultStorageType;if(o===l.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]);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===l.NONE)return;let s=o||n.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);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,u.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===l.NONE)return;let s=o||n.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<e.length;i++){let d=e[i];for(let f=0;f<r.length;f++)r[f][s]===d[s]&&(r[f]=d)}u.saveItem(t,JSON.stringify(r));return}else{u.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)===l.NONE)return;let s=o||n.defaultIdentiyKey,g=u.getItem(t);if(g){let r=JSON.parse(g);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),u.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===l.NONE)return;let g=a||n.defaultIdentiyKey,r=(o=e[g])===null||o===void 0?void 0:o.toString();r?U.onDelOneById(t,r,a,s):console.log("onDelOne: not found id by key="+g+"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)===l.NONE)return;let s=o||n.defaultIdentiyKey,g=u.getItem(t);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)for(let d=0;d<e.length;d++){let f=e[d];r[i][s]===f&&(n.EnableLog&&console.log(`del one: ${s}=${f}`),r.splice(i,1))}return u.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===l.NONE)return;let s=o||n.defaultIdentiyKey,g=e.map(r=>{var i;return(i=r[s])===null||i===void 0?void 0:i.toString()}).filter(r=>!!r);g&&g.length>0?U.onDelManyByIds(t,g,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===l.OnlySessionStorage?sessionStorage.removeItem(o):e===l.OnlyLocalStorage?localStorage.removeItem(o):e===l.BothStorage&&(sessionStorage.removeItem(o),localStorage.removeItem(o))},evictAllCaches:function(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:n.defaultStorageType;t===l.OnlySessionStorage?sessionStorage.clear():t===l.OnlyLocalStorage?localStorage.clear():t===l.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 j=t=>encodeURIComponent(JSON.stringify(t));import{useEffect as X,useState as D}from"react";function Y(t,e,o){var a,s,g,r;let i=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,c=arguments.length>6?arguments[6]:void 0,y=arguments.length>7?arguments[7]:void 0;K(t,e,o,i,d||((a=n.request)===null||a===void 0?void 0:a.showToast),f||((s=n.request)===null||s===void 0?void 0:s.showToast),c||((g=n.request)===null||g===void 0?void 0:g.showLoading),y||((r=n.request)===null||r===void 0?void 0:r.hideLoading))}function K(t,e,o){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType,s=arguments.length>4?arguments[4]:void 0,g=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=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=>{i&&i();let f=d.data;if(f.code===O.OK){let c=x(f);return c===void 0?s&&(n.EnableLog&&console.log("genericFetch: no data from remote server"),s()):(o&&u.saveItem(o,JSON.stringify(c),a),e(c)),!1}else return g&&(n.EnableLog&&console.log("genericFetch: fail from remote server: code="+f.code+",msg="+f.msg),g(f.msg||f.code)),!1}).catch(d=>(i&&i(),g&&g(d.message),n.EnableLog&&console.log("genericFetch exception from remote server:",d),!1)),!1}function Z(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,[g,r]=D(!1),[i,d]=D(),[f,c]=D();return X(()=>{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,h,S,C;a&&(!((T=n.request)!==null&&T!==void 0&&T.showLoading)||!((h=n.request)!==null&&h!==void 0&&h.hideLoading))&&console.warn("not config request showLoading/hideLoading, please set ConfigRequest firstly"),K(()=>I(t),p=>{r(!1),d(p)},e,s,()=>{r(!1),c("\u6570\u636E\u79FB\u6C11\u706B\u661F\u4E86^_^")},p=>{r(!1),c(p)},(S=n.request)===null||S===void 0?void 0:S.showLoading,(C=n.request)===null||C===void 0?void 0:C.hideLoading)}},[t]),{loading:g,entity:i,errMsg:f}}import{useEffect as ee,useRef as te,useState as L}from"react";function z(t){if(!t)return"";t.pagination&&(t.umi=j(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===""||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,g=arguments.length>4&&arguments[4]!==void 0?arguments[4]:n.defaultStorageType,[r,i]=L(),[d,f]=L(!0),[c,y]=L(!1),[N,I]=L(),[T,h]=L(e?A(e):!0),[S,C]=L(t+z(o)),[p,W]=L(0),{current:v}=te({pageSize:(o==null||(a=o.pagination)===null||a===void 0?void 0:a.pageSize)||n.PageSize,useCache:!!e,isLoadMore:!1}),J=m=>{v.useCache=m},$=m=>{v.isLoadMore=m},_=()=>{W(p+1)},V=m=>{n.EnableLog&&console.log("update query for wholeUrl...query="+JSON.stringify(m)),C(t+z(m))},F=(m,H,G,w)=>{var B;n.EnableLog&&console.log("fetch from remote... url="+m);let q=(B=n.request)===null||B===void 0?void 0:B.get;if(!q){console.warn("please inject get request firstly");return}q(m).then(M=>{f(!1),J(!0);let E=M.data,b=x(E);if(E.code===O.OK)if(b){y(!1);let P=H&&r&&r.length>0?r.concat(b):b;if(i(P),e&&u.saveObject(e,P,g),s){let R=b.length>=G;h(R),e&&k(e,R)}}else y(!0),s&&(I("no data"),e&&k(e,!1));else y(!0),s&&(h(!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(!0),w&&w(),f(!1),y(!0),I(M.message),s&&(e&&k(e,!1),h(!1)),$(!1),n.EnableLog&&console.log("useCacheList exception from remote server:",M)})};return ee(()=>{if(n.EnableLog&&console.log("in useCacheList useEffect, try load from local or remote, refreshCount="+p+", wholeUrl="+S),f(!0),v.useCache&&e){let m=u.getObject(e,g);m?(n.EnableLog&&console.log("fetch from local cache... shortKey="+e),i(m),f(!1),h(A(e))):(n.EnableLog&&console.log("no local cache, try from remote..."),F(S,v.isLoadMore,v.pageSize))}else n.EnableLog&&console.log("useCache=false, try from remote..."),F(S,v.isLoadMore,v.pageSize)},[S,p]),{isLoading:d,isError:c,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,U as Cache,u as CacheStorage,l as StorageType,n as UseCacheConfig,Q as currentHost,j as encodeUmi,Y as fetchWithLoading,K as genericFetch,x as getDataFromBox,Z as useCache,ne as useCacheList}; | ||
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}; |
@@ -311,6 +311,6 @@ import * as react from 'react'; | ||
loadMoreState: boolean; | ||
list: T[] | undefined; | ||
list: T[]; | ||
refreshCount: number; | ||
setList: react.Dispatch<react.SetStateAction<T[] | undefined>>; | ||
fetchDataFromRemote: (wholeUrl: string, isLoadMore: boolean, pageSize: number, onDone?: ((data?: T[]) => void) | undefined) => void; | ||
setList: react.Dispatch<react.SetStateAction<T[]>>; | ||
fetchDataFromRemote: (isLoadMore: boolean, pageSize: number, wholeUrl?: string, onDone?: ((data?: T[]) => void) | undefined) => void; | ||
setQuery: (query?: Q) => void; | ||
@@ -317,0 +317,0 @@ setRefresh: () => void; |
@@ -1,1 +0,1 @@ | ||
"use strict";var q=Object.defineProperty;var te=Object.getOwnPropertyDescriptor;var ne=Object.getOwnPropertyNames;var oe=Object.prototype.hasOwnProperty;var re=(t,e)=>{for(var n in e)q(t,n,{get:e[n],enumerable:!0})},ae=(t,e,n,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of ne(e))!oe.call(t,s)&&s!==n&&q(t,s,{get:()=>e[s],enumerable:!(a=te(e,s))||a.enumerable});return t};var se=t=>ae(q({},"__esModule",{value:!0}),t);var ie={};re(ie,{CODE:()=>S,Cache:()=>J,CacheStorage:()=>u,StorageType:()=>l,UseCacheConfig:()=>o,currentHost:()=>A,encodeUmi:()=>$,fetchWithLoading:()=>W,genericFetch:()=>F,getDataFromBox:()=>N,useCache:()=>_,useCacheList:()=>G});module.exports=se(ie);var l={OnlySessionStorage:1,OnlyLocalStorage:2,BothStorage:3,NONE:0};var o={EnableLog:!1,cacheKeyPrefix:()=>"",defaultIdentiyKey:"_id",defaultStorageType:l.OnlySessionStorage,PageSize:10},A=()=>window.location.protocol+"//"+window.location.host;var u={getItem:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage,n=arguments.length>2?arguments[2]:void 0;if(e===l.NONE)return n;let a,s=o.cacheKeyPrefix()+t;return e===l.OnlySessionStorage?a=sessionStorage.getItem(s):e===l.OnlyLocalStorage?a=localStorage.getItem(s):e===l.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]:l.OnlySessionStorage,n=arguments.length>2?arguments[2]:void 0,a=u.getItem(t,e);return a?JSON.parse(a):n},saveItem:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:l.OnlySessionStorage;if(n===l.NONE)return;let a=o.cacheKeyPrefix()+t;n===l.OnlySessionStorage?sessionStorage.setItem(a,e):n===l.OnlyLocalStorage?localStorage.setItem(a,e):n===l.BothStorage&&(sessionStorage.setItem(a,e),localStorage.setItem(a,e)),o.EnableLog&&console.log(`CacheStorage.saveItem: key=${a}, v=${e}`)},saveObject:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:l.OnlySessionStorage;return u.saveItem(t,JSON.stringify(e),n)},remove:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage;if(e===l.NONE)return;let n=o.cacheKeyPrefix()+t;e===l.OnlySessionStorage?sessionStorage.removeItem(n):e===l.OnlyLocalStorage?localStorage.removeItem(n):e===l.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===l.NONE)return;let s=n||o.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);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===l.NONE)return;let s=n||o.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++){let d=r[i];for(let f=0;f<e.length;f++)d[s]===e[f]&&r.push(d)}return r}}},onAddOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultStorageType;if(n===l.NONE)return;let a=u.getItem(t,n);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]);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===l.NONE)return;let s=n||o.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);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,u.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===l.NONE)return;let s=n||o.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<e.length;i++){let d=e[i];for(let f=0;f<r.length;f++)r[f][s]===d[s]&&(r[f]=d)}u.saveItem(t,JSON.stringify(r));return}else{u.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)===l.NONE)return;let s=n||o.defaultIdentiyKey,g=u.getItem(t);if(g){let r=JSON.parse(g);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),u.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===l.NONE)return;let g=a||o.defaultIdentiyKey,r=(n=e[g])===null||n===void 0?void 0:n.toString();r?J.onDelOneById(t,r,a,s):console.log("onDelOne: not found id by key="+g+"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)===l.NONE)return;let s=n||o.defaultIdentiyKey,g=u.getItem(t);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)for(let d=0;d<e.length;d++){let f=e[d];r[i][s]===f&&(o.EnableLog&&console.log(`del one: ${s}=${f}`),r.splice(i,1))}return u.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===l.NONE)return;let s=n||o.defaultIdentiyKey,g=e.map(r=>{var i;return(i=r[s])===null||i===void 0?void 0:i.toString()}).filter(r=>!!r);g&&g.length>0?J.onDelManyByIds(t,g,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===l.OnlySessionStorage?sessionStorage.removeItem(n):e===l.OnlyLocalStorage?localStorage.removeItem(n):e===l.BothStorage&&(sessionStorage.removeItem(n),localStorage.removeItem(n))},evictAllCaches:function(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:o.defaultStorageType;t===l.OnlySessionStorage?sessionStorage.clear():t===l.OnlyLocalStorage?localStorage.clear():t===l.BothStorage&&(sessionStorage.clear(),localStorage.clear())}};var S;(function(t){t.OK="OK",t.KO="KO",t.NewUser="NewUser",t.TokenExpired="TokenExpired"})(S||(S={}));function N(t){if(t){if(t.code===S.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 W(t,e,n){var a,s,g,r;let i=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.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;F(t,e,n,i,d||((a=o.request)===null||a===void 0?void 0:a.showToast),f||((s=o.request)===null||s===void 0?void 0:s.showToast),y||((g=o.request)===null||g===void 0?void 0:g.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,g=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=u.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 f=d.data;if(f.code===S.OK){let y=N(f);return y===void 0?s&&(o.EnableLog&&console.log("genericFetch: no data from remote server"),s()):(n&&u.saveItem(n,JSON.stringify(y),a),e(y)),!1}else return g&&(o.EnableLog&&console.log("genericFetch: fail from remote server: code="+f.code+",msg="+f.msg),g(f.msg||f.code)),!1}).catch(d=>(i&&i(),g&&g(d.message),o.EnableLog&&console.log("genericFetch exception from remote server:",d),!1)),!1}function _(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,[g,r]=(0,T.useState)(!1),[i,d]=(0,T.useState)(),[f,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,p,O,x;a&&(!((b=o.request)!==null&&b!==void 0&&b.showLoading)||!((p=o.request)!==null&&p!==void 0&&p.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)},(O=o.request)===null||O===void 0?void 0:O.showLoading,(x=o.request)===null||x===void 0?void 0:x.hideLoading)}},[t]),{loading:g,entity:i,errMsg:f}}var c=require("react");function V(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===""||e.push(`${n}=${a}`)}return e.length>0?"?"+e.sort().join("&"):""}function G(t,e,n){var a;let s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!0,g=arguments.length>4&&arguments[4]!==void 0?arguments[4]:o.defaultStorageType,[r,i]=(0,c.useState)(),[d,f]=(0,c.useState)(!0),[y,h]=(0,c.useState)(!1),[C,I]=(0,c.useState)(),[b,p]=(0,c.useState)(e?H(e):!0),[O,x]=(0,c.useState)(t+V(n)),[v,Q]=(0,c.useState)(0),{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=()=>{Q(v+1)},Y=m=>{o.EnableLog&&console.log("update query for wholeUrl...query="+JSON.stringify(m)),x(t+V(m))},D=(m,Z,ee,M)=>{var K;o.EnableLog&&console.log("fetch from remote... url="+m);let P=(K=o.request)===null||K===void 0?void 0:K.get;if(!P){console.warn("please inject get request firstly");return}P(m).then(k=>{f(!1),U(!0);let E=k.data,w=N(E);if(E.code===S.OK)if(w){h(!1);let R=Z&&r&&r.length>0?r.concat(w):w;if(i(R),e&&u.saveObject(e,R,g),s){let z=w.length>=ee;p(z),e&&B(e,z)}}else h(!0),s&&(I("no data"),e&&B(e,!1));else h(!0),s&&(p(!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(!0),M&&M(),f(!1),h(!0),I(k.message),s&&(e&&B(e,!1),p(!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="+O),f(!0),L.useCache&&e){let m=u.getObject(e,g);m?(o.EnableLog&&console.log("fetch from local cache... shortKey="+e),i(m),f(!1),p(H(e))):(o.EnableLog&&console.log("no local cache, try from remote..."),D(O,L.isLoadMore,L.pageSize))}else o.EnableLog&&console.log("useCache=false, try from remote..."),D(O,L.isLoadMore,L.pageSize)},[O,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 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}); |
{ | ||
"name": "@rwsbillyang/usecache", | ||
"version": "0.9.8", | ||
"version": "0.9.9", | ||
"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": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75329