@rwsbillyang/usecache
Advanced tools
Comparing version 0.9.1 to 0.9.2
@@ -1,1 +0,1 @@ | ||
var G=Object.defineProperty,X=Object.defineProperties;var Y=Object.getOwnPropertyDescriptors;var W=Object.getOwnPropertySymbols;var Z=Object.prototype.hasOwnProperty,ee=Object.prototype.propertyIsEnumerable;var H=(t,e,n)=>e in t?G(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,j=(t,e)=>{for(var n in e||(e={}))Z.call(e,n)&&H(t,n,e[n]);if(W)for(var n of W(e))ee.call(e,n)&&H(t,n,e[n]);return t},K=(t,e)=>X(t,Y(e));var l={OnlySessionStorage:1,OnlyLocalStorage:2,BothStorage:3,NONE:0};var r={EnableLog:!1,cacheKeyPrefix:"",defaultIdentiyKey:"_id",defaultStorageType:l.OnlySessionStorage,PageSize:10},te=()=>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=r.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))),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=r.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))},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)}};var U={findOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType;if(a===l.NONE)return;let s=n||r.defaultIdentiyKey,g=u.getItem(t,a);if(g){let o=JSON.parse(g);if(o&&o.length>0){for(let i=0;i<o.length;i++)if(o[i][s]===e)return o[i]}}},findMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType;if(a===l.NONE)return;let s=n||r.defaultIdentiyKey,g=u.getItem(t,a);if(g){let o=JSON.parse(g);if(o&&o.length>0){for(let i=0;i<o.length;i++){let f=o[i];for(let d=0;d<e.length;d++)f[s]===e[d]&&o.push(f)}return o}}},onAddOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.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]:r.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType;if(a===l.NONE)return;let s=n||r.defaultIdentiyKey,g=u.getItem(t,a);if(g){let o=JSON.parse(g);if(o&&o.length>0){for(let i=0;i<o.length;i++)if(o[i][s]===e[s]){r.EnableLog&&console.log(`onEditOne, e[${s}]=${e[s]}, shortKey=${t}`),o[i]=e,u.saveObject(t,o);return}r.EnableLog&&console.log(`onEditOne\uFF1Anot found in list, key=${s}, shortKey=${t}`);return}}else r.EnableLog&&console.log("onEditOne\uFF1Anot found list: shortKey="+t)},onEditMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType;if(a===l.NONE)return;let s=n||r.defaultIdentiyKey,g=u.getItem(t,a);if(g){let o=JSON.parse(g);if(o&&o.length>0){for(let i=0;i<e.length;i++){let f=e[i];for(let d=0;d<o.length;d++)o[d][s]===f[s]&&(o[d]=f)}u.saveItem(t,JSON.stringify(o));return}else{u.saveItem(t,JSON.stringify(e));return}}else r.EnableLog&&console.log("onEditMany: not found list, shortKey="+t)},onDelOneById:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType)===l.NONE)return;let s=n||r.defaultIdentiyKey,g=u.getItem(t);if(g){let o=JSON.parse(g);if(o&&o.length>0){for(let i=0;i<o.length;i++)if(o[i][s]===e)return r.EnableLog&&console.log(`del one: ${s}=${e}`),o.splice(i,1),u.saveItem(t,JSON.stringify(o)),o}}},onDelOne:function(t,e){var n;let a=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.defaultIdentiyKey,s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType;if(s===l.NONE)return;let g=a||r.defaultIdentiyKey,o=(n=e[g])===null||n===void 0?void 0:n.toString();o?U.onDelOneById(t,o,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]:r.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType)===l.NONE)return;let s=n||r.defaultIdentiyKey,g=u.getItem(t);if(g){let o=JSON.parse(g);if(o&&o.length>0){for(let i=0;i<o.length;i++)for(let f=0;f<e.length;f++){let d=e[f];o[i][s]===d&&(r.EnableLog&&console.log(`del one: ${s}=${d}`),o.splice(i,1))}return u.saveItem(t,JSON.stringify(o)),o}}},onDelMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType;if(a===l.NONE)return;let s=n||r.defaultIdentiyKey,g=e.map(o=>{var i;return(i=o[s])===null||i===void 0?void 0:i.toString()}).filter(o=>!!o);g&&g.length>0?U.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]:r.defaultStorageType,n=r.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]:r.defaultStorageType;t===l.OnlySessionStorage?sessionStorage.clear():t===l.OnlyLocalStorage?localStorage.clear():t===l.BothStorage&&(sessionStorage.clear(),localStorage.clear())}};var p;(function(t){t.OK="OK",t.KO="KO",t.NewUser="NewUser",t.TokenExpired="TokenExpired"})(p||(p={}));function E(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 F=t=>encodeURIComponent(JSON.stringify(t));import{useEffect as ne,useState as P}from"react";function oe(t,e,n){var a,s,g,o;let i=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType,f=arguments.length>4?arguments[4]:void 0,d=arguments.length>5?arguments[5]:void 0,m=arguments.length>6?arguments[6]:void 0,O=arguments.length>7?arguments[7]:void 0;$(t,e,n,i,f||((a=r.request)===null||a===void 0?void 0:a.showToast),d||((s=r.request)===null||s===void 0?void 0:s.showToast),m||((g=r.request)===null||g===void 0?void 0:g.showLoading),O||((o=r.request)===null||o===void 0?void 0:o.hideLoading))}function $(t,e,n){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType,s=arguments.length>4?arguments[4]:void 0,g=arguments.length>5?arguments[5]:void 0,o=arguments.length>6?arguments[6]:void 0,i=arguments.length>7?arguments[7]:void 0;if(n){let f=u.getObject(n,a);if(f)return e(f),!0}else o&&o(),t().then(f=>{i&&i();let d=f.data;if(d.code===p.OK){let m=E(d);return m===void 0?s&&s():(n&&u.saveItem(n,JSON.stringify(m),a),e(m)),!1}else return g&&g(d.msg||d.code),!1}).catch(f=>(i&&i(),g&&g(f.message),!1));return!1}function re(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]:r.defaultStorageType,[g,o]=P(!1),[i,f]=P(),[d,m]=P();return ne(()=>{var O,S;o(!0);let C=n?(O=r.request)===null||O===void 0?void 0:O.getWithouAuth:(S=r.request)===null||S===void 0?void 0:S.get;if(!C)console.warn("not config request promise, please set ConfigRequest firstly");else{var v,N,h,T;a&&(!((v=r.request)!==null&&v!==void 0&&v.showLoading)||!((N=r.request)!==null&&N!==void 0&&N.hideLoading))&&console.warn("not config request showLoading/hideLoading, please set ConfigRequest firstly"),$(()=>C(t),I=>{o(!1),f(I)},e,s,()=>{o(!1),m("\u6570\u636E\u79FB\u6C11\u706B\u661F\u4E86^_^")},I=>{o(!1),m(I)},(h=r.request)===null||h===void 0?void 0:h.showLoading,(T=r.request)===null||T===void 0?void 0:T.hideLoading)}},[t]),{loading:g,entity:i,errMsg:d}}import{useEffect as ae,useState as y}from"react";function se(t,e,n){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!0,s=arguments.length>4&&arguments[4]!==void 0?arguments[4]:r.defaultStorageType,[g,o]=y(),[i,f]=y(n),[d,m]=y(!0),[O,S]=y(!1),[C,v]=y(),[N,h]=y(e?V(e):!0),[T,I]=y(!!e),[Q,J]=y(!1),[q,_]=y(0),M=(c,x)=>{var k;r.EnableLog&&console.log("fetch from remote... shortKey="+e+", query="+JSON.stringify(c));let A=(k=r.request)===null||k===void 0?void 0:k.get;if(!A){console.warn("please inject get request firstly");return}A(t,c!=null&&c.pagination?K(j({},c),{umi:F(c.pagination),sort:void 0,pagination:void 0}):K(j({},c),{sort:void 0})).then(B=>{m(!1),I(!0);let b=B.data,L=E(b);if(b.code===p.OK)if(L){S(!1);let R=Q&&g&&g.length>0?g.concat(L):L;if(o(R),e&&u.saveObject(e,R,s),a){var D;let z=L.length>=((c==null||(D=c.pagination)===null||D===void 0?void 0:D.pageSize)||r.PageSize);h(z),e&&w(e,z)}}else S(!0),a&&(v("no data"),e&&w(e,!1));else S(!0),a&&(h(!1),e&&w(e,!1)),v(b.msg||b.code);x&&x(L),J(!1)}).catch(B=>{I(!0),x&&x(),m(!1),S(!0),v(B.message),a&&(e&&w(e,!1),h(!1)),J(!1)})};return ae(()=>{if(r.EnableLog&&console.log("in useEffect loading, url="+t+", query="+JSON.stringify(i)),m(!0),T&&e){let c=u.getObject(e,s);c?(r.EnableLog&&console.log("fetch from local cache... shortKey="+e),o(c),m(!1),h(V(e))):(r.EnableLog&&console.log("no local cache, try from remote..."),M(i))}else r.EnableLog&&console.log("useCache=false, try from remote..."),M(i)},[t,i,q]),{isLoading:d,isError:O,errMsg:C,loadMoreState:N,query:i,setQuery:f,list:g,setList:o,fetchDataFromRemote:M,refresh:q,setRefresh:_,setUseCache:I,setIsLoadMore:J}}function V(t){let e=r.cacheKeyPrefix+t+"/loadMore",n=sessionStorage.getItem(e);return!(n&&n==="0")}function w(t,e){let n=r.cacheKeyPrefix+t+"/loadMore";sessionStorage.setItem(n,e?"1":"0")}export{p as CODE,U as Cache,u as CacheStorage,l as StorageType,r as UseCacheConfig,te as currentHost,F as encodeUmi,oe as fetchWithLoading,$ as genericFetch,E as getDataFromBox,re as useCache,se as useCacheList}; | ||
var G=Object.defineProperty,X=Object.defineProperties;var Y=Object.getOwnPropertyDescriptors;var V=Object.getOwnPropertySymbols;var Z=Object.prototype.hasOwnProperty,ee=Object.prototype.propertyIsEnumerable;var W=(t,e,n)=>e in t?G(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,D=(t,e)=>{for(var n in e||(e={}))Z.call(e,n)&&W(t,n,e[n]);if(V)for(var n of V(e))ee.call(e,n)&&W(t,n,e[n]);return t},j=(t,e)=>X(t,Y(e));var l={OnlySessionStorage:1,OnlyLocalStorage:2,BothStorage:3,NONE:0};var o={EnableLog:!1,cacheKeyPrefix:"",defaultIdentiyKey:"_id",defaultStorageType:l.OnlySessionStorage,PageSize:10},te=()=>window.location.protocol+"//"+window.location.host;var f={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}, v=${a}, 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=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]: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 f.saveItem(t,JSON.stringify(e),n)}};var K={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=f.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=f.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 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===l.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===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.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,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===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.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 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)===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.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),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===l.NONE)return;let g=a||o.defaultIdentiyKey,r=(n=e[g])===null||n===void 0?void 0:n.toString();r?K.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=f.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 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===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?K.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 p;(function(t){t.OK="OK",t.KO="KO",t.NewUser="NewUser",t.TokenExpired="TokenExpired"})(p||(p={}));function C(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));import{useEffect as ne,useState as U}from"react";function oe(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,u=arguments.length>5?arguments[5]:void 0,m=arguments.length>6?arguments[6]:void 0,O=arguments.length>7?arguments[7]:void 0;P(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),m||((g=o.request)===null||g===void 0?void 0:g.showLoading),O||((r=o.request)===null||r===void 0?void 0:r.hideLoading))}function P(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=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)}else r&&r(),t().then(d=>{i&&i();let u=d.data;if(u.code===p.OK){let m=C(u);return m===void 0?s&&(o.EnableLog&&console.log("genericFetch: no data from remote server"),s()):(n&&f.saveItem(n,JSON.stringify(m),a),e(m)),!1}else return g&&(o.EnableLog&&console.log("genericFetch: fail from remote server: code="+u.code+",msg="+u.msg),g(u.msg||u.code)),!1}).catch(d=>(i&&i(),g&&g(d.message),o.EnableLog&&console.log("genericFetch exception from remote server:",d),!1));return!1}function re(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]=U(!1),[i,d]=U(),[u,m]=U();return ne(()=>{var O,h;r(!0);let b=n?(O=o.request)===null||O===void 0?void 0:O.getWithouAuth:(h=o.request)===null||h===void 0?void 0:h.get;if(!b)console.warn("not config request promise, please set ConfigRequest firstly");else{var v,E,S,N;a&&(!((v=o.request)!==null&&v!==void 0&&v.showLoading)||!((E=o.request)!==null&&E!==void 0&&E.hideLoading))&&console.warn("not config request showLoading/hideLoading, please set ConfigRequest firstly"),P(()=>b(t),I=>{r(!1),d(I)},e,s,()=>{r(!1),m("\u6570\u636E\u79FB\u6C11\u706B\u661F\u4E86^_^")},I=>{r(!1),m(I)},(S=o.request)===null||S===void 0?void 0:S.showLoading,(N=o.request)===null||N===void 0?void 0:N.hideLoading)}},[t]),{loading:g,entity:i,errMsg:u}}import{useEffect as ae,useState as y}from"react";function se(t,e,n){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!0,s=arguments.length>4&&arguments[4]!==void 0?arguments[4]:o.defaultStorageType,[g,r]=y(),[i,d]=y(n),[u,m]=y(!0),[O,h]=y(!1),[b,v]=y(),[E,S]=y(e?H(e):!0),[N,I]=y(!!e),[Q,J]=y(!1),[q,_]=y(0),M=(c,x)=>{var F;o.EnableLog&&console.log("fetch from remote... shortKey="+e+", query="+JSON.stringify(c));let A=(F=o.request)===null||F===void 0?void 0:F.get;if(!A){console.warn("please inject get request firstly");return}A(t,c!=null&&c.pagination?j(D({},c),{umi:$(c.pagination),sort:void 0,pagination:void 0}):j(D({},c),{sort:void 0})).then(w=>{m(!1),I(!0);let L=w.data,T=C(L);if(L.code===p.OK)if(T){h(!1);let R=Q&&g&&g.length>0?g.concat(T):T;if(r(R),e&&f.saveObject(e,R,s),a){var B;let z=T.length>=((c==null||(B=c.pagination)===null||B===void 0?void 0:B.pageSize)||o.PageSize);S(z),e&&k(e,z)}}else h(!0),a&&(v("no data"),e&&k(e,!1));else h(!0),a&&(S(!1),e&&k(e,!1)),v(L.msg||L.code),o.EnableLog&&console.log("useCacheList: fail from remote server: code="+L.code+",msg="+L.msg);x&&x(T),J(!1)}).catch(w=>{I(!0),x&&x(),m(!1),h(!0),v(w.message),a&&(e&&k(e,!1),S(!1)),J(!1),o.EnableLog&&console.log("useCacheList exception from remote server:",w)})};return ae(()=>{if(o.EnableLog&&console.log("in useEffect loading, url="+t+", query="+JSON.stringify(i)),m(!0),N&&e){let c=f.getObject(e,s);c?(o.EnableLog&&console.log("fetch from local cache... shortKey="+e),r(c),m(!1),S(H(e))):(o.EnableLog&&console.log("no local cache, try from remote..."),M(i))}else o.EnableLog&&console.log("useCache=false, try from remote..."),M(i)},[t,i,q]),{isLoading:u,isError:O,errMsg:b,loadMoreState:E,query:i,setQuery:d,list:g,setList:r,fetchDataFromRemote:M,refresh:q,setRefresh:_,setUseCache:I,setIsLoadMore:J}}function H(t){let e=o.cacheKeyPrefix+t+"/loadMore",n=sessionStorage.getItem(e);return!(n&&n==="0")}function k(t,e){let n=o.cacheKeyPrefix+t+"/loadMore";sessionStorage.setItem(n,e?"1":"0")}export{p as CODE,K as Cache,f as CacheStorage,l as StorageType,o as UseCacheConfig,te as currentHost,$ as encodeUmi,oe as fetchWithLoading,P as genericFetch,C as getDataFromBox,re as useCache,se as useCacheList}; |
@@ -1,1 +0,1 @@ | ||
"use strict";var J=Object.defineProperty,ne=Object.defineProperties,oe=Object.getOwnPropertyDescriptor,re=Object.getOwnPropertyDescriptors,ae=Object.getOwnPropertyNames,H=Object.getOwnPropertySymbols;var Q=Object.prototype.hasOwnProperty,se=Object.prototype.propertyIsEnumerable;var V=(t,e,n)=>e in t?J(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,$=(t,e)=>{for(var n in e||(e={}))Q.call(e,n)&&V(t,n,e[n]);if(H)for(var n of H(e))se.call(e,n)&&V(t,n,e[n]);return t},q=(t,e)=>ne(t,re(e));var ie=(t,e)=>{for(var n in e)J(t,n,{get:e[n],enumerable:!0})},ge=(t,e,n,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of ae(e))!Q.call(t,s)&&s!==n&&J(t,s,{get:()=>e[s],enumerable:!(a=oe(e,s))||a.enumerable});return t};var le=t=>ge(J({},"__esModule",{value:!0}),t);var ue={};ie(ue,{CODE:()=>S,Cache:()=>M,CacheStorage:()=>u,StorageType:()=>l,UseCacheConfig:()=>r,currentHost:()=>_,encodeUmi:()=>k,fetchWithLoading:()=>G,genericFetch:()=>B,getDataFromBox:()=>N,useCache:()=>X,useCacheList:()=>Z});module.exports=le(ue);var l={OnlySessionStorage:1,OnlyLocalStorage:2,BothStorage:3,NONE:0};var r={EnableLog:!1,cacheKeyPrefix:"",defaultIdentiyKey:"_id",defaultStorageType:l.OnlySessionStorage,PageSize:10},_=()=>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=r.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))),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=r.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))},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)}};var M={findOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType;if(a===l.NONE)return;let s=n||r.defaultIdentiyKey,g=u.getItem(t,a);if(g){let o=JSON.parse(g);if(o&&o.length>0){for(let i=0;i<o.length;i++)if(o[i][s]===e)return o[i]}}},findMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType;if(a===l.NONE)return;let s=n||r.defaultIdentiyKey,g=u.getItem(t,a);if(g){let o=JSON.parse(g);if(o&&o.length>0){for(let i=0;i<o.length;i++){let f=o[i];for(let d=0;d<e.length;d++)f[s]===e[d]&&o.push(f)}return o}}},onAddOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.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]:r.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType;if(a===l.NONE)return;let s=n||r.defaultIdentiyKey,g=u.getItem(t,a);if(g){let o=JSON.parse(g);if(o&&o.length>0){for(let i=0;i<o.length;i++)if(o[i][s]===e[s]){r.EnableLog&&console.log(`onEditOne, e[${s}]=${e[s]}, shortKey=${t}`),o[i]=e,u.saveObject(t,o);return}r.EnableLog&&console.log(`onEditOne\uFF1Anot found in list, key=${s}, shortKey=${t}`);return}}else r.EnableLog&&console.log("onEditOne\uFF1Anot found list: shortKey="+t)},onEditMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType;if(a===l.NONE)return;let s=n||r.defaultIdentiyKey,g=u.getItem(t,a);if(g){let o=JSON.parse(g);if(o&&o.length>0){for(let i=0;i<e.length;i++){let f=e[i];for(let d=0;d<o.length;d++)o[d][s]===f[s]&&(o[d]=f)}u.saveItem(t,JSON.stringify(o));return}else{u.saveItem(t,JSON.stringify(e));return}}else r.EnableLog&&console.log("onEditMany: not found list, shortKey="+t)},onDelOneById:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType)===l.NONE)return;let s=n||r.defaultIdentiyKey,g=u.getItem(t);if(g){let o=JSON.parse(g);if(o&&o.length>0){for(let i=0;i<o.length;i++)if(o[i][s]===e)return r.EnableLog&&console.log(`del one: ${s}=${e}`),o.splice(i,1),u.saveItem(t,JSON.stringify(o)),o}}},onDelOne:function(t,e){var n;let a=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.defaultIdentiyKey,s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType;if(s===l.NONE)return;let g=a||r.defaultIdentiyKey,o=(n=e[g])===null||n===void 0?void 0:n.toString();o?M.onDelOneById(t,o,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]:r.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType)===l.NONE)return;let s=n||r.defaultIdentiyKey,g=u.getItem(t);if(g){let o=JSON.parse(g);if(o&&o.length>0){for(let i=0;i<o.length;i++)for(let f=0;f<e.length;f++){let d=e[f];o[i][s]===d&&(r.EnableLog&&console.log(`del one: ${s}=${d}`),o.splice(i,1))}return u.saveItem(t,JSON.stringify(o)),o}}},onDelMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType;if(a===l.NONE)return;let s=n||r.defaultIdentiyKey,g=e.map(o=>{var i;return(i=o[s])===null||i===void 0?void 0:i.toString()}).filter(o=>!!o);g&&g.length>0?M.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]:r.defaultStorageType,n=r.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]:r.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 k=t=>encodeURIComponent(JSON.stringify(t));var T=require("react");function G(t,e,n){var a,s,g,o;let i=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType,f=arguments.length>4?arguments[4]:void 0,d=arguments.length>5?arguments[5]:void 0,m=arguments.length>6?arguments[6]:void 0,O=arguments.length>7?arguments[7]:void 0;B(t,e,n,i,f||((a=r.request)===null||a===void 0?void 0:a.showToast),d||((s=r.request)===null||s===void 0?void 0:s.showToast),m||((g=r.request)===null||g===void 0?void 0:g.showLoading),O||((o=r.request)===null||o===void 0?void 0:o.hideLoading))}function B(t,e,n){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:r.defaultStorageType,s=arguments.length>4?arguments[4]:void 0,g=arguments.length>5?arguments[5]:void 0,o=arguments.length>6?arguments[6]:void 0,i=arguments.length>7?arguments[7]:void 0;if(n){let f=u.getObject(n,a);if(f)return e(f),!0}else o&&o(),t().then(f=>{i&&i();let d=f.data;if(d.code===S.OK){let m=N(d);return m===void 0?s&&s():(n&&u.saveItem(n,JSON.stringify(m),a),e(m)),!1}else return g&&g(d.msg||d.code),!1}).catch(f=>(i&&i(),g&&g(f.message),!1));return!1}function X(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]:r.defaultStorageType,[g,o]=(0,T.useState)(!1),[i,f]=(0,T.useState)(),[d,m]=(0,T.useState)();return(0,T.useEffect)(()=>{var O,h;o(!0);let x=n?(O=r.request)===null||O===void 0?void 0:O.getWithouAuth:(h=r.request)===null||h===void 0?void 0:h.get;if(!x)console.warn("not config request promise, please set ConfigRequest firstly");else{var v,L,p,E;a&&(!((v=r.request)!==null&&v!==void 0&&v.showLoading)||!((L=r.request)!==null&&L!==void 0&&L.hideLoading))&&console.warn("not config request showLoading/hideLoading, please set ConfigRequest firstly"),B(()=>x(t),I=>{o(!1),f(I)},e,s,()=>{o(!1),m("\u6570\u636E\u79FB\u6C11\u706B\u661F\u4E86^_^")},I=>{o(!1),m(I)},(p=r.request)===null||p===void 0?void 0:p.showLoading,(E=r.request)===null||E===void 0?void 0:E.hideLoading)}},[t]),{loading:g,entity:i,errMsg:d}}var y=require("react");function Z(t,e,n){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!0,s=arguments.length>4&&arguments[4]!==void 0?arguments[4]:r.defaultStorageType,[g,o]=(0,y.useState)(),[i,f]=(0,y.useState)(n),[d,m]=(0,y.useState)(!0),[O,h]=(0,y.useState)(!1),[x,v]=(0,y.useState)(),[L,p]=(0,y.useState)(e?Y(e):!0),[E,I]=(0,y.useState)(!!e),[ee,j]=(0,y.useState)(!1),[A,te]=(0,y.useState)(0),K=(c,b)=>{var U;r.EnableLog&&console.log("fetch from remote... shortKey="+e+", query="+JSON.stringify(c));let R=(U=r.request)===null||U===void 0?void 0:U.get;if(!R){console.warn("please inject get request firstly");return}R(t,c!=null&&c.pagination?q($({},c),{umi:k(c.pagination),sort:void 0,pagination:void 0}):q($({},c),{sort:void 0})).then(F=>{m(!1),I(!0);let w=F.data,C=N(w);if(w.code===S.OK)if(C){h(!1);let z=ee&&g&&g.length>0?g.concat(C):C;if(o(z),e&&u.saveObject(e,z,s),a){var P;let W=C.length>=((c==null||(P=c.pagination)===null||P===void 0?void 0:P.pageSize)||r.PageSize);p(W),e&&D(e,W)}}else h(!0),a&&(v("no data"),e&&D(e,!1));else h(!0),a&&(p(!1),e&&D(e,!1)),v(w.msg||w.code);b&&b(C),j(!1)}).catch(F=>{I(!0),b&&b(),m(!1),h(!0),v(F.message),a&&(e&&D(e,!1),p(!1)),j(!1)})};return(0,y.useEffect)(()=>{if(r.EnableLog&&console.log("in useEffect loading, url="+t+", query="+JSON.stringify(i)),m(!0),E&&e){let c=u.getObject(e,s);c?(r.EnableLog&&console.log("fetch from local cache... shortKey="+e),o(c),m(!1),p(Y(e))):(r.EnableLog&&console.log("no local cache, try from remote..."),K(i))}else r.EnableLog&&console.log("useCache=false, try from remote..."),K(i)},[t,i,A]),{isLoading:d,isError:O,errMsg:x,loadMoreState:L,query:i,setQuery:f,list:g,setList:o,fetchDataFromRemote:K,refresh:A,setRefresh:te,setUseCache:I,setIsLoadMore:j}}function Y(t){let e=r.cacheKeyPrefix+t+"/loadMore",n=sessionStorage.getItem(e);return!(n&&n==="0")}function D(t,e){let n=r.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 J=Object.defineProperty,ne=Object.defineProperties,oe=Object.getOwnPropertyDescriptor,re=Object.getOwnPropertyDescriptors,ae=Object.getOwnPropertyNames,W=Object.getOwnPropertySymbols;var Q=Object.prototype.hasOwnProperty,se=Object.prototype.propertyIsEnumerable;var H=(t,e,n)=>e in t?J(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,P=(t,e)=>{for(var n in e||(e={}))Q.call(e,n)&&H(t,n,e[n]);if(W)for(var n of W(e))se.call(e,n)&&H(t,n,e[n]);return t},q=(t,e)=>ne(t,re(e));var ie=(t,e)=>{for(var n in e)J(t,n,{get:e[n],enumerable:!0})},ge=(t,e,n,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of ae(e))!Q.call(t,s)&&s!==n&&J(t,s,{get:()=>e[s],enumerable:!(a=oe(e,s))||a.enumerable});return t};var le=t=>ge(J({},"__esModule",{value:!0}),t);var fe={};ie(fe,{CODE:()=>h,Cache:()=>M,CacheStorage:()=>f,StorageType:()=>l,UseCacheConfig:()=>o,currentHost:()=>_,encodeUmi:()=>F,fetchWithLoading:()=>G,genericFetch:()=>B,getDataFromBox:()=>E,useCache:()=>X,useCacheList:()=>Z});module.exports=le(fe);var l={OnlySessionStorage:1,OnlyLocalStorage:2,BothStorage:3,NONE:0};var o={EnableLog:!1,cacheKeyPrefix:"",defaultIdentiyKey:"_id",defaultStorageType:l.OnlySessionStorage,PageSize:10},_=()=>window.location.protocol+"//"+window.location.host;var f={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}, v=${a}, 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=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]: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 f.saveItem(t,JSON.stringify(e),n)}};var M={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=f.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=f.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 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===l.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===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.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,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===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.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 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)===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.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),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===l.NONE)return;let g=a||o.defaultIdentiyKey,r=(n=e[g])===null||n===void 0?void 0:n.toString();r?M.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=f.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 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===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?M.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 h;(function(t){t.OK="OK",t.KO="KO",t.NewUser="NewUser",t.TokenExpired="TokenExpired"})(h||(h={}));function E(t){if(t){if(t.code===h.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 F=t=>encodeURIComponent(JSON.stringify(t));var N=require("react");function G(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,u=arguments.length>5?arguments[5]:void 0,m=arguments.length>6?arguments[6]:void 0,O=arguments.length>7?arguments[7]:void 0;B(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),m||((g=o.request)===null||g===void 0?void 0:g.showLoading),O||((r=o.request)===null||r===void 0?void 0:r.hideLoading))}function B(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=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)}else r&&r(),t().then(d=>{i&&i();let u=d.data;if(u.code===h.OK){let m=E(u);return m===void 0?s&&(o.EnableLog&&console.log("genericFetch: no data from remote server"),s()):(n&&f.saveItem(n,JSON.stringify(m),a),e(m)),!1}else return g&&(o.EnableLog&&console.log("genericFetch: fail from remote server: code="+u.code+",msg="+u.msg),g(u.msg||u.code)),!1}).catch(d=>(i&&i(),g&&g(d.message),o.EnableLog&&console.log("genericFetch exception from remote server:",d),!1));return!1}function X(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,N.useState)(!1),[i,d]=(0,N.useState)(),[u,m]=(0,N.useState)();return(0,N.useEffect)(()=>{var O,S;r(!0);let x=n?(O=o.request)===null||O===void 0?void 0:O.getWithouAuth:(S=o.request)===null||S===void 0?void 0:S.get;if(!x)console.warn("not config request promise, please set ConfigRequest firstly");else{var v,T,p,C;a&&(!((v=o.request)!==null&&v!==void 0&&v.showLoading)||!((T=o.request)!==null&&T!==void 0&&T.hideLoading))&&console.warn("not config request showLoading/hideLoading, please set ConfigRequest firstly"),B(()=>x(t),I=>{r(!1),d(I)},e,s,()=>{r(!1),m("\u6570\u636E\u79FB\u6C11\u706B\u661F\u4E86^_^")},I=>{r(!1),m(I)},(p=o.request)===null||p===void 0?void 0:p.showLoading,(C=o.request)===null||C===void 0?void 0:C.hideLoading)}},[t]),{loading:g,entity:i,errMsg:u}}var y=require("react");function Z(t,e,n){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!0,s=arguments.length>4&&arguments[4]!==void 0?arguments[4]:o.defaultStorageType,[g,r]=(0,y.useState)(),[i,d]=(0,y.useState)(n),[u,m]=(0,y.useState)(!0),[O,S]=(0,y.useState)(!1),[x,v]=(0,y.useState)(),[T,p]=(0,y.useState)(e?Y(e):!0),[C,I]=(0,y.useState)(!!e),[ee,j]=(0,y.useState)(!1),[A,te]=(0,y.useState)(0),K=(c,w)=>{var $;o.EnableLog&&console.log("fetch from remote... shortKey="+e+", query="+JSON.stringify(c));let R=($=o.request)===null||$===void 0?void 0:$.get;if(!R){console.warn("please inject get request firstly");return}R(t,c!=null&&c.pagination?q(P({},c),{umi:F(c.pagination),sort:void 0,pagination:void 0}):q(P({},c),{sort:void 0})).then(k=>{m(!1),I(!0);let L=k.data,b=E(L);if(L.code===h.OK)if(b){S(!1);let z=ee&&g&&g.length>0?g.concat(b):b;if(r(z),e&&f.saveObject(e,z,s),a){var U;let V=b.length>=((c==null||(U=c.pagination)===null||U===void 0?void 0:U.pageSize)||o.PageSize);p(V),e&&D(e,V)}}else S(!0),a&&(v("no data"),e&&D(e,!1));else S(!0),a&&(p(!1),e&&D(e,!1)),v(L.msg||L.code),o.EnableLog&&console.log("useCacheList: fail from remote server: code="+L.code+",msg="+L.msg);w&&w(b),j(!1)}).catch(k=>{I(!0),w&&w(),m(!1),S(!0),v(k.message),a&&(e&&D(e,!1),p(!1)),j(!1),o.EnableLog&&console.log("useCacheList exception from remote server:",k)})};return(0,y.useEffect)(()=>{if(o.EnableLog&&console.log("in useEffect loading, url="+t+", query="+JSON.stringify(i)),m(!0),C&&e){let c=f.getObject(e,s);c?(o.EnableLog&&console.log("fetch from local cache... shortKey="+e),r(c),m(!1),p(Y(e))):(o.EnableLog&&console.log("no local cache, try from remote..."),K(i))}else o.EnableLog&&console.log("useCache=false, try from remote..."),K(i)},[t,i,A]),{isLoading:u,isError:O,errMsg:x,loadMoreState:T,query:i,setQuery:d,list:g,setList:r,fetchDataFromRemote:K,refresh:A,setRefresh:te,setUseCache:I,setIsLoadMore:j}}function Y(t){let e=o.cacheKeyPrefix+t+"/loadMore",n=sessionStorage.getItem(e);return!(n&&n==="0")}function D(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.1", | ||
"version": "0.9.2", | ||
"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
74063
401