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

swr

Package Overview
Dependencies
Maintainers
54
Versions
162
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swr - npm Package Compare versions

Comparing version 0.1.14 to 0.1.15

2

dist/index.js

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

module.exports=function(e,r){"use strict";var t={};function __webpack_require__(r){if(t[r]){return t[r].exports}var n=t[r]={i:r,l:false,exports:{}};e[r].call(n.exports,n,n.exports,__webpack_require__);n.l=true;return n.exports}__webpack_require__.ab=__dirname+"/";function startup(){return __webpack_require__(310)}return startup()}({75:function(e,r){"use strict";Object.defineProperty(r,"__esModule",{value:true});var t=new WeakMap;var n=0;function hash(e){if(!e.length)return"";var r="arg";for(var u=0;u<e.length;++u){var a=void 0;if(typeof e[u]!=="object"){if(typeof e[u]==="string"){a='"'+e[u]+'"'}else{a=String(e[u])}}else{if(!t.has(e[u])){a=n;t.set(e[u],n++)}else{a=t.get(e[u])}}r+="@"+a}return r}r.default=hash},153:function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:true});var n=t(297);var u=n.createContext({});u.displayName="SWRConfigContext";r.default=u},297:function(e){e.exports=require("react")},310:function(e,r,t){"use strict";function __export(e){for(var t in e)if(!r.hasOwnProperty(t))r[t]=e[t]}var n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(r,"__esModule",{value:true});__export(t(797));var u=n(t(797));var a=t(319);r.useSWRPages=a.useSWRPages;r.default=u.default},319:function(e,r,t){"use strict";var n=this&&this.__spreadArrays||function(){for(var e=0,r=0,t=arguments.length;r<t;r++)e+=arguments[r].length;for(var n=Array(e),u=0,r=0;r<t;r++)for(var a=arguments[r],i=0,f=a.length;i<f;i++,u++)n[u]=a[i];return n};var u=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var r={};if(e!=null)for(var t in e)if(Object.hasOwnProperty.call(e,t))r[t]=e[t];r["default"]=e;return r};Object.defineProperty(r,"__esModule",{value:true});var a=u(t(297));var i=t(546);function useSWRPages(e,r,t,u){if(u===void 0){u=[]}var f="_swr_page_count_"+e;var o="_swr_page_offset_"+e;var l=a.useState(i.cacheGet(f)||1),c=l[0],s=l[1];var d=a.useState(i.cacheGet(o)||[null]),v=d[0],p=d[1];var R=a.useState([]),S=R[0],_=R[1];var h=a.useRef([]);var O=a.useRef(r);var E=a.useRef(false);var C=a.useCallback(function(e){var r=O.current(e);if(r&&!r.length){E.current=true}else{E.current=false}return r},[]);var y=v[c]===null;var T=c===v.length;var b=y&&c===1&&E.current;var w=a.useCallback(function(){if(T||y)return;s(function(e){i.cacheSet(f,e+1);return e+1})},[T||y]);var g=a.useCallback(r,u);O.current=g;var I=a.useMemo(function(){var e=function(e){return function(r){if(!S[e]||S[e].data!==r.data||S[e].error!==r.error||S[e].revalidate!==r.revalidate){_(function(t){var u=n(t);u[e]=r;return u});if(typeof r.data!=="undefined"){var u=t(r,e);if(v[e+1]!==u){p(function(r){var t=n(r);t[e+1]=u;i.cacheSet(o,t);return t})}}}return r}};var r=[];var u=h.current;for(var f=0;f<c;++f){if(!u[f]||u[f].offset!==v[f]||u[f].pageFn!==g){u[f]={component:a.default.createElement(C,{key:"page-"+v[f]+"-"+f,offset:v[f],withSWR:e(f)}),pageFn:g,offset:v[f]}}r.push(u[f].component)}return r},[g,c,S,v,e]);return{pages:I,pageCount:c,pageSWRs:S,isLoadingMore:T,isReachingEnd:y,isEmpty:b,loadMore:w}}r.useSWRPages=useSWRPages},447:function(e,r){"use strict";Object.defineProperty(r,"__esModule",{value:true});function isOnline(){if(typeof navigator.onLine!=="undefined"){return navigator.onLine}return true}r.default=isOnline},453:function(e,r){"use strict";Object.defineProperty(r,"__esModule",{value:true});function throttle(e,r){var t=false;return function(){var n=[];for(var u=0;u<arguments.length;u++){n[u]=arguments[u]}if(t)return;t=true;e.apply(void 0,n);setTimeout(function(){return t=false},r)}}r.default=throttle},491:function(e){"use strict";var r=Array.isArray;var t=Object.keys;var n=Object.prototype.hasOwnProperty;e.exports=function equal(e,u){if(e===u)return true;if(e&&u&&typeof e=="object"&&typeof u=="object"){var a=r(e),i=r(u),f,o,l;if(a&&i){o=e.length;if(o!=u.length)return false;for(f=o;f--!==0;)if(!equal(e[f],u[f]))return false;return true}if(a!=i)return false;var c=e instanceof Date,s=u instanceof Date;if(c!=s)return false;if(c&&s)return e.getTime()==u.getTime();var d=e instanceof RegExp,v=u instanceof RegExp;if(d!=v)return false;if(d&&v)return e.toString()==u.toString();var p=t(e);o=p.length;if(o!==t(u).length)return false;for(f=o;f--!==0;)if(!n.call(u,p[f]))return false;for(f=o;f--!==0;){l=p[f];if(!equal(e[l],u[l]))return false}return true}return e!==e&&u!==u}},546:function(e,r,t){"use strict";var n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(r,"__esModule",{value:true});var u=n(t(731));var a=n(t(447));var i=new Map;function cacheGet(e){return i.get(e)}r.cacheGet=cacheGet;function cacheSet(e,r){return i.set(e,r)}r.cacheSet=cacheSet;function cacheClear(){i.clear()}r.cacheClear=cacheClear;var f={};r.CONCURRENT_PROMISES=f;var o={};r.CONCURRENT_PROMISES_TS=o;var l={};r.FOCUS_REVALIDATORS=l;var c={};r.CACHE_REVALIDATORS=c;var s={};r.MUTATION_TS=s;function onErrorRetry(e,r,t,n,a){if(!u.default()){return}var i=Math.min(a.retryCount||0,8);var f=~~((Math.random()+.5)*(1<<i))*t.errorRetryInterval;setTimeout(n,f,a)}var d={onLoadingSlow:function(){},onSuccess:function(){},onError:function(){},onErrorRetry:onErrorRetry,errorRetryInterval:5*1e3,focusThrottleInterval:5*1e3,dedupingInterval:2*1e3,loadingTimeout:3*1e3,refreshInterval:0,revalidateOnFocus:true,refreshWhenHidden:false,shouldRetryOnError:true,suspense:false};if(typeof window!=="undefined"){if(navigator["connection"]){if(["slow-2g","2g"].indexOf(navigator["connection"].effectiveType)!==-1){d.errorRetryInterval=10*1e3;d.loadingTimeout=5*1e3}}}var v=false;if(typeof window!=="undefined"&&window.addEventListener&&!v){var p=function(){if(!u.default()||!a.default())return;for(var e in l){if(l[e][0])l[e][0]()}};window.addEventListener("visibilitychange",p,false);window.addEventListener("focus",p,false);v=true}r.default=d},731:function(e,r){"use strict";Object.defineProperty(r,"__esModule",{value:true});function isDocumentVisible(){if(typeof document!=="undefined"&&typeof document.visibilityState!=="undefined"){return document.visibilityState!=="hidden"}return true}r.default=isDocumentVisible},797:function(e,r,t){"use strict";var n=this&&this.__assign||function(){n=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++){r=arguments[t];for(var u in r)if(Object.prototype.hasOwnProperty.call(r,u))e[u]=r[u]}return e};return n.apply(this,arguments)};var u=this&&this.__awaiter||function(e,r,t,n){function adopt(e){return e instanceof t?e:new t(function(r){r(e)})}return new(t||(t=Promise))(function(t,u){function fulfilled(e){try{step(n.next(e))}catch(e){u(e)}}function rejected(e){try{step(n["throw"](e))}catch(e){u(e)}}function step(e){e.done?t(e.value):adopt(e.value).then(fulfilled,rejected)}step((n=n.apply(e,r||[])).next())})};var a=this&&this.__generator||function(e,r){var t={label:0,sent:function(){if(a[0]&1)throw a[1];return a[1]},trys:[],ops:[]},n,u,a,i;return i={next:verb(0),throw:verb(1),return:verb(2)},typeof Symbol==="function"&&(i[Symbol.iterator]=function(){return this}),i;function verb(e){return function(r){return step([e,r])}}function step(i){if(n)throw new TypeError("Generator is already executing.");while(t)try{if(n=1,u&&(a=i[0]&2?u["return"]:i[0]?u["throw"]||((a=u["return"])&&a.call(u),0):u.next)&&!(a=a.call(u,i[1])).done)return a;if(u=0,a)i=[i[0]&2,a.value];switch(i[0]){case 0:case 1:a=i;break;case 4:t.label++;return{value:i[1],done:false};case 5:t.label++;u=i[1];i=[0];continue;case 7:i=t.ops.pop();t.trys.pop();continue;default:if(!(a=t.trys,a=a.length>0&&a[a.length-1])&&(i[0]===6||i[0]===2)){t=0;continue}if(i[0]===3&&(!a||i[1]>a[0]&&i[1]<a[3])){t.label=i[1];break}if(i[0]===6&&t.label<a[1]){t.label=a[1];a=i;break}if(a&&t.label<a[2]){t.label=a[2];t.ops.push(i);break}if(a[2])t.ops.pop();t.trys.pop();continue}i=r.call(e,t)}catch(e){i=[6,e];u=0}finally{n=a=0}if(i[0]&5)throw i[1];return{value:i[0]?i[1]:void 0,done:true}}};var i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};var f=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var r={};if(e!=null)for(var t in e)if(Object.hasOwnProperty.call(e,t))r[t]=e[t];r["default"]=e;return r};Object.defineProperty(r,"__esModule",{value:true});var o=t(297);var l=i(t(491));var c=f(t(546));var s=i(t(153));var d=i(t(731));var v=i(t(453));var p=i(t(75));var R=typeof window==="undefined";var S=R?o.useEffect:o.useLayoutEffect;var _=function(e){return e?"err@"+e:""};var h=function(e){var r=null;if(typeof e==="function"){try{e=e()}catch(r){e=""}}if(Array.isArray(e)){r=e;e=p.default(e)}else{e=String(e||"")}return[e,r]};var O=function(e,r){if(r===void 0){r=true}var t=h(e)[0];if(!t)return;var n=c.CACHE_REVALIDATORS[t];if(t&&n){var u=c.cacheGet(t);var a=c.cacheGet(_(t));for(var i=0;i<n.length;++i){n[i](r,u,a,true)}}};r.trigger=O;var E=function(e,r,t){var n=c.CACHE_REVALIDATORS[e];if(e&&n){for(var u=0;u<n.length;++u){n[u](false,r,t)}}};var C=function(e,r,t){return u(void 0,void 0,void 0,function(){var n,u,i,f,o,l;return a(this,function(a){switch(a.label){case 0:n=h(e)[0];if(!n)return[2];c.MUTATION_TS[n]=Date.now()-1;if(!(r&&typeof r.then==="function"))return[3,5];a.label=1;case 1:a.trys.push([1,3,,4]);return[4,r];case 2:u=a.sent();return[3,4];case 3:f=a.sent();i=f;return[3,4];case 4:return[3,6];case 5:u=r;if(typeof t==="undefined"){t=true}a.label=6;case 6:if(typeof u!=="undefined"){c.cacheSet(n,u)}o=c.CACHE_REVALIDATORS[n];if(o){for(l=0;l<o.length;++l){o[l](!!t,u,i,true)}}return[2]}})})};r.mutate=C;function mergeState(e,r){return n(n({},e),r)}function useSWR(){var e=this;var r=[];for(var t=0;t<arguments.length;t++){r[t]=arguments[t]}var n,i,f={};if(r.length>=1){n=r[0]}if(r.length>2){i=r[1];f=r[2]}else{if(typeof r[1]==="function"){i=r[1]}else if(typeof r[1]==="object"){f=r[1]}}var p=h(n),O=p[0],C=p[1];var y=_(O);f=Object.assign({},c.default,o.useContext(s.default),f);if(typeof i==="undefined"){i=f.fetcher}var T=c.cacheGet(O)||f.initialData;var b=c.cacheGet(y);var w=o.useReducer(mergeState,{data:T,error:b,isValidating:false}),g=w[0],I=w[1];var A=o.useRef(false);var M=o.useRef(O);var N=o.useRef(T);var P=o.useRef(b);var m=o.useCallback(function(r){if(r===void 0){r={}}return u(e,void 0,void 0,function(){var e,t,n,u,o,s,d;return a(this,function(a){switch(a.label){case 0:if(!O||!i)return[2,false];if(A.current)return[2,false];r=Object.assign({dedupe:false},r);e=true;t=typeof c.CONCURRENT_PROMISES[O]!=="undefined"&&r.dedupe;a.label=1;case 1:a.trys.push([1,6,,7]);I({isValidating:true});n=void 0;u=void 0;if(!t)return[3,3];u=c.CONCURRENT_PROMISES_TS[O];return[4,c.CONCURRENT_PROMISES[O]];case 2:n=a.sent();return[3,5];case 3:if(c.CONCURRENT_PROMISES[O]){c.MUTATION_TS[O]=Date.now()-1}if(f.loadingTimeout&&!c.cacheGet(O)){setTimeout(function(){if(e)f.onLoadingSlow(O,f)},f.loadingTimeout)}if(C!==null){c.CONCURRENT_PROMISES[O]=i.apply(void 0,C)}else{c.CONCURRENT_PROMISES[O]=i(O)}c.CONCURRENT_PROMISES_TS[O]=u=Date.now();setTimeout(function(){delete c.CONCURRENT_PROMISES[O];delete c.CONCURRENT_PROMISES_TS[O]},f.dedupingInterval);return[4,c.CONCURRENT_PROMISES[O]];case 4:n=a.sent();f.onSuccess(n,O,f);a.label=5;case 5:if(c.MUTATION_TS[O]&&u<=c.MUTATION_TS[O]){I({isValidating:false});return[2,false]}c.cacheSet(O,n);c.cacheSet(y,undefined);M.current=O;o={isValidating:false};if(typeof P.current!=="undefined"){o.error=undefined;P.current=undefined}if(l.default(N.current,n)){}else{o.data=n;N.current=n}I(o);if(!t){E(O,n,undefined)}return[3,7];case 6:s=a.sent();delete c.CONCURRENT_PROMISES[O];delete c.CONCURRENT_PROMISES_TS[O];c.cacheSet(y,s);M.current=O;if(P.current!==s){P.current=s;I({isValidating:false,error:s});if(!t){E(O,undefined,s)}}f.onError(s,O,f);if(f.shouldRetryOnError){d=(r.retryCount||0)+1;f.onErrorRetry(s,O,f,m,Object.assign({dedupe:true},r,{retryCount:d}))}return[3,7];case 7:e=false;return[2,true]}})})},[O]);S(function(){if(!O)return undefined;A.current=false;var r=N.current;var t=c.cacheGet(O)||f.initialData;if(M.current!==O||!l.default(r,t)){I({data:t});N.current=t;M.current=O}var n=function(){return m({dedupe:true})};if(typeof t!=="undefined"&&!R&&window["requestIdleCallback"]){window["requestIdleCallback"](n)}else{n()}var i;if(f.revalidateOnFocus){i=v.default(n,f.focusThrottleInterval);if(!c.FOCUS_REVALIDATORS[O]){c.FOCUS_REVALIDATORS[O]=[i]}else{c.FOCUS_REVALIDATORS[O].push(i)}}var o=function(e,r,t,u){if(e===void 0){e=true}if(u===void 0){u=true}var a={};if(typeof r!=="undefined"&&!l.default(N.current,r)){a.data=r;N.current=r}if(P.current!==t){a.error=t;P.current=t}I(a);M.current=O;if(e){if(u){return n()}else{return m()}}return false};if(!c.CACHE_REVALIDATORS[O]){c.CACHE_REVALIDATORS[O]=[o]}else{c.CACHE_REVALIDATORS[O].push(o)}var s=null;if(f.refreshInterval){var p=function(){return u(e,void 0,void 0,function(){var e;return a(this,function(r){switch(r.label){case 0:if(!(!P.current&&(f.refreshWhenHidden||d.default())))return[3,2];return[4,n()];case 1:r.sent();r.label=2;case 2:e=f.refreshInterval;s=setTimeout(p,e);return[2]}})})};s=setTimeout(p,f.refreshInterval)}return function(){I=function(){return null};A.current=true;if(i&&c.FOCUS_REVALIDATORS[O]){var e=c.FOCUS_REVALIDATORS[O];var r=e.indexOf(i);if(r>=0){e[r]=e[e.length-1];e.pop()}}if(c.CACHE_REVALIDATORS[O]){var e=c.CACHE_REVALIDATORS[O];var r=e.indexOf(o);if(r>=0){e[r]=e[e.length-1];e.pop()}}if(s!==null){clearTimeout(s)}}},[O,f.refreshInterval,m]);if(f.suspense){if(R)throw new Error("Suspense on server side is not yet supported!");var U=c.cacheGet(O);var D=c.cacheGet(y);if(typeof U==="undefined"&&typeof D==="undefined"){if(!c.CONCURRENT_PROMISES[O]){m()}if(c.CONCURRENT_PROMISES[O]&&typeof c.CONCURRENT_PROMISES[O].then==="function"){throw c.CONCURRENT_PROMISES[O]}U=c.CONCURRENT_PROMISES[O]}if(typeof U==="undefined"&&D){throw D}return{error:D,data:U,revalidate:m,isValidating:g.isValidating}}return{error:M.current===O?g.error:b,data:M.current===O?g.data:T,revalidate:m,isValidating:g.isValidating}}var y=s.default.Provider;r.SWRConfig=y;r.default=useSWR}});
module.exports=function(e,r){"use strict";var t={};function __webpack_require__(r){if(t[r]){return t[r].exports}var n=t[r]={i:r,l:false,exports:{}};e[r].call(n.exports,n,n.exports,__webpack_require__);n.l=true;return n.exports}__webpack_require__.ab=__dirname+"/";function startup(){return __webpack_require__(310)}return startup()}({75:function(e,r){"use strict";Object.defineProperty(r,"__esModule",{value:true});var t=new WeakMap;var n=0;function hash(e){if(!e.length)return"";var r="arg";for(var a=0;a<e.length;++a){var u=void 0;if(typeof e[a]!=="object"){if(typeof e[a]==="string"){u='"'+e[a]+'"'}else{u=String(e[a])}}else{if(!t.has(e[a])){u=n;t.set(e[a],n++)}else{u=t.get(e[a])}}r+="@"+u}return r}r.default=hash},153:function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:true});var n=t(297);var a=n.createContext({});a.displayName="SWRConfigContext";r.default=a},297:function(e){e.exports=require("react")},310:function(e,r,t){"use strict";function __export(e){for(var t in e)if(!r.hasOwnProperty(t))r[t]=e[t]}var n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(r,"__esModule",{value:true});__export(t(797));var a=n(t(797));var u=t(319);r.useSWRPages=u.useSWRPages;r.default=a.default},319:function(e,r,t){"use strict";var n=this&&this.__spreadArrays||function(){for(var e=0,r=0,t=arguments.length;r<t;r++)e+=arguments[r].length;for(var n=Array(e),a=0,r=0;r<t;r++)for(var u=arguments[r],i=0,f=u.length;i<f;i++,a++)n[a]=u[i];return n};var a=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var r={};if(e!=null)for(var t in e)if(Object.hasOwnProperty.call(e,t))r[t]=e[t];r["default"]=e;return r};Object.defineProperty(r,"__esModule",{value:true});var u=a(t(297));var i=t(546);function useSWRPages(e,r,t,a){if(a===void 0){a=[]}var f="_swr_page_count_"+e;var o="_swr_page_offset_"+e;var l=u.useState(i.cacheGet(f)||1),c=l[0],s=l[1];var d=u.useState(i.cacheGet(o)||[null]),v=d[0],p=d[1];var R=u.useState([]),S=R[0],h=R[1];var _=u.useRef([]);var O=u.useRef(r);var E=u.useRef(false);var C=u.useCallback(function(e){var r=O.current(e);if(r&&!r.length){E.current=true}else{E.current=false}return r},[]);var y=v[c]===null;var T=c===v.length;var b=y&&c===1&&E.current;var w=u.useCallback(function(){if(T||y)return;s(function(e){i.cacheSet(f,e+1);return e+1})},[T||y]);var g=u.useCallback(r,a);O.current=g;var I=u.useMemo(function(){var e=function(e){return function(r){if(!S[e]||S[e].data!==r.data||S[e].error!==r.error||S[e].revalidate!==r.revalidate){h(function(t){var a=n(t);a[e]=r;return a});if(typeof r.data!=="undefined"){var a=t(r,e);if(v[e+1]!==a){p(function(r){var t=n(r);t[e+1]=a;i.cacheSet(o,t);return t})}}}return r}};var r=[];var a=_.current;for(var f=0;f<c;++f){if(!a[f]||a[f].offset!==v[f]||a[f].pageFn!==g){a[f]={component:u.default.createElement(C,{key:"page-"+v[f]+"-"+f,offset:v[f],withSWR:e(f)}),pageFn:g,offset:v[f]}}r.push(a[f].component)}return r},[g,c,S,v,e]);return{pages:I,pageCount:c,pageSWRs:S,isLoadingMore:T,isReachingEnd:y,isEmpty:b,loadMore:w}}r.useSWRPages=useSWRPages},447:function(e,r){"use strict";Object.defineProperty(r,"__esModule",{value:true});function isOnline(){if(typeof navigator.onLine!=="undefined"){return navigator.onLine}return true}r.default=isOnline},453:function(e,r){"use strict";Object.defineProperty(r,"__esModule",{value:true});function throttle(e,r){var t=false;return function(){var n=[];for(var a=0;a<arguments.length;a++){n[a]=arguments[a]}if(t)return;t=true;e.apply(void 0,n);setTimeout(function(){return t=false},r)}}r.default=throttle},491:function(e){"use strict";var r=Array.isArray;var t=Object.keys;var n=Object.prototype.hasOwnProperty;e.exports=function equal(e,a){if(e===a)return true;if(e&&a&&typeof e=="object"&&typeof a=="object"){var u=r(e),i=r(a),f,o,l;if(u&&i){o=e.length;if(o!=a.length)return false;for(f=o;f--!==0;)if(!equal(e[f],a[f]))return false;return true}if(u!=i)return false;var c=e instanceof Date,s=a instanceof Date;if(c!=s)return false;if(c&&s)return e.getTime()==a.getTime();var d=e instanceof RegExp,v=a instanceof RegExp;if(d!=v)return false;if(d&&v)return e.toString()==a.toString();var p=t(e);o=p.length;if(o!==t(a).length)return false;for(f=o;f--!==0;)if(!n.call(a,p[f]))return false;for(f=o;f--!==0;){l=p[f];if(!equal(e[l],a[l]))return false}return true}return e!==e&&a!==a}},546:function(e,r,t){"use strict";var n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(r,"__esModule",{value:true});var a=n(t(731));var u=n(t(447));var i=new Map;function cacheGet(e){return i.get(e)}r.cacheGet=cacheGet;function cacheSet(e,r){return i.set(e,r)}r.cacheSet=cacheSet;function cacheClear(){i.clear()}r.cacheClear=cacheClear;var f={};r.CONCURRENT_PROMISES=f;var o={};r.CONCURRENT_PROMISES_TS=o;var l={};r.FOCUS_REVALIDATORS=l;var c={};r.CACHE_REVALIDATORS=c;var s={};r.MUTATION_TS=s;function onErrorRetry(e,r,t,n,u){if(!a.default()){return}var i=Math.min(u.retryCount||0,8);var f=~~((Math.random()+.5)*(1<<i))*t.errorRetryInterval;setTimeout(n,f,u)}var d={onLoadingSlow:function(){},onSuccess:function(){},onError:function(){},onErrorRetry:onErrorRetry,errorRetryInterval:5*1e3,focusThrottleInterval:5*1e3,dedupingInterval:2*1e3,loadingTimeout:3*1e3,refreshInterval:0,revalidateOnFocus:true,revalidateOnReconnect:true,refreshWhenHidden:false,refreshWhenOffline:false,shouldRetryOnError:true,suspense:false};if(typeof window!=="undefined"){if(navigator["connection"]){if(["slow-2g","2g"].indexOf(navigator["connection"].effectiveType)!==-1){d.errorRetryInterval=10*1e3;d.loadingTimeout=5*1e3}}}var v=false;if(typeof window!=="undefined"&&window.addEventListener&&!v){var p=function(){if(!a.default()||!u.default())return;for(var e in l){if(l[e][0])l[e][0]()}};window.addEventListener("visibilitychange",p,false);window.addEventListener("focus",p,false);v=true}r.default=d},731:function(e,r){"use strict";Object.defineProperty(r,"__esModule",{value:true});function isDocumentVisible(){if(typeof document!=="undefined"&&typeof document.visibilityState!=="undefined"){return document.visibilityState!=="hidden"}return true}r.default=isDocumentVisible},797:function(e,r,t){"use strict";var n=this&&this.__assign||function(){n=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++){r=arguments[t];for(var a in r)if(Object.prototype.hasOwnProperty.call(r,a))e[a]=r[a]}return e};return n.apply(this,arguments)};var a=this&&this.__awaiter||function(e,r,t,n){function adopt(e){return e instanceof t?e:new t(function(r){r(e)})}return new(t||(t=Promise))(function(t,a){function fulfilled(e){try{step(n.next(e))}catch(e){a(e)}}function rejected(e){try{step(n["throw"](e))}catch(e){a(e)}}function step(e){e.done?t(e.value):adopt(e.value).then(fulfilled,rejected)}step((n=n.apply(e,r||[])).next())})};var u=this&&this.__generator||function(e,r){var t={label:0,sent:function(){if(u[0]&1)throw u[1];return u[1]},trys:[],ops:[]},n,a,u,i;return i={next:verb(0),throw:verb(1),return:verb(2)},typeof Symbol==="function"&&(i[Symbol.iterator]=function(){return this}),i;function verb(e){return function(r){return step([e,r])}}function step(i){if(n)throw new TypeError("Generator is already executing.");while(t)try{if(n=1,a&&(u=i[0]&2?a["return"]:i[0]?a["throw"]||((u=a["return"])&&u.call(a),0):a.next)&&!(u=u.call(a,i[1])).done)return u;if(a=0,u)i=[i[0]&2,u.value];switch(i[0]){case 0:case 1:u=i;break;case 4:t.label++;return{value:i[1],done:false};case 5:t.label++;a=i[1];i=[0];continue;case 7:i=t.ops.pop();t.trys.pop();continue;default:if(!(u=t.trys,u=u.length>0&&u[u.length-1])&&(i[0]===6||i[0]===2)){t=0;continue}if(i[0]===3&&(!u||i[1]>u[0]&&i[1]<u[3])){t.label=i[1];break}if(i[0]===6&&t.label<u[1]){t.label=u[1];u=i;break}if(u&&t.label<u[2]){t.label=u[2];t.ops.push(i);break}if(u[2])t.ops.pop();t.trys.pop();continue}i=r.call(e,t)}catch(e){i=[6,e];a=0}finally{n=u=0}if(i[0]&5)throw i[1];return{value:i[0]?i[1]:void 0,done:true}}};var i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};var f=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var r={};if(e!=null)for(var t in e)if(Object.hasOwnProperty.call(e,t))r[t]=e[t];r["default"]=e;return r};Object.defineProperty(r,"__esModule",{value:true});var o=i(t(491));var l=t(297);var c=f(t(546));var s=i(t(75));var d=i(t(731));var v=i(t(447));var p=i(t(453));var R=i(t(153));var S=typeof window==="undefined";var h=S?l.useEffect:l.useLayoutEffect;var _=function(e){return e?"err@"+e:""};var O=function(e){var r=null;if(typeof e==="function"){try{e=e()}catch(r){e=""}}if(Array.isArray(e)){r=e;e=s.default(e)}else{e=String(e||"")}return[e,r]};var E=function(e,r){if(r===void 0){r=true}var t=O(e)[0];if(!t)return;var n=c.CACHE_REVALIDATORS[t];if(t&&n){var a=c.cacheGet(t);var u=c.cacheGet(_(t));for(var i=0;i<n.length;++i){n[i](r,a,u,true)}}};r.trigger=E;var C=function(e,r,t){var n=c.CACHE_REVALIDATORS[e];if(e&&n){for(var a=0;a<n.length;++a){n[a](false,r,t)}}};var y=function(e,r,t){return a(void 0,void 0,void 0,function(){var n,a,i,f,o,l;return u(this,function(u){switch(u.label){case 0:n=O(e)[0];if(!n)return[2];c.MUTATION_TS[n]=Date.now()-1;if(!(r&&typeof r.then==="function"))return[3,5];u.label=1;case 1:u.trys.push([1,3,,4]);return[4,r];case 2:a=u.sent();return[3,4];case 3:f=u.sent();i=f;return[3,4];case 4:return[3,6];case 5:a=r;if(typeof t==="undefined"){t=true}u.label=6;case 6:if(typeof a!=="undefined"){c.cacheSet(n,a)}o=c.CACHE_REVALIDATORS[n];if(o){for(l=0;l<o.length;++l){o[l](!!t,a,i,true)}}return[2]}})})};r.mutate=y;function mergeState(e,r){return n(n({},e),r)}function useSWR(){var e=this;var r=[];for(var t=0;t<arguments.length;t++){r[t]=arguments[t]}var n,i,f={};if(r.length>=1){n=r[0]}if(r.length>2){i=r[1];f=r[2]}else{if(typeof r[1]==="function"){i=r[1]}else if(typeof r[1]==="object"){f=r[1]}}var s=O(n),E=s[0],y=s[1];var T=_(E);f=Object.assign({},c.default,l.useContext(R.default),f);if(typeof i==="undefined"){i=f.fetcher}var b=c.cacheGet(E)||f.initialData;var w=c.cacheGet(T);var g=l.useReducer(mergeState,{data:b,error:w,isValidating:false}),I=g[0],A=g[1];var M=l.useRef(false);var N=l.useRef(E);var m=l.useRef(b);var P=l.useRef(w);var U=l.useCallback(function(r){if(r===void 0){r={}}return a(e,void 0,void 0,function(){var e,t,n,a,l,s,d;return u(this,function(u){switch(u.label){case 0:if(!E||!i)return[2,false];if(M.current)return[2,false];r=Object.assign({dedupe:false},r);e=true;t=typeof c.CONCURRENT_PROMISES[E]!=="undefined"&&r.dedupe;u.label=1;case 1:u.trys.push([1,6,,7]);A({isValidating:true});n=void 0;a=void 0;if(!t)return[3,3];a=c.CONCURRENT_PROMISES_TS[E];return[4,c.CONCURRENT_PROMISES[E]];case 2:n=u.sent();return[3,5];case 3:if(c.CONCURRENT_PROMISES[E]){c.MUTATION_TS[E]=Date.now()-1}if(f.loadingTimeout&&!c.cacheGet(E)){setTimeout(function(){if(e)f.onLoadingSlow(E,f)},f.loadingTimeout)}if(y!==null){c.CONCURRENT_PROMISES[E]=i.apply(void 0,y)}else{c.CONCURRENT_PROMISES[E]=i(E)}c.CONCURRENT_PROMISES_TS[E]=a=Date.now();setTimeout(function(){delete c.CONCURRENT_PROMISES[E];delete c.CONCURRENT_PROMISES_TS[E]},f.dedupingInterval);return[4,c.CONCURRENT_PROMISES[E]];case 4:n=u.sent();f.onSuccess(n,E,f);u.label=5;case 5:if(c.MUTATION_TS[E]&&a<=c.MUTATION_TS[E]){A({isValidating:false});return[2,false]}c.cacheSet(E,n);c.cacheSet(T,undefined);N.current=E;l={isValidating:false};if(typeof P.current!=="undefined"){l.error=undefined;P.current=undefined}if(o.default(m.current,n)){}else{l.data=n;m.current=n}A(l);if(!t){C(E,n,undefined)}return[3,7];case 6:s=u.sent();delete c.CONCURRENT_PROMISES[E];delete c.CONCURRENT_PROMISES_TS[E];c.cacheSet(T,s);N.current=E;if(P.current!==s){P.current=s;A({isValidating:false,error:s});if(!t){C(E,undefined,s)}}f.onError(s,E,f);if(f.shouldRetryOnError){d=(r.retryCount||0)+1;f.onErrorRetry(s,E,f,U,Object.assign({dedupe:true},r,{retryCount:d}))}return[3,7];case 7:e=false;return[2,true]}})})},[E]);h(function(){if(!E)return undefined;M.current=false;var r=m.current;var t=c.cacheGet(E)||f.initialData;if(N.current!==E||!o.default(r,t)){A({data:t});m.current=t;N.current=E}var n=function(){return U({dedupe:true})};if(typeof t!=="undefined"&&!S&&window["requestIdleCallback"]){window["requestIdleCallback"](n)}else{n()}var i;if(f.revalidateOnFocus){i=p.default(n,f.focusThrottleInterval);if(!c.FOCUS_REVALIDATORS[E]){c.FOCUS_REVALIDATORS[E]=[i]}else{c.FOCUS_REVALIDATORS[E].push(i)}}var l=function(e,r,t,a){if(e===void 0){e=true}if(a===void 0){a=true}var u={};if(typeof r!=="undefined"&&!o.default(m.current,r)){u.data=r;m.current=r}if(P.current!==t){u.error=t;P.current=t}A(u);N.current=E;if(e){if(a){return n()}else{return U()}}return false};if(!c.CACHE_REVALIDATORS[E]){c.CACHE_REVALIDATORS[E]=[l]}else{c.CACHE_REVALIDATORS[E].push(l)}var s=null;if(f.refreshInterval){var R=function(){return a(e,void 0,void 0,function(){var e;return u(this,function(r){switch(r.label){case 0:if(!(!P.current&&(f.refreshWhenHidden||d.default())&&(!f.refreshWhenOffline&&v.default())))return[3,2];return[4,n()];case 1:r.sent();r.label=2;case 2:e=f.refreshInterval;s=setTimeout(R,e);return[2]}})})};s=setTimeout(R,f.refreshInterval)}var h=null;if(f.revalidateOnReconnect){h=addEventListener("online",n)}return function(){A=function(){return null};M.current=true;if(i&&c.FOCUS_REVALIDATORS[E]){var e=c.FOCUS_REVALIDATORS[E];var r=e.indexOf(i);if(r>=0){e[r]=e[e.length-1];e.pop()}}if(c.CACHE_REVALIDATORS[E]){var e=c.CACHE_REVALIDATORS[E];var r=e.indexOf(l);if(r>=0){e[r]=e[e.length-1];e.pop()}}if(s!==null){clearTimeout(s)}if(h!==null){removeEventListener("online",h)}}},[E,f.refreshInterval,U]);if(f.suspense){if(S)throw new Error("Suspense on server side is not yet supported!");var D=c.cacheGet(E);var L=c.cacheGet(T);if(typeof D==="undefined"&&typeof L==="undefined"){if(!c.CONCURRENT_PROMISES[E]){U()}if(c.CONCURRENT_PROMISES[E]&&typeof c.CONCURRENT_PROMISES[E].then==="function"){throw c.CONCURRENT_PROMISES[E]}D=c.CONCURRENT_PROMISES[E]}if(typeof D==="undefined"&&L){throw L}return{error:L,data:D,revalidate:U,isValidating:I.isValidating}}return{error:N.current===E?I.error:w,data:N.current===E?I.data:b,revalidate:U,isValidating:I.isValidating}}var T=R.default.Provider;r.SWRConfig=T;r.default=useSWR}});

@@ -10,3 +10,5 @@ import { Reducer } from 'react';

refreshWhenHidden?: boolean;
refreshWhenOffline?: boolean;
revalidateOnFocus?: boolean;
revalidateOnReconnect?: boolean;
shouldRetryOnError?: boolean;

@@ -13,0 +15,0 @@ fetcher?: Fn;

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

import { keyInterface, ConfigInterface, triggerInterface, mutateInterface, responseInterface, fetcherFn } from './types';
import { ConfigInterface, fetcherFn, keyInterface, mutateInterface, responseInterface, triggerInterface } from './types';
declare const trigger: triggerInterface;

@@ -3,0 +3,0 @@ declare const mutate: mutateInterface;

@@ -45,3 +45,5 @@ import isDocumentVisible from './libs/is-document-visible';

revalidateOnFocus: true,
revalidateOnReconnect: true,
refreshWhenHidden: false,
refreshWhenOffline: false,
shouldRetryOnError: true,

@@ -48,0 +50,0 @@ suspense: false

@@ -10,3 +10,5 @@ import { Reducer } from 'react';

refreshWhenHidden?: boolean;
refreshWhenOffline?: boolean;
revalidateOnFocus?: boolean;
revalidateOnReconnect?: boolean;
shouldRetryOnError?: boolean;

@@ -13,0 +15,0 @@ fetcher?: Fn;

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

import { keyInterface, ConfigInterface, triggerInterface, mutateInterface, responseInterface, fetcherFn } from './types';
import { ConfigInterface, fetcherFn, keyInterface, mutateInterface, responseInterface, triggerInterface } from './types';
declare const trigger: triggerInterface;

@@ -3,0 +3,0 @@ declare const mutate: mutateInterface;

@@ -1,8 +0,9 @@

import { useEffect, useLayoutEffect, useRef, useContext, useCallback, useReducer } from 'react';
import deepEqual from 'fast-deep-equal';
import defaultConfig, { CONCURRENT_PROMISES, CONCURRENT_PROMISES_TS, FOCUS_REVALIDATORS, CACHE_REVALIDATORS, MUTATION_TS, cacheGet, cacheSet } from './config';
import SWRConfigContext from './swr-config-context';
import { useCallback, useContext, useEffect, useLayoutEffect, useReducer, useRef } from 'react';
import defaultConfig, { cacheGet, cacheSet, CACHE_REVALIDATORS, CONCURRENT_PROMISES, CONCURRENT_PROMISES_TS, FOCUS_REVALIDATORS, MUTATION_TS } from './config';
import hash from './libs/hash';
import isDocumentVisible from './libs/is-document-visible';
import isOnline from './libs/is-online';
import throttle from './libs/throttle';
import hash from './libs/hash';
import SWRConfigContext from './swr-config-context';
const IS_SERVER = typeof window === 'undefined';

@@ -345,3 +346,4 @@ // React currently throws a warning when using useLayoutEffect on the server.

if (!errorRef.current &&
(config.refreshWhenHidden || isDocumentVisible())) {
(config.refreshWhenHidden || isDocumentVisible()) &&
(!config.refreshWhenOffline && isOnline())) {
// only revalidate when the page is visible

@@ -357,2 +359,7 @@ // if API request errored, we stop polling in this round

}
// set up reconnecting when the browser regains network connection
let reconnect = null;
if (config.revalidateOnReconnect) {
reconnect = addEventListener('online', softRevalidate);
}
return () => {

@@ -384,2 +391,5 @@ // cleanup

}
if (reconnect !== null) {
removeEventListener('online', reconnect);
}
};

@@ -386,0 +396,0 @@ }, [key, config.refreshInterval, revalidate]);

{
"name": "swr",
"version": "0.1.14",
"version": "0.1.15",
"description": "React Hooks library for remote data fetching",

@@ -5,0 +5,0 @@ "main": "./dist/index.js",

@@ -113,4 +113,6 @@ [![SWR](https://assets.zeit.co/image/upload/v1572289618/swr/banner.png)](https://swr.now.sh)

- `revalidateOnFocus = true`: auto revalidate when window gets focused
- `revalidateOnReconnect = true`: automatically revalidate when the browser regains a network connection (via `navigator.onLine`)
- `refreshInterval = 0`: polling interval (disabled by default)
- `refreshWhenHidden = false`: polling when the window is invisible (if `refreshInterval` is enabled)
- `refreshWhenOffline = false`: polling when the browser is offline (determined by `navigator.onLine`)
- `shouldRetryOnError = true`: retry when fetcher has an error [(details)](#error-retries)

@@ -117,0 +119,0 @@ - `dedupingInterval = 2000`: dedupe requests with the same key in this time span

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc