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

swrev

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swrev - npm Package Compare versions

Comparing version 4.0.0-next.6 to 4.0.0

dist/swrev.mjs

2

dist/events.d.ts
import { SWRKey } from './key';
export declare type SWRListener = (payload: any) => void;
export type SWRListener = (payload: any) => void;
export interface SWREventManager {

@@ -4,0 +4,0 @@ subscribe(key: SWRKey, listener: SWRListener): void;

/**
* Determines the type of the SWR keys.
*/
export declare type SWRKey = string;
export type SWRKey = string;

@@ -19,7 +19,7 @@ import { SWRCache, CacheClearOptions } from './cache';

*/
export declare type SWRFetcher<D = any> = (...props: any[]) => Promise<D> | D;
export type SWRFetcher<D = any> = (...props: any[]) => Promise<D> | D;
/**
* Determines the revalidation function to use.
*/
export declare type SWRRevalidateFunction<D = any> = (key?: string | undefined, options?: Partial<SWRRevalidateOptions<D>> | undefined) => Promise<D>;
export type SWRRevalidateFunction<D = any> = (key?: string | undefined, options?: Partial<SWRRevalidateOptions<D>> | undefined) => Promise<D>;
/**

@@ -26,0 +26,0 @@ * Determines the options available for the SWR configuration.

@@ -8,7 +8,7 @@ import { SWRCache, CacheClearOptions } from './cache';

*/
export declare type SWRFunctionStateValue<D> = (state: D | undefined) => D;
export type SWRFunctionStateValue<D> = (state: D | undefined) => D;
/**
* Determines how a SWR mutate value looks like.
*/
export declare type SWRMutateValue<D> = D | SWRFunctionStateValue<D>;
export type SWRMutateValue<D> = D | SWRFunctionStateValue<D>;
/**

@@ -15,0 +15,0 @@ * Stale While Revalidate

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

var B=Object.defineProperty,G=Object.defineProperties;var J=Object.getOwnPropertyDescriptors;var L=Object.getOwnPropertySymbols;var N=Object.prototype.hasOwnProperty,Q=Object.prototype.propertyIsEnumerable;var W=(i,r,a)=>r in i?B(i,r,{enumerable:!0,configurable:!0,writable:!0,value:a}):i[r]=a,n=(i,r)=>{for(var a in r||(r={}))N.call(r,a)&&W(i,a,r[a]);if(L)for(var a of L(r))Q.call(r,a)&&W(i,a,r[a]);return i},A=(i,r)=>G(i,J(r));var f=(i,r,a)=>(W(i,typeof r!="symbol"?r+"":r,a),a);(function(i,r){typeof exports=="object"&&typeof module!="undefined"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(i=typeof globalThis!="undefined"?globalThis:i||self,r(i.swrev={}))})(this,function(i){"use strict";class r{constructor(){f(this,"listeners",new Map)}subscribe(e,t){this.listeners.has(e)||this.listeners.set(e,[]),!this.listeners.get(e).includes(t)&&this.listeners.get(e).push(t)}unsubscribe(e,t){!this.listeners.has(e)||!this.listeners.get(e).includes(t)||(this.listeners.get(e).splice(this.listeners.get(e).indexOf(t),1),this.listeners.get(e).length===0&&this.listeners.delete(e))}emit(e,t){!this.listeners.has(e)||this.listeners.get(e).forEach(s=>s(t))}}const a={broadcast:!1},P={broadcast:!1};class w{constructor({data:e,expiresAt:t=null}){f(this,"data");f(this,"expiresAt");this.data=e,this.expiresAt=t}isResolving(){return this.data instanceof Promise}hasExpired(){return this.expiresAt===null||this.expiresAt<new Date}expiresIn(e){return this.expiresAt=new Date,this.expiresAt.setMilliseconds(this.expiresAt.getMilliseconds()+e),this}}class D{constructor(){f(this,"elements",new Map);f(this,"event",new r)}resolve(e,t){Promise.resolve(t.data).then(s=>{if(s==null)return this.remove(e);t.data=s,this.broadcast(e,s)})}get(e){return this.elements.get(e)}set(e,t){this.elements.set(e,t),this.resolve(e,t)}remove(e,t){const{broadcast:s}=n(n({},a),t);s&&this.broadcast(e,void 0),this.elements.delete(e)}clear(e){const{broadcast:t}=n(n({},P),e);if(t)for(const s of this.elements.keys())this.broadcast(s,void 0);this.elements.clear()}has(e){return this.elements.has(e)}subscribe(e,t){this.event.subscribe(e,t)}unsubscribe(e,t){this.event.unsubscribe(e,t)}broadcast(e,t){this.event.emit(e,t)}}const g={cache:new D,errors:new r,fetcher:async u=>{const e=await fetch(u);if(!e.ok)throw Error("Not a 2XX response.");return e.json()},fallbackData:void 0,loadInitialCache:!0,revalidateOnStart:!0,dedupingInterval:2e3,revalidateOnFocus:!0,focusThrottleInterval:5e3,revalidateOnReconnect:!0,reconnectWhen:(u,{enabled:e})=>e&&typeof window!="undefined"?(window.addEventListener("online",u),()=>window.removeEventListener("online",u)):()=>{},focusWhen:(u,{enabled:e,throttleInterval:t})=>{if(e&&typeof window!="undefined"){let s=null;const o=()=>{const l=Date.now();(s===null||l-s>t)&&(s=l,u())};return window.addEventListener("focus",o),()=>window.removeEventListener("focus",o)}return()=>{}},revalidateFunction:void 0},m=A(n({},g),{force:!1}),K={revalidate:!0,revalidateOptions:n({},m),revalidateFunction:void 0},S={broadcast:!1};class j{constructor(e){f(this,"options");this.options=n(n({},g),e)}get cache(){return this.options.cache}get errors(){return this.options.errors}async requestData(e,t){return await Promise.resolve(t(e)).catch(s=>{throw this.errors.emit(e,s),s})}resolveKey(e){if(typeof e=="function")try{return e()}catch{return}return e}clear(e,t){const s=n(n({},S),t);if(e==null)return this.cache.clear(s);if(!Array.isArray(e))return this.cache.remove(e,s);for(const o of e)this.cache.remove(o,s)}async revalidate(e,t){if(!e)throw new Error("[Revalidate] Key issue: ${key}");const{fetcher:s,dedupingInterval:o}=this.options,{force:l,fetcher:h,dedupingInterval:c}=n(A(n({},m),{fetcher:s,dedupingInterval:o}),t);if(l||!this.cache.has(e)||this.cache.has(e)&&this.cache.get(e).hasExpired()){const d=this.requestData(e,h),v=d.catch(()=>{});return this.cache.set(e,new w({data:v}).expiresIn(c)),await d}return this.getWait(e)}async mutate(e,t,s){var d;if(!e)throw new Error("[Mutate] Key issue: ${key}");const{revalidate:o,revalidateOptions:l,revalidateFunction:h}=n(n({},K),s);let c;if(typeof t=="function"){let v;if(this.cache.has(e)){const b=this.cache.get(e);b.isResolving()||(v=b.data)}c=t(v)}else c=t;return this.cache.set(e,new w({data:c})),o?await((d=h==null?void 0:h(e,l))!=null?d:this.revalidate(e,l)):c}subscribeData(e,t){if(e){const s=o=>t(o);return this.cache.subscribe(e,s),()=>this.cache.unsubscribe(e,s)}return()=>{}}subscribeErrors(e,t){if(e){const s=o=>t(o);return this.errors.subscribe(e,s),()=>this.errors.unsubscribe(e,s)}return()=>{}}get(e){if(e&&this.cache.has(e)){const t=this.cache.get(e);if(!t.isResolving())return t.data}}getWait(e){return new Promise((t,s)=>{const o=this.subscribeData(e,c=>{if(o(),c!==void 0)return t(c)}),l=this.subscribeErrors(e,c=>{if(l(),c!==void 0)return s(c)}),h=this.get(e);if(h!==void 0)return t(h)})}subscribe(e,t,s,o){const{fetcher:l,fallbackData:h,loadInitialCache:c,revalidateOnStart:d,dedupingInterval:v,revalidateOnFocus:b,focusThrottleInterval:q,revalidateOnReconnect:X,reconnectWhen:_,focusWhen:$,revalidateFunction:O}=n(n({},this.options),o),H=T=>{var F;return(F=O==null?void 0:O(this.resolveKey(e),T))!=null?F:this.revalidate(this.resolveKey(e),T)},C=()=>H({fetcher:l,dedupingInterval:v}),p=c?this.get(this.resolveKey(e)):h!=null?h:void 0,x=d?C():Promise.resolve(void 0),z=p?Promise.resolve(p):x;p&&(t==null||t(p));const I=t?this.subscribeData(this.resolveKey(e),t):void 0,E=s?this.subscribeErrors(this.resolveKey(e),s):void 0,R=$(C,{throttleInterval:q,enabled:b}),M=_(C,{enabled:X});return{unsubscribe:()=>{I==null||I(),E==null||E(),R==null||R(),M==null||M()},dataPromise:z,revalidatePromise:x}}}i.CacheItem=w,i.DefaultCache=D,i.DefaultSWREventManager=r,i.SWR=j,i.defaultCacheClearOptions=P,i.defaultCacheRemoveOptions=a,i.defaultClearOptions=S,i.defaultMutateOptions=K,i.defaultOptions=g,i.defaultRevalidateOptions=m,Object.defineProperties(i,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
(function(i,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(i=typeof globalThis<"u"?globalThis:i||self,n(i.swrev={}))})(this,function(i){"use strict";var X=Object.defineProperty;var $=(i,n,l)=>n in i?X(i,n,{enumerable:!0,configurable:!0,writable:!0,value:l}):i[n]=l;var d=(i,n,l)=>($(i,typeof n!="symbol"?n+"":n,l),l);class n{constructor(){d(this,"listeners",new Map)}subscribe(e,t){this.listeners.has(e)||this.listeners.set(e,[]),!this.listeners.get(e).includes(t)&&this.listeners.get(e).push(t)}unsubscribe(e,t){this.listeners.has(e)&&this.listeners.get(e).includes(t)&&(this.listeners.get(e).splice(this.listeners.get(e).indexOf(t),1),this.listeners.get(e).length===0&&this.listeners.delete(e))}emit(e,t){this.listeners.has(e)&&this.listeners.get(e).forEach(s=>s(t))}}const l={broadcast:!1},R={broadcast:!1};class b{constructor({data:e,expiresAt:t=null}){d(this,"data");d(this,"expiresAt");this.data=e,this.expiresAt=t}isResolving(){return this.data instanceof Promise}hasExpired(){return this.expiresAt===null||this.expiresAt<new Date}expiresIn(e){return this.expiresAt=new Date,this.expiresAt.setMilliseconds(this.expiresAt.getMilliseconds()+e),this}}class M{constructor(){d(this,"elements",new Map);d(this,"event",new n)}resolve(e,t){Promise.resolve(t.data).then(s=>{if(s==null)return this.remove(e);t.data=s,this.broadcast(e,s)})}get(e){return this.elements.get(e)}set(e,t){this.elements.set(e,t),this.resolve(e,t)}remove(e,t){const{broadcast:s}={...l,...t};s&&this.broadcast(e,void 0),this.elements.delete(e)}clear(e){const{broadcast:t}={...R,...e};if(t)for(const s of this.elements.keys())this.broadcast(s,void 0);this.elements.clear()}has(e){return this.elements.has(e)}subscribe(e,t){this.event.subscribe(e,t)}unsubscribe(e,t){this.event.unsubscribe(e,t)}broadcast(e,t){this.event.emit(e,t)}}const p={cache:new M,errors:new n,fetcher:async c=>{const e=await fetch(c);if(!e.ok)throw Error("Not a 2XX response.");return e.json()},fallbackData:void 0,loadInitialCache:!0,revalidateOnStart:!0,dedupingInterval:2e3,revalidateOnFocus:!0,focusThrottleInterval:5e3,revalidateOnReconnect:!0,reconnectWhen:(c,{enabled:e})=>e&&typeof window<"u"?(window.addEventListener("online",c),()=>window.removeEventListener("online",c)):()=>{},focusWhen:(c,{enabled:e,throttleInterval:t})=>{if(e&&typeof window<"u"){let s=null;const r=()=>{const o=Date.now();(s===null||o-s>t)&&(s=o,c())};return window.addEventListener("focus",r),()=>window.removeEventListener("focus",r)}return()=>{}},revalidateFunction:void 0},w={...p,force:!1},W={revalidate:!0,revalidateOptions:{...w},revalidateFunction:void 0},A={broadcast:!1};class K{constructor(e){d(this,"options");this.options={...p,...e}}get cache(){return this.options.cache}get errors(){return this.options.errors}async requestData(e,t){return await Promise.resolve(t(e)).catch(s=>{throw this.errors.emit(e,s),s})}resolveKey(e){if(typeof e=="function")try{return e()}catch{return}return e}clear(e,t){const s={...A,...t};if(e==null)return this.cache.clear(s);if(!Array.isArray(e))return this.cache.remove(e,s);for(const r of e)this.cache.remove(r,s)}async revalidate(e,t){if(!e)throw new Error("[Revalidate] Key issue: ${key}");const{fetcher:s,dedupingInterval:r}=this.options,{force:o,fetcher:h,dedupingInterval:a}={...w,fetcher:s,dedupingInterval:r,...t};if(o||!this.cache.has(e)||this.cache.has(e)&&this.cache.get(e).hasExpired()){const u=this.requestData(e,h),f=u.catch(()=>{});return this.cache.set(e,new b({data:f}).expiresIn(a)),await u}return this.getWait(e)}async mutate(e,t,s){if(!e)throw new Error("[Mutate] Key issue: ${key}");const{revalidate:r,revalidateOptions:o,revalidateFunction:h}={...W,...s};let a;if(typeof t=="function"){let u;if(this.cache.has(e)){const f=this.cache.get(e);f.isResolving()||(u=f.data)}a=t(u)}else a=t;return this.cache.set(e,new b({data:a})),r?await((h==null?void 0:h(e,o))??this.revalidate(e,o)):a}subscribeData(e,t){if(e){const s=r=>t(r);return this.cache.subscribe(e,s),()=>this.cache.unsubscribe(e,s)}return()=>{}}subscribeErrors(e,t){if(e){const s=r=>t(r);return this.errors.subscribe(e,s),()=>this.errors.unsubscribe(e,s)}return()=>{}}get(e){if(e&&this.cache.has(e)){const t=this.cache.get(e);if(!t.isResolving())return t.data}}getWait(e){return new Promise((t,s)=>{const r=this.subscribeData(e,a=>{if(r(),a!==void 0)return t(a)}),o=this.subscribeErrors(e,a=>{if(o(),a!==void 0)return s(a)}),h=this.get(e);if(h!==void 0)return t(h)})}subscribe(e,t,s,r){const{fetcher:o,fallbackData:h,loadInitialCache:a,revalidateOnStart:u,dedupingInterval:f,revalidateOnFocus:S,focusThrottleInterval:x,revalidateOnReconnect:T,reconnectWhen:F,focusWhen:L,revalidateFunction:g}={...this.options,...r},j=P=>(g==null?void 0:g(this.resolveKey(e),P))??this.revalidate(this.resolveKey(e),P),m=()=>j({fetcher:o,dedupingInterval:f}),v=a?this.get(this.resolveKey(e)):h??void 0,D=u?m():Promise.resolve(void 0),q=v?Promise.resolve(v):D;v&&(t==null||t(v));const O=t?this.subscribeData(this.resolveKey(e),t):void 0,C=s?this.subscribeErrors(this.resolveKey(e),s):void 0,I=L(m,{throttleInterval:x,enabled:S}),E=F(m,{enabled:T});return{unsubscribe:()=>{O==null||O(),C==null||C(),I==null||I(),E==null||E()},dataPromise:q,revalidatePromise:D}}}i.CacheItem=b,i.DefaultCache=M,i.DefaultSWREventManager=n,i.SWR=K,i.defaultCacheClearOptions=R,i.defaultCacheRemoveOptions=l,i.defaultClearOptions=A,i.defaultMutateOptions=W,i.defaultOptions=p,i.defaultRevalidateOptions=w,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
{
"version": "4.0.0-next.6",
"version": "4.0.0",
"license": "MIT",

@@ -13,7 +13,7 @@ "name": "swrev",

"main": "./dist/swrev.umd.js",
"module": "./dist/swrev.es.js",
"module": "./dist/swrev.mjs",
"exports": {
"./package.json": "./package.json",
".": {
"import": "./dist/swrev.es.js",
"import": "./dist/swrev.mjs",
"require": "./dist/swrev.umd.js"

@@ -32,5 +32,5 @@ }

"devDependencies": {
"typescript": "^4.6.2",
"vite": "^2.8.6"
"typescript": "^4.9.5",
"vite": "^4.3.8"
}
}
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