idmp
Version:
A lightweight TypeScript library for deduplicating and caching async function calls with automatic retries, designed for idempotent network requests in React and Node.js.
3 lines (2 loc) • 1.73 kB
JavaScript
/*! idmp v0.0.0 | (c) github/haozi | MIT */
(function(c,u){typeof exports=="object"&&typeof module!="undefined"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(c=typeof globalThis!="undefined"?globalThis:c||self,u(c.idmp={}))})(this,function(c){"use strict";var I=(c,u,y)=>new Promise((D,h)=>{var p=r=>{try{l(y.next(r))}catch(g){h(g)}},E=r=>{try{l(y.throw(r))}catch(g){h(g)}},l=r=>r.done?D(r.value):Promise.resolve(r.value).then(p,E);l((y=y.apply(c,u)).next())});/*! idmp v3.4.4 | (c) github/haozi | MIT */const D=()=>{},h=n=>n<0?0:n>6048e5?6048e5:n,p=n=>{const{maxRetry:o=30,maxAge:s=3e3,minRetryDelay:d=50,maxRetryDelay:a=5e3,onBeforeRetry:m=D,signal:t}=n||{},e=h(s);return{maxRetry:o,maxAge:e,minRetryDelay:d,maxRetryDelay:a,onBeforeRetry:m,f:s===1/0,signal:t}},l="@idmp/v1/",r=n=>`${l}${n}`,g=n=>{let o;try{o=window[n]}catch(t){}const s=t=>{if(t)try{const e=r(t);o.removeItem(e)}catch(e){}};return{get:t=>{if(!t)return;const e=r(t);let i;try{if(i=JSON.parse(o.getItem(e)||""),i===void 0)return;const{t:f,a:A,d:v}=i;if(Date.now()-f>A){s(e);return}return v}catch(f){}},set:(t,e,i)=>{if(!t)return;const f=r(t);try{o.setItem(f,JSON.stringify({t:Date.now(),a:i,d:e}))}catch(A){}},remove:s,clear:()=>{try{for(let t=o.length-1;t>=0;t--){const e=o.key(t);e&&e.startsWith(l)&&s(e)}}catch(t){}}}},N=(n,o="sessionStorage")=>{const s=g(o),d=(a,m,t)=>{const e=p(t);return n(a,()=>I(null,null,function*(){const i=s.get(a);if(i!==void 0)return i;const f=yield m();return f!==void 0&&s.set(a,f,e.maxAge),f}),t)};return d.flush=a=>{n.flush(a),s.remove(a)},d.flushAll=()=>{n.flushAll(),s.clear()},d};c.default=N,c.getCacheKey=r,Object.defineProperties(c,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});