New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

swr

Package Overview
Dependencies
Maintainers
59
Versions
166
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.3.0-beta.8 to 0.3.0-beta.9

esm/cache.d.ts

0

dist/cache.d.ts

@@ -0,0 +0,0 @@ import { CacheInterface, keyInterface, cacheListener } from './types';

@@ -0,0 +0,0 @@ import { ConfigInterface } from './types';

@@ -0,0 +0,0 @@ export * from './use-swr';

3

dist/index.js

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

var e,t=require("react"),n=(e=require("fast-deep-equal"))&&"object"==typeof e&&"default"in e?e.default:e;function r(){return"undefined"==typeof document||void 0===document.visibilityState||"hidden"!==document.visibilityState}function i(){return void 0===navigator.onLine||navigator.onLine}function o(e,t){if(null==e)return{};var n,r,i={},o=Object.keys(e);for(r=0;r<o.length;r++)t.indexOf(n=o[r])>=0||(i[n]=e[n]);return i}function u(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}var a=new WeakMap,c=0,f=new(function(){function e(e){void 0===e&&(e={}),this.__cache=new Map(Object.entries(e)),this.__listeners=[]}var t=e.prototype;return t.get=function(e){var t=this.serializeKey(e);return this.__cache.get(t[0])},t.set=function(e,t,n){void 0===n&&(n=!0);var r=this.serializeKey(e);this.__cache.set(r[0],t),n&&R(e,t,!1),this.notify()},t.keys=function(){return Array.from(this.__cache.keys())},t.has=function(e){var t=this.serializeKey(e);return this.__cache.has(t[0])},t.clear=function(e){void 0===e&&(e=!0),e&&this.__cache.forEach(function(e){return R(e,null,!1)}),this.__cache.clear(),this.notify()},t.delete=function(e,t){void 0===t&&(t=!0);var n=this.serializeKey(e)[0];t&&R(e,null,!1),this.__cache.delete(n),this.notify()},t.serializeKey=function(e){var t=null;if("function"==typeof e)try{e=e()}catch(t){e=""}return Array.isArray(e)?(t=e,e=function(e){if(!e.length)return"";for(var t="arg",n=0;n<e.length;++n){var r=void 0;null===e[n]||"object"!=typeof e[n]?r="string"==typeof e[n]?'"'+e[n]+'"':String(e[n]):a.has(e[n])?r=a.get(e[n]):(r=c,a.set(e[n],c++)),t+="@"+r}return t}(e)):e=String(e||""),[e,t,e?"err@"+e:""]},t.subscribe=function(e){var t=this;if("function"!=typeof e)throw new Error("Expected the listener to be a function.");var n=!0;return this.__listeners.push(e),function(){if(n){n=!1;var r=t.__listeners.indexOf(e);r>-1&&(t.__listeners[r]=t.__listeners[t.__listeners.length-1],t.__listeners.length--)}}},t.notify=function(){for(var e,t=function(e,t){var n;if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(n=function(e,t){if(e){if("string"==typeof e)return u(e,void 0);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?u(e,void 0):void 0}}(e))){n&&(e=n);var r=0;return function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(n=e[Symbol.iterator]()).next.bind(n)}(this.__listeners);!(e=t()).done;)(0,e.value)()},e}()),s={},l={},v={},d={},h={},y={},g="undefined"!=typeof window&&navigator.connection&&-1!==["slow-2g","2g"].indexOf(navigator.connection.effectiveType),p={onLoadingSlow:function(){},onSuccess:function(){},onError:function(){},onErrorRetry:function(e,t,n,i,o){if(r()&&!(n.errorRetryCount&&o.retryCount>n.errorRetryCount)){var u=Math.min(o.retryCount||0,8),a=~~((Math.random()+.5)*(1<<u))*n.errorRetryInterval;setTimeout(i,a,o)}},errorRetryInterval:1e3*(g?10:5),focusThrottleInterval:5e3,dedupingInterval:2e3,loadingTimeout:1e3*(g?5:3),refreshInterval:0,revalidateOnFocus:!0,revalidateOnReconnect:!0,refreshWhenHidden:!1,refreshWhenOffline:!1,shouldRetryOnError:!0,suspense:!1,compare:n},m=!1;if("undefined"!=typeof window&&window.addEventListener&&!m){var b=function(){if(r()&&i())for(var e in v)v[e][0]&&v[e][0]()};window.addEventListener("visibilitychange",b,!1),window.addEventListener("focus",b,!1),m=!0}function w(e,t){var n=!1;return function(){n||(n=!0,e.apply(void 0,[].slice.call(arguments)),setTimeout(function(){return n=!1},t))}}var _=t.createContext({});function O(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}_.displayName="SWRConfigContext";var P="undefined"==typeof window,S=P?t.useEffect:t.useLayoutEffect,C=function(e,t){void 0===t&&(t=!0);var n=f.serializeKey(e),r=n[0],i=n[2];if(!r)return Promise.resolve();var o=d[r];if(r&&o){for(var u=f.get(r),a=f.get(i),c=[],s=0;s<o.length;++s)c.push(o[s](t,u,a,s>0));return Promise.all(c).then(function(){return f.get(r)})}return Promise.resolve(f.get(r))},E=function(e,t,n){var r=d[e];if(e&&r)for(var i=0;i<r.length;++i)r[i](!1,t,n)},R=function(e,t,n){void 0===n&&(n=!0);try{var r=function(){if(s!==h[o]||v!==l[o]){if(c)throw c;return a}void 0!==a&&f.set(o,a,!1),f.set(u,c,!1),y[o]=Date.now()-1;var e=d[o];if(e){for(var t=[],r=0;r<e.length;++r)t.push(e[r](!!n,a,c,r>0));return Promise.all(t).then(function(){return f.get(o)})}if(c)throw c;return a},i=f.serializeKey(e),o=i[0],u=i[2];if(!o)return Promise.resolve();if(void 0===t)return Promise.resolve(C(e,n));h[o]=Date.now()-1,y[o]=0;var a,c,s=h[o],v=l[o],g=function(){if(t&&"function"==typeof t){var e=O(function(){return Promise.resolve(t(f.get(o))).then(function(e){a=e})},function(e){c=e});if(e&&e.then)return e.then(function(){})}else{var n=function(){if(t&&"function"==typeof t.then){var e=O(function(){return Promise.resolve(t).then(function(e){a=e})},function(e){c=e});if(e&&e.then)return e.then(function(){})}else a=t}();if(n&&n.then)return n.then(function(){})}}();return Promise.resolve(g&&g.then?g.then(r):r())}catch(e){return Promise.reject(e)}};function j(){var e,n,o=[].slice.call(arguments),u={};o.length>=1&&(e=o[0]),o.length>2?(n=o[1],u=o[2]):"function"==typeof o[1]?n=o[1]:"object"==typeof o[1]&&(u=o[1]);var a=f.serializeKey(e),c=a[0],g=a[1],m=a[2];u=Object.assign({},p,t.useContext(_),u),void 0===n&&(n=u.fetcher);var b=f.get(c)||u.initialData,C=f.get(m),j=t.useRef({data:!1,error:!1,isValidating:!1}),x=t.useRef({data:b,error:C,isValidating:!1}),I=t.useState(null)[1],z=t.useCallback(function(e){var t=!1;for(var n in e)x.current[n]=e[n],j.current[n]&&(t=!0);(t||u.suspense)&&I({})},[]),k=t.useRef(!1),T=t.useRef(c),L=t.useRef({emit:function(e){var t;k.current||(t=u)[e].apply(t,[].slice.call(arguments,1))}}),A=t.useCallback(function(e,t){return R(c,e,t)},[c]),V=t.useCallback(function(e){void 0===e&&(e={});try{var t,r=function(e){return t?e:(i=!1,!0)};if(!c||!n)return Promise.resolve(!1);if(k.current)return Promise.resolve(!1);e=Object.assign({dedupe:!1},e);var i=!0,o=void 0!==s[c]&&e.dedupe,a=O(function(){function e(){if(l[c]>a||h[c]&&(a<=h[c]||a<=y[c]||0===y[c]))return z({isValidating:!1}),t=1,!1;f.set(c,r,!1),f.set(m,void 0,!1);var e={isValidating:!1};void 0!==x.current.error&&(e.error=void 0),u.compare(x.current.data,r)||(e.data=r),z(e),o||E(c,r,void 0)}var r,a;z({isValidating:!0});var v=o?(a=l[c],Promise.resolve(s[c]).then(function(e){r=e})):(u.loadingTimeout&&!f.get(c)&&setTimeout(function(){i&&L.current.emit("onLoadingSlow",c,u)},u.loadingTimeout),s[c]=null!==g?n.apply(void 0,g):n(c),l[c]=a=Date.now(),Promise.resolve(s[c]).then(function(e){r=e,setTimeout(function(){delete s[c],delete l[c]},u.dedupingInterval),L.current.emit("onSuccess",r,c,u)}));return v&&v.then?v.then(e):e()},function(t){delete s[c],delete l[c],f.set(m,t,!1),x.current.error!==t&&(z({isValidating:!1,error:t}),o||E(c,void 0,t)),L.current.emit("onError",t,c,u),u.shouldRetryOnError&&L.current.emit("onErrorRetry",t,c,u,V,Object.assign({dedupe:!0},e,{retryCount:(e.retryCount||0)+1}))});return Promise.resolve(a&&a.then?a.then(r):r(a))}catch(e){return Promise.reject(e)}},[c]);if(S(function(){if(c){k.current=!1;var e=x.current.data,t=f.get(c)||u.initialData;T.current===c&&u.compare(e,t)||(z({data:t}),T.current=c);var n,r=function(){return V({dedupe:!0})};(u.revalidateOnMount||!u.initialData&&void 0===u.revalidateOnMount)&&(void 0!==t&&!P&&window.requestIdleCallback?window.requestIdleCallback(r):r()),u.revalidateOnFocus&&(n=w(r,u.focusThrottleInterval),v[c]?v[c].push(n):v[c]=[n]);var i=function(e,t,n,i){void 0===e&&(e=!0),void 0===i&&(i=!0);var o={},a=!1;return void 0===t||u.compare(x.current.data,t)||(o.data=t,a=!0),x.current.error!==n&&(o.error=n,a=!0),a&&z(o),!!e&&(i?r():V())};d[c]?d[c].push(i):d[c]=[i];var o=null;return!P&&window.addEventListener&&u.revalidateOnReconnect&&window.addEventListener("online",o=r),function(){if(z=function(){return null},k.current=!0,n&&v[c]){var e=v[c],t=e.indexOf(n);t>=0&&(e[t]=e[e.length-1],e.pop())}if(d[c]){var r=d[c],u=r.indexOf(i);u>=0&&(r[u]=r[r.length-1],r.pop())}!P&&window.removeEventListener&&null!==o&&window.removeEventListener("online",o)}}},[c,V]),S(function(){var e=null;return u.refreshInterval&&(e=setTimeout(function t(){try{var n=function(){u.refreshInterval&&(e=setTimeout(t,u.refreshInterval))},o=function(){if(!x.current.error&&(u.refreshWhenHidden||r())&&(u.refreshWhenOffline||i()))return Promise.resolve(V({dedupe:!0})).then(function(){})}();return Promise.resolve(o&&o.then?o.then(n):n())}catch(e){return Promise.reject(e)}},u.refreshInterval)),function(){e&&clearTimeout(e)}},[u.refreshInterval,u.refreshWhenHidden,u.refreshWhenOffline,V]),u.suspense){var K=f.get(c)||b,W=f.get(m)||C;if(void 0===K&&void 0===W){if(s[c]||V(),s[c]&&"function"==typeof s[c].then)throw s[c];K=s[c]}if(void 0===K&&W)throw W;return{error:W,data:K,revalidate:V,mutate:A,isValidating:x.current.isValidating}}return t.useMemo(function(){var e={revalidate:V,mutate:A};return Object.defineProperties(e,{error:{get:function(){return j.current.error=!0,T.current===c?x.current.error:C},enumerable:!0},data:{get:function(){return j.current.data=!0,T.current===c?x.current.data:b},enumerable:!0},isValidating:{get:function(){return j.current.isValidating=!0,x.current.isValidating},enumerable:!0}}),e},[V])}var x=_.Provider;function I(e,t,n){if(!e.s){if(n instanceof z){if(!n.s)return void(n.o=I.bind(null,e,t));1&t&&(t=n.s),n=n.v}if(n&&n.then)return void n.then(I.bind(null,e,t),I.bind(null,e,2));e.s=t,e.v=n;var r=e.o;r&&r(e)}}var z=function(){function e(){}return e.prototype.then=function(t,n){var r=new e,i=this.s;if(i){var o=1&i?t:n;if(o){try{I(r,1,o(this.v))}catch(e){I(r,2,e)}return r}return this}return this.o=function(e){try{var i=e.v;1&e.s?I(r,1,t?t(i):i):n?I(r,1,n(i)):I(r,2,i)}catch(e){I(r,2,e)}},r},e}();function k(e){return e instanceof z&&1&e.s}function T(e,t,n){for(var r;;){var i=e();if(k(i)&&(i=i.v),!i)return o;if(i.then){r=0;break}var o=n();if(o&&o.then){if(!k(o)){r=1;break}o=o.s}if(t){var u=t();if(u&&u.then&&!k(u)){r=2;break}}}var a=new z,c=I.bind(null,a,2);return(0===r?i.then(s):1===r?o.then(f):u.then(l)).then(void 0,c),a;function f(r){o=r;do{if(t&&(u=t())&&u.then&&!k(u))return void u.then(l).then(void 0,c);if(!(i=e())||k(i)&&!i.v)return void I(a,1,o);if(i.then)return void i.then(s).then(void 0,c);k(o=n())&&(o=o.v)}while(!o||!o.then);o.then(f).then(void 0,c)}function s(e){e?(o=n())&&o.then?o.then(f).then(void 0,c):f(o):I(a,1,o)}function l(){(i=e())?i.then?i.then(s).then(void 0,c):s(i):I(a,1,o)}}exports.SWRConfig=x,exports.cache=f,exports.default=j,exports.mutate=R,exports.trigger=C,exports.useSWRInfinite=function(){var e,n,r=[].slice.call(arguments),i={};r.length>=1&&(e=r[0]),r.length>2?(n=r[1],i=r[2]):"function"==typeof r[1]?n=r[1]:"object"==typeof r[1]&&(i=r[1]);var u=i=Object.assign({},p,t.useContext(_),i),a=u.initialSize,c=void 0===a?1:a,s=u.revalidateAll,l=void 0!==s&&s,v=u.persistSize,d=void 0!==v&&v,h=u.fetcher,y=o(u,["initialSize","revalidateAll","persistSize","fetcher"]);void 0===n&&(n=h);var g=null;try{var m=f.serializeKey(e(0,null));g=m[0]}catch(e){}var b=t.useState(!1)[1],w=null;g&&(w="context@"+g);var O,P=null;g&&(O=f.get(P="size@"+g));var S=t.useRef(O||c);t.useEffect(function(){d||(S.current=c)},[g]);var C=j(g?["many",g]:null,function(){try{var t,r=function(){return f.delete(w),c},o=f.get(w)||{},u=o.originalData,a=o.force,c=[],s=null,v=0,d=T(function(){return!t&&v<S.current},function(){return++v},function(){function r(){c.push(y),s=y}var o=f.serializeKey(e(v,s)),d=o[0],h=o[1];if(d){var y=f.get(d),g=l||a||void 0===a&&0===v||u&&!i.compare(u[v],y)||void 0===y,p=function(){if(g){var e=function(){f.set(d,y,!1)},t=null!==h?Promise.resolve(n.apply(void 0,h)).then(function(e){y=e}):Promise.resolve(n(d)).then(function(e){y=e});return t&&t.then?t.then(e):e()}}();return p&&p.then?p.then(r):r()}t=1});return Promise.resolve(d&&d.then?d.then(r):r())}catch(e){return Promise.reject(e)}},y),E=C.mutate;return C.size=S.current,C.mutate=t.useCallback(function(e,t){return void 0===t&&(t=!0),t&&void 0!==e?f.set(w,{originalData:C.data,force:!1},!1):t&&f.set(w,{force:!0},!1),E(e,t)},[E,C.data,w]),C.setSize=t.useCallback(function(e){return"function"==typeof e?S.current=e(S.current):"number"==typeof e&&(S.current=e),f.set(P,S.current,!1),b(function(e){return!e}),C.mutate(function(e){return e})},[C.mutate,P]),C};
//# sourceMappingURL=index.js.map
module.exports=function(e,t){"use strict";var r={};function __webpack_require__(t){if(r[t]){return r[t].exports}var n=r[t]={i:t,l:false,exports:{}};e[t].call(n.exports,n,n.exports,__webpack_require__);n.l=true;return n.exports}__webpack_require__.ab=__dirname+"/";function startup(){return __webpack_require__(325)}return startup()}({202:function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:true});var r=new WeakMap;var n=0;function hash(e){if(!e.length)return"";var t="arg";for(var i=0;i<e.length;++i){var u=void 0;if(e[i]===null||typeof e[i]!=="object"){if(typeof e[i]==="string"){u='"'+e[i]+'"'}else{u=String(e[i])}}else{if(!r.has(e[i])){u=n;r.set(e[i],n++)}else{u=r.get(e[i])}}t+="@"+u}return t}t.default=hash},205:function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:true});function isOnline(){if(typeof navigator.onLine!=="undefined"){return navigator.onLine}return true}t.default=isOnline},209:function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:true});var n=r(297);var i=n.createContext({});i.displayName="SWRConfigContext";t.default=i},297:function(e){e.exports=require("react")},325:function(e,t,r){"use strict";function __export(e){for(var r in e)if(!t.hasOwnProperty(r))t[r]=e[r]}var n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:true});__export(r(516));var i=n(r(516));var u=r(942);t.useSWRInfinite=u.useSWRInfinite;var a=r(641);t.cache=a.cache;t.default=i.default},459:function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:true});function isDocumentVisible(){if(typeof document!=="undefined"&&typeof document.visibilityState!=="undefined"){return document.visibilityState!=="hidden"}return true}t.default=isDocumentVisible},516:function(e,t,r){"use strict";var n=this&&this.__awaiter||function(e,t,r,n){function adopt(e){return e instanceof r?e:new r(function(t){t(e)})}return new(r||(r=Promise))(function(r,i){function fulfilled(e){try{step(n.next(e))}catch(e){i(e)}}function rejected(e){try{step(n["throw"](e))}catch(e){i(e)}}function step(e){e.done?r(e.value):adopt(e.value).then(fulfilled,rejected)}step((n=n.apply(e,t||[])).next())})};var i=this&&this.__generator||function(e,t){var r={label:0,sent:function(){if(u[0]&1)throw u[1];return u[1]},trys:[],ops:[]},n,i,u,a;return a={next:verb(0),throw:verb(1),return:verb(2)},typeof Symbol==="function"&&(a[Symbol.iterator]=function(){return this}),a;function verb(e){return function(t){return step([e,t])}}function step(a){if(n)throw new TypeError("Generator is already executing.");while(r)try{if(n=1,i&&(u=a[0]&2?i["return"]:a[0]?i["throw"]||((u=i["return"])&&u.call(i),0):i.next)&&!(u=u.call(i,a[1])).done)return u;if(i=0,u)a=[a[0]&2,u.value];switch(a[0]){case 0:case 1:u=a;break;case 4:r.label++;return{value:a[1],done:false};case 5:r.label++;i=a[1];a=[0];continue;case 7:a=r.ops.pop();r.trys.pop();continue;default:if(!(u=r.trys,u=u.length>0&&u[u.length-1])&&(a[0]===6||a[0]===2)){r=0;continue}if(a[0]===3&&(!u||a[1]>u[0]&&a[1]<u[3])){r.label=a[1];break}if(a[0]===6&&r.label<u[1]){r.label=u[1];u=a;break}if(u&&r.label<u[2]){r.label=u[2];r.ops.push(a);break}if(u[2])r.ops.pop();r.trys.pop();continue}a=t.call(e,r)}catch(e){a=[6,e];i=0}finally{n=u=0}if(a[0]&5)throw a[1];return{value:a[0]?a[1]:void 0,done:true}}};var u=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(e!=null)for(var r in e)if(Object.hasOwnProperty.call(e,r))t[r]=e[r];t["default"]=e;return t};var a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:true});var f=r(297);var o=u(r(641));var l=a(r(459));var c=a(r(205));var s=a(r(542));var d=a(r(209));var v=typeof window==="undefined";var h=v?f.useEffect:f.useLayoutEffect;var p=function(e,t){if(t===void 0){t=true}var r=o.cache.serializeKey(e),n=r[0],i=r[2];if(!n)return Promise.resolve();var u=o.CACHE_REVALIDATORS[n];if(n&&u){var a=o.cache.get(n);var f=o.cache.get(i);var l=[];for(var c=0;c<u.length;++c){l.push(u[c](t,a,f,c>0))}return Promise.all(l).then(function(){return o.cache.get(n)})}return Promise.resolve(o.cache.get(n))};t.trigger=p;var _=function(e,t,r){var n=o.CACHE_REVALIDATORS[e];if(e&&n){for(var i=0;i<n.length;++i){n[i](false,t,r)}}};var y=function(e,t,r){if(r===void 0){r=true}return n(void 0,void 0,void 0,function(){var n,u,a,f,l,c,s,d,v,h,_,y;return i(this,function(i){switch(i.label){case 0:n=o.cache.serializeKey(e),u=n[0],a=n[2];if(!u)return[2];if(typeof t==="undefined")return[2,p(e,r)];o.MUTATION_TS[u]=Date.now()-1;o.MUTATION_END_TS[u]=0;f=o.MUTATION_TS[u];l=o.CONCURRENT_PROMISES_TS[u];if(!(t&&typeof t==="function"))return[3,5];i.label=1;case 1:i.trys.push([1,3,,4]);return[4,t(o.cache.get(u))];case 2:c=i.sent();return[3,4];case 3:d=i.sent();s=d;return[3,4];case 4:return[3,11];case 5:if(!(t&&typeof t.then==="function"))return[3,10];i.label=6;case 6:i.trys.push([6,8,,9]);return[4,t];case 7:c=i.sent();return[3,9];case 8:v=i.sent();s=v;return[3,9];case 9:return[3,11];case 10:c=t;i.label=11;case 11:if(f!==o.MUTATION_TS[u]||l!==o.CONCURRENT_PROMISES_TS[u]){if(s)throw s;return[2,c]}if(typeof c!=="undefined"){o.cache.set(u,c,false)}o.cache.set(a,s,false);o.MUTATION_END_TS[u]=Date.now()-1;h=o.CACHE_REVALIDATORS[u];if(h){_=[];for(y=0;y<h.length;++y){_.push(h[y](!!r,c,s,y>0))}return[2,Promise.all(_).then(function(){return o.cache.get(u)})]}if(s)throw s;return[2,c]}})})};t.mutate=y;function useSWR(){var e=this;var t=[];for(var r=0;r<arguments.length;r++){t[r]=arguments[r]}var u,a,p={};if(t.length>=1){u=t[0]}if(t.length>2){a=t[1];p=t[2]}else{if(typeof t[1]==="function"){a=t[1]}else if(typeof t[1]==="object"){p=t[1]}}var O=o.cache.serializeKey(u),S=O[0],R=O[1],b=O[2];p=Object.assign({},o.default,f.useContext(d.default),p);if(typeof a==="undefined"){a=p.fetcher}var E=o.cache.get(S)||p.initialData;var T=o.cache.get(b);var C=f.useRef({data:false,error:false,isValidating:false});var w=f.useRef({data:E,error:T,isValidating:false});var m=f.useState(null)[1];var I=f.useCallback(function(e){var t=false;for(var r in e){w.current[r]=e[r];if(C.current[r]){t=true}}if(t||p.suspense){m({})}},[]);var N=f.useRef(false);var M=f.useRef(S);var A=f.useRef({emit:function(e){var t=[];for(var r=1;r<arguments.length;r++){t[r-1]=arguments[r]}if(N.current)return;p[e].apply(p,t)}});var g=f.useCallback(function(e,t){return y(S,e,t)},[S]);var P=f.useCallback(function(t){if(t===void 0){t={}}return n(e,void 0,void 0,function(){var e,r,n,u,f,l,c,s;return i(this,function(i){switch(i.label){case 0:if(!S||!a)return[2,false];if(N.current)return[2,false];t=Object.assign({dedupe:false},t);e=true;r=typeof o.CONCURRENT_PROMISES[S]!=="undefined"&&t.dedupe;i.label=1;case 1:i.trys.push([1,6,,7]);I({isValidating:true});n=void 0;u=void 0;if(!r)return[3,3];u=o.CONCURRENT_PROMISES_TS[S];return[4,o.CONCURRENT_PROMISES[S]];case 2:n=i.sent();return[3,5];case 3:if(p.loadingTimeout&&!o.cache.get(S)){setTimeout(function(){if(e)A.current.emit("onLoadingSlow",S,p)},p.loadingTimeout)}if(R!==null){o.CONCURRENT_PROMISES[S]=a.apply(void 0,R)}else{o.CONCURRENT_PROMISES[S]=a(S)}o.CONCURRENT_PROMISES_TS[S]=u=Date.now();return[4,o.CONCURRENT_PROMISES[S]];case 4:n=i.sent();setTimeout(function(){delete o.CONCURRENT_PROMISES[S];delete o.CONCURRENT_PROMISES_TS[S]},p.dedupingInterval);A.current.emit("onSuccess",n,S,p);i.label=5;case 5:f=o.CONCURRENT_PROMISES_TS[S]>u||o.MUTATION_TS[S]&&(u<=o.MUTATION_TS[S]||u<=o.MUTATION_END_TS[S]||o.MUTATION_END_TS[S]===0);if(f){I({isValidating:false});return[2,false]}o.cache.set(S,n,false);o.cache.set(b,undefined,false);l={isValidating:false};if(typeof w.current.error!=="undefined"){l.error=undefined}if(!p.compare(w.current.data,n)){l.data=n}I(l);if(!r){_(S,n,undefined)}return[3,7];case 6:c=i.sent();delete o.CONCURRENT_PROMISES[S];delete o.CONCURRENT_PROMISES_TS[S];o.cache.set(b,c,false);if(w.current.error!==c){I({isValidating:false,error:c});if(!r){_(S,undefined,c)}}A.current.emit("onError",c,S,p);if(p.shouldRetryOnError){s=(t.retryCount||0)+1;A.current.emit("onErrorRetry",c,S,p,P,Object.assign({dedupe:true},t,{retryCount:s}))}return[3,7];case 7:e=false;return[2,true]}})})},[S]);h(function(){if(!S)return undefined;N.current=false;var e=w.current.data;var t=o.cache.get(S)||p.initialData;if(M.current!==S||!p.compare(e,t)){I({data:t});M.current=S}var r=function(){return P({dedupe:true})};if(p.revalidateOnMount||!p.initialData&&p.revalidateOnMount===undefined){if(typeof t!=="undefined"&&!v&&window["requestIdleCallback"]){window["requestIdleCallback"](r)}else{r()}}var n;if(p.revalidateOnFocus){n=s.default(r,p.focusThrottleInterval);if(!o.FOCUS_REVALIDATORS[S]){o.FOCUS_REVALIDATORS[S]=[n]}else{o.FOCUS_REVALIDATORS[S].push(n)}}var i=function(e,t,n,i){if(e===void 0){e=true}if(i===void 0){i=true}var u={};var a=false;if(typeof t!=="undefined"&&!p.compare(w.current.data,t)){u.data=t;a=true}if(w.current.error!==n){u.error=n;a=true}if(a){I(u)}if(e){if(i){return r()}else{return P()}}return false};if(!o.CACHE_REVALIDATORS[S]){o.CACHE_REVALIDATORS[S]=[i]}else{o.CACHE_REVALIDATORS[S].push(i)}var u=null;if(!v&&window.addEventListener&&p.revalidateOnReconnect){window.addEventListener("online",u=r)}return function(){I=function(){return null};N.current=true;if(n&&o.FOCUS_REVALIDATORS[S]){var e=o.FOCUS_REVALIDATORS[S];var t=e.indexOf(n);if(t>=0){e[t]=e[e.length-1];e.pop()}}if(o.CACHE_REVALIDATORS[S]){var e=o.CACHE_REVALIDATORS[S];var t=e.indexOf(i);if(t>=0){e[t]=e[e.length-1];e.pop()}}if(!v&&window.removeEventListener&&u!==null){window.removeEventListener("online",u)}}},[S,P]);h(function(){var t=null;var r=function(){return n(e,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:if(!(!w.current.error&&(p.refreshWhenHidden||l.default())&&(p.refreshWhenOffline||c.default())))return[3,2];return[4,P({dedupe:true})];case 1:e.sent();e.label=2;case 2:if(p.refreshInterval){t=setTimeout(r,p.refreshInterval)}return[2]}})})};if(p.refreshInterval){t=setTimeout(r,p.refreshInterval)}return function(){if(t)clearTimeout(t)}},[p.refreshInterval,p.refreshWhenHidden,p.refreshWhenOffline,P]);if(p.suspense){var U=o.cache.get(S)||E;var j=o.cache.get(b)||T;if(typeof U==="undefined"&&typeof j==="undefined"){if(!o.CONCURRENT_PROMISES[S]){P()}if(o.CONCURRENT_PROMISES[S]&&typeof o.CONCURRENT_PROMISES[S].then==="function"){throw o.CONCURRENT_PROMISES[S]}U=o.CONCURRENT_PROMISES[S]}if(typeof U==="undefined"&&j){throw j}return{error:j,data:U,revalidate:P,mutate:g,isValidating:w.current.isValidating}}return f.useMemo(function(){var e={revalidate:P,mutate:g};Object.defineProperties(e,{error:{get:function(){C.current.error=true;return M.current===S?w.current.error:T},enumerable:true},data:{get:function(){C.current.data=true;return M.current===S?w.current.data:E},enumerable:true},isValidating:{get:function(){C.current.isValidating=true;return w.current.isValidating},enumerable:true}});return e},[P])}var O=d.default.Provider;t.SWRConfig=O;t.default=useSWR},542:function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:true});function throttle(e,t){var r=false;return function(){var n=[];for(var i=0;i<arguments.length;i++){n[i]=arguments[i]}if(r)return;r=true;e.apply(void 0,n);setTimeout(function(){return r=false},t)}}t.default=throttle},641:function(e,t,r){"use strict";var n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:true});var i=n(r(832));var u=n(r(459));var a=n(r(205));var f=n(r(722));var o=new f.default;t.cache=o;var l={};t.CONCURRENT_PROMISES=l;var c={};t.CONCURRENT_PROMISES_TS=c;var s={};t.FOCUS_REVALIDATORS=s;var d={};t.CACHE_REVALIDATORS=d;var v={};t.MUTATION_TS=v;var h={};t.MUTATION_END_TS=h;function onErrorRetry(e,t,r,n,i){if(!u.default()){return}if(r.errorRetryCount&&i.retryCount>r.errorRetryCount){return}var a=Math.min(i.retryCount||0,8);var f=~~((Math.random()+.5)*(1<<a))*r.errorRetryInterval;setTimeout(n,f,i)}var p=typeof window!=="undefined"&&navigator["connection"]&&["slow-2g","2g"].indexOf(navigator["connection"].effectiveType)!==-1;var _={onLoadingSlow:function(){},onSuccess:function(){},onError:function(){},onErrorRetry:onErrorRetry,errorRetryInterval:(p?10:5)*1e3,focusThrottleInterval:5*1e3,dedupingInterval:2*1e3,loadingTimeout:(p?5:3)*1e3,refreshInterval:0,revalidateOnFocus:true,revalidateOnReconnect:true,refreshWhenHidden:false,refreshWhenOffline:false,shouldRetryOnError:true,suspense:false,compare:i.default};var y=false;if(typeof window!=="undefined"&&window.addEventListener&&!y){var O=function(){if(!u.default()||!a.default())return;for(var e in s){if(s[e][0])s[e][0]()}};window.addEventListener("visibilitychange",O,false);window.addEventListener("focus",O,false);y=true}t.default=_},722:function(e,t,r){"use strict";var n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:true});var i=r(516);var u=n(r(202));var a=function(){function Cache(e){if(e===void 0){e={}}this.__cache=new Map(Object.entries(e));this.__listeners=[]}Cache.prototype.get=function(e){var t=this.serializeKey(e)[0];return this.__cache.get(t)};Cache.prototype.set=function(e,t,r){if(r===void 0){r=true}var n=this.serializeKey(e)[0];this.__cache.set(n,t);if(r)i.mutate(e,t,false);this.notify()};Cache.prototype.keys=function(){return Array.from(this.__cache.keys())};Cache.prototype.has=function(e){var t=this.serializeKey(e)[0];return this.__cache.has(t)};Cache.prototype.clear=function(e){if(e===void 0){e=true}if(e)this.__cache.forEach(function(e){return i.mutate(e,null,false)});this.__cache.clear();this.notify()};Cache.prototype.delete=function(e,t){if(t===void 0){t=true}var r=this.serializeKey(e)[0];if(t)i.mutate(e,null,false);this.__cache.delete(r);this.notify()};Cache.prototype.serializeKey=function(e){var t=null;if(typeof e==="function"){try{e=e()}catch(t){e=""}}if(Array.isArray(e)){t=e;e=u.default(e)}else{e=String(e||"")}var r=e?"err@"+e:"";return[e,t,r]};Cache.prototype.subscribe=function(e){var t=this;if(typeof e!=="function"){throw new Error("Expected the listener to be a function.")}var r=true;this.__listeners.push(e);return function(){if(!r)return;r=false;var n=t.__listeners.indexOf(e);if(n>-1){t.__listeners[n]=t.__listeners[t.__listeners.length-1];t.__listeners.length--}}};Cache.prototype.notify=function(){for(var e=0,t=this.__listeners;e<t.length;e++){var r=t[e];r()}};return Cache}();t.default=a},832:function(e){"use strict";var t=Array.isArray;var r=Object.keys;var n=Object.prototype.hasOwnProperty;e.exports=function equal(e,i){if(e===i)return true;if(e&&i&&typeof e=="object"&&typeof i=="object"){var u=t(e),a=t(i),f,o,l;if(u&&a){o=e.length;if(o!=i.length)return false;for(f=o;f--!==0;)if(!equal(e[f],i[f]))return false;return true}if(u!=a)return false;var c=e instanceof Date,s=i instanceof Date;if(c!=s)return false;if(c&&s)return e.getTime()==i.getTime();var d=e instanceof RegExp,v=i instanceof RegExp;if(d!=v)return false;if(d&&v)return e.toString()==i.toString();var h=r(e);o=h.length;if(o!==r(i).length)return false;for(f=o;f--!==0;)if(!n.call(i,h[f]))return false;for(f=o;f--!==0;){l=h[f];if(!equal(e[l],i[l]))return false}return true}return e!==e&&i!==i}},942:function(e,t,r){"use strict";var n=this&&this.__awaiter||function(e,t,r,n){function adopt(e){return e instanceof r?e:new r(function(t){t(e)})}return new(r||(r=Promise))(function(r,i){function fulfilled(e){try{step(n.next(e))}catch(e){i(e)}}function rejected(e){try{step(n["throw"](e))}catch(e){i(e)}}function step(e){e.done?r(e.value):adopt(e.value).then(fulfilled,rejected)}step((n=n.apply(e,t||[])).next())})};var i=this&&this.__generator||function(e,t){var r={label:0,sent:function(){if(u[0]&1)throw u[1];return u[1]},trys:[],ops:[]},n,i,u,a;return a={next:verb(0),throw:verb(1),return:verb(2)},typeof Symbol==="function"&&(a[Symbol.iterator]=function(){return this}),a;function verb(e){return function(t){return step([e,t])}}function step(a){if(n)throw new TypeError("Generator is already executing.");while(r)try{if(n=1,i&&(u=a[0]&2?i["return"]:a[0]?i["throw"]||((u=i["return"])&&u.call(i),0):i.next)&&!(u=u.call(i,a[1])).done)return u;if(i=0,u)a=[a[0]&2,u.value];switch(a[0]){case 0:case 1:u=a;break;case 4:r.label++;return{value:a[1],done:false};case 5:r.label++;i=a[1];a=[0];continue;case 7:a=r.ops.pop();r.trys.pop();continue;default:if(!(u=r.trys,u=u.length>0&&u[u.length-1])&&(a[0]===6||a[0]===2)){r=0;continue}if(a[0]===3&&(!u||a[1]>u[0]&&a[1]<u[3])){r.label=a[1];break}if(a[0]===6&&r.label<u[1]){r.label=u[1];u=a;break}if(u&&r.label<u[2]){r.label=u[2];r.ops.push(a);break}if(u[2])r.ops.pop();r.trys.pop();continue}a=t.call(e,r)}catch(e){a=[6,e];i=0}finally{n=u=0}if(a[0]&5)throw a[1];return{value:a[0]?a[1]:void 0,done:true}}};var u=this&&this.__rest||function(e,t){var r={};for(var n in e)if(Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0)r[n]=e[n];if(e!=null&&typeof Object.getOwnPropertySymbols==="function")for(var i=0,n=Object.getOwnPropertySymbols(e);i<n.length;i++){if(t.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(e,n[i]))r[n[i]]=e[n[i]]}return r};var a=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(e!=null)for(var r in e)if(Object.hasOwnProperty.call(e,r))t[r]=e[r];t["default"]=e;return t};var f=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:true});var o=r(297);var l=a(r(641));var c=f(r(209));var s=f(r(516));function useSWRInfinite(){var e=this;var t=[];for(var r=0;r<arguments.length;r++){t[r]=arguments[r]}var a,f,d={};if(t.length>=1){a=t[0]}if(t.length>2){f=t[1];d=t[2]}else{if(typeof t[1]==="function"){f=t[1]}else if(typeof t[1]==="object"){d=t[1]}}d=Object.assign({},l.default,o.useContext(c.default),d);var v=d.initialSize,h=v===void 0?1:v,p=d.revalidateAll,_=p===void 0?false:p,y=d.persistSize,O=y===void 0?false:y,S=d.fetcher,R=u(d,["initialSize","revalidateAll","persistSize","fetcher"]);if(typeof f==="undefined"){f=S}var b=null;try{b=l.cache.serializeKey(a(0,null))[0]}catch(e){}var E=o.useState(false)[1];var T=null;if(b){T="context@"+b}var C=null;var w;if(b){C="size@"+b;w=l.cache.get(C)}var m=o.useRef(w||h);o.useEffect(function(){if(!O){m.current=h}},[b]);var I=s.default(b?["many",b]:null,function(){return n(e,void 0,void 0,function(){var e,t,r,n,u,o,c,s,v,h,p;return i(this,function(i){switch(i.label){case 0:e=l.cache.get(T)||{},t=e.originalData,r=e.force;n=[];u=null;o=0;i.label=1;case 1:if(!(o<m.current))return[3,8];c=l.cache.serializeKey(a(o,u)),s=c[0],v=c[1];if(!s){return[3,8]}h=l.cache.get(s);p=_||r||typeof r==="undefined"&&o===0||t&&!d.compare(t[o],h)||typeof h==="undefined";if(!p)return[3,6];if(!(v!==null))return[3,3];return[4,f.apply(void 0,v)];case 2:h=i.sent();return[3,5];case 3:return[4,f(s)];case 4:h=i.sent();i.label=5;case 5:l.cache.set(s,h,false);i.label=6;case 6:n.push(h);u=h;i.label=7;case 7:++o;return[3,1];case 8:l.cache.delete(T);return[2,n]}})})},R);var N=I.mutate;I.size=m.current;I.mutate=o.useCallback(function(e,t){if(t===void 0){t=true}if(t&&typeof e!=="undefined"){var r=I.data;l.cache.set(T,{originalData:r,force:false},false)}else if(t){l.cache.set(T,{force:true},false)}return N(e,t)},[N,I.data,T]);I.setSize=o.useCallback(function(e){if(typeof e==="function"){m.current=e(m.current)}else if(typeof e==="number"){m.current=e}l.cache.set(C,m.current,false);E(function(e){return!e});return I.mutate(function(e){return e})},[I.mutate,C]);return I}t.useSWRInfinite=useSWRInfinite}});
export default function hash(args: any[]): string;
export default function isDocumentVisible(): boolean;
export default function isOnline(): boolean;
export default function throttle(fn: any, interval: number): (...args: any[]) => void;
import { ConfigInterface } from './types';
declare const SWRConfigContext: import("react").Context<ConfigInterface<any, any, import("./types").fetcherFn<any>>>;
export default SWRConfigContext;

@@ -0,0 +0,0 @@ export declare type fetcherFn<Data> = (...args: any) => Data | Promise<Data>;

@@ -0,0 +0,0 @@ import { keyType, fetcherFn, ConfigInterface, responseInterface } from './types';

@@ -7,4 +7,4 @@ import { ConfigInterface, fetcherFn, keyInterface, mutateInterface, responseInterface, triggerInterface } from './types';

declare function useSWR<Data = any, Error = any>(key: keyInterface, fn?: fetcherFn<Data>, config?: ConfigInterface<Data, Error>): responseInterface<Data, Error>;
declare const SWRConfig: import("react").Provider<ConfigInterface<any, any, fetcherFn<any>>>;
declare const SWRConfig: import("react").ProviderExoticComponent<import("react").ProviderProps<ConfigInterface<any, any, fetcherFn<any>>>>;
export { trigger, mutate, SWRConfig };
export default useSWR;
{
"name": "swr",
"version": "0.3.0-beta.8",
"version": "0.3.0-beta.9",
"description": "React Hooks library for remote data fetching",
"source": "src/index.ts",
"main": "dist/index.js",
"module": "dist/index.module.js",
"umd:main": "dist/index.umd.js",
"unpkg": "dist/index.umd.js",
"react-native": "dist/index.module.js",
"types": "dist/index.d.ts",
"main": "./dist/index.js",
"module": "./esm/index.js",
"react-native": "./esm/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist/**"
"dist/**",
"esm/**"
],

@@ -20,3 +18,5 @@ "repository": "vercel/swr",

"dev": "vercel dev",
"build": "microbundle",
"build": "npm run build:esm && npm run build:cjs",
"build:cjs": "ncc build src/index.ts -o dist -m -e react",
"build:esm": "tsc --module ES6 --outDir esm",
"watch": "tsc --watch",

@@ -48,2 +48,3 @@ "types:check": "tsc --noEmit",

"@typescript-eslint/parser": "2.5.0",
"@zeit/ncc": "0.20.5",
"eslint": "6.6.0",

@@ -54,3 +55,2 @@ "eslint-config-prettier": "6.5.0",

"lint-staged": "8.2.1",
"microbundle": "0.12.2",
"prettier": "1.18.2",

@@ -57,0 +57,0 @@ "react": "16.11.0",

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