Comparing version 1.7.0 to 1.8.0
@@ -1,2 +0,2 @@ | ||
import { SWR, SWRKey, SWROptions, SWRMutateOptions, SWRRevalidateOptions, CacheClearOptions } from 'swrev'; | ||
import { SWR, SWRKey, SWROptions, SWRMutateOptions, SWRMutateValue, SWRRevalidateOptions, CacheClearOptions } from 'swrev'; | ||
/** | ||
@@ -13,5 +13,5 @@ * Exports the extended SWR class with an extra method | ||
error: import("svelte/store").Writable<E | undefined>; | ||
mutate: (value: D, ops?: Partial<SWRMutateOptions<D>> | undefined) => void; | ||
revalidate: (ops?: Partial<SWRRevalidateOptions<D>> | undefined) => void; | ||
clear: (ops?: Partial<CacheClearOptions> | undefined) => void; | ||
mutate: (value: SWRMutateValue<D>, ops?: Partial<SWRMutateOptions<D>>) => void; | ||
revalidate: (ops?: Partial<SWRRevalidateOptions<D>>) => void; | ||
clear: (ops?: Partial<CacheClearOptions>) => void; | ||
isLoading: import("svelte/store").Readable<boolean>; | ||
@@ -55,3 +55,3 @@ isValid: import("svelte/store").Readable<boolean>; | ||
*/ | ||
export declare const get: <D = any>(key?: string | undefined) => D | undefined; | ||
export declare const get: <D = any>(key?: SWRKey) => D | undefined; | ||
/** | ||
@@ -77,5 +77,5 @@ * Gets an element from the cache. The difference | ||
error: import("svelte/store").Writable<E | undefined>; | ||
mutate: (value: D, ops?: Partial<SWRMutateOptions<D>> | undefined) => void; | ||
mutate: (value: SWRMutateValue<D>, ops?: Partial<SWRMutateOptions<D>> | undefined) => void; | ||
revalidate: (ops?: Partial<SWRRevalidateOptions<D>> | undefined) => void; | ||
clear: (ops?: Partial<CacheClearOptions> | undefined) => void; | ||
clear: (ops?: Partial<CacheClearOptions>) => void; | ||
isLoading: import("svelte/store").Readable<boolean>; | ||
@@ -89,3 +89,3 @@ isValid: import("svelte/store").Readable<boolean>; | ||
*/ | ||
export declare const mutate: <D = any>(key: SWRKey, value: D, options?: Partial<SWRMutateOptions<D>> | undefined) => void; | ||
export declare const mutate: <D = any>(key: SWRKey, value: SWRMutateValue<D>, options?: Partial<SWRMutateOptions<D>> | undefined) => void; | ||
/** | ||
@@ -99,2 +99,2 @@ * Revalidates the key and mutates the cache if needed. | ||
*/ | ||
export declare const clear: (keys?: string | string[] | undefined, options?: Partial<CacheClearOptions> | undefined) => void; | ||
export declare const clear: (keys?: string | string[], options?: Partial<CacheClearOptions>) => void; |
@@ -1,19 +0,3 @@ | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
import { SWR } from "swrev"; | ||
import { onMount, onDestroy } from "svelte"; | ||
import { beforeUpdate, onDestroy } from "svelte"; | ||
function noop() { | ||
@@ -49,3 +33,3 @@ } | ||
let stop; | ||
const subscribers = new Set(); | ||
const subscribers = /* @__PURE__ */ new Set(); | ||
function set(new_value) { | ||
@@ -132,3 +116,3 @@ if (safe_not_equal(value, new_value)) { | ||
const error = writable(void 0, () => () => unsubscribe == null ? void 0 : unsubscribe()); | ||
onMount(() => { | ||
beforeUpdate(() => { | ||
const onData = (d) => { | ||
@@ -139,14 +123,18 @@ error.set(void 0); | ||
const onError = (e) => error.set(e); | ||
unsubscribe = this.use(key, onData, onError, __spreadValues({ | ||
loadInitialCache: true | ||
}, options)).unsubscribe; | ||
if (!unsubscribe) { | ||
unsubscribe = this.use(key, onData, onError, { | ||
loadInitialCache: true, | ||
...options | ||
}).unsubscribe; | ||
} | ||
}); | ||
onDestroy(() => unsubscribe == null ? void 0 : unsubscribe()); | ||
const mutate2 = (value, ops) => { | ||
return this.mutate(this.resolveKey(key), value, __spreadValues({ | ||
revalidateOptions: options | ||
}, ops)); | ||
return this.mutate(this.resolveKey(key), value, { | ||
revalidateOptions: options, | ||
...ops | ||
}); | ||
}; | ||
const revalidate2 = (ops) => { | ||
return this.revalidate(this.resolveKey(key), __spreadValues(__spreadValues({}, options), ops)); | ||
return this.revalidate(this.resolveKey(key), { ...options, ...ops }); | ||
}; | ||
@@ -153,0 +141,0 @@ const clear2 = (ops) => { |
@@ -1,1 +0,1 @@ | ||
var J=Object.defineProperty;var E=Object.getOwnPropertySymbols;var N=Object.prototype.hasOwnProperty,Q=Object.prototype.propertyIsEnumerable;var O=(t,u,o)=>u in t?J(t,u,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[u]=o,g=(t,u)=>{for(var o in u||(u={}))N.call(u,o)&&O(t,o,u[o]);if(E)for(var o of E(u))Q.call(u,o)&&O(t,o,u[o]);return t};(function(t,u){typeof exports=="object"&&typeof module!="undefined"?u(exports,require("swrev"),require("svelte")):typeof define=="function"&&define.amd?define(["exports","swrev","svelte"],u):(t=typeof globalThis!="undefined"?globalThis:t||self,u(t.sswr={},t.swrev,t.svelte))})(this,function(t,u,o){"use strict";function w(){}function D(e){return e()}function j(e){e.forEach(D)}function K(e){return typeof e=="function"}function M(e,n){return e!=e?n==n:e!==n||e&&typeof e=="object"||typeof e=="function"}function T(e,...n){if(e==null)return w;const s=e.subscribe(...n);return s.unsubscribe?()=>s.unsubscribe():s}Promise.resolve();const h=[];function z(e,n){return{subscribe:y(e,n).subscribe}}function y(e,n=w){let s;const r=new Set;function l(a){if(M(e,a)&&(e=a,s)){const b=!h.length;for(const i of r)i[1](),h.push(i,e);if(b){for(let i=0;i<h.length;i+=2)h[i][0](h[i+1]);h.length=0}}}function d(a){l(a(e))}function S(a,b=w){const i=[a,b];return r.add(i),r.size===1&&(s=n(l)||w),a(e),()=>{r.delete(i),r.size===0&&(s(),s=null)}}return{set:l,update:d,subscribe:S}}function _(e,n,s){const r=!Array.isArray(e),l=r?[e]:e,d=n.length<2;return z(s,S=>{let a=!1;const b=[];let i=0,W=w;const c=()=>{if(i)return;W();const f=n(r?b[0]:b,S);d?S(f):W=K(f)?f:w},v=l.map((f,R)=>T(f,H=>{b[R]=H,i&=~(1<<R),a&&c()},()=>{i|=1<<R}));return a=!0,c(),function(){j(v),W()}})}class q extends u.SWR{useSWR(n,s){let r;const l=y(void 0,()=>()=>r==null?void 0:r()),d=y(void 0,()=>()=>r==null?void 0:r());o.onMount(()=>{const c=f=>{d.set(void 0),l.set(f)},v=f=>d.set(f);r=this.use(n,c,v,g({loadInitialCache:!0},s)).unsubscribe}),o.onDestroy(()=>r==null?void 0:r());const S=(c,v)=>this.mutate(this.resolveKey(n),c,g({revalidateOptions:s},v)),a=c=>this.revalidate(this.resolveKey(n),g(g({},s),c)),b=c=>this.clear(this.resolveKey(n),c),i=_([l,d],([c,v])=>c===void 0&&v===void 0),W=_([l,d],([c,v])=>c!==void 0&&v===void 0);return{data:l,error:d,mutate:S,revalidate:a,clear:b,isLoading:i,isValid:W}}}const m=e=>new q(e);t.swr=m();const A=e=>(t.swr=m(e),t.swr),P=(e,n)=>t.swr.subscribe(e,n),C=(e,n)=>t.swr.subscribeErrors(e,n),I=e=>t.swr.get(e),L=e=>t.swr.getOrWait(e),V=(e,n,s,r)=>t.swr.use(e,n,s,r),$=(e,n)=>t.swr.useSWR(e,n),B=(e,n,s)=>t.swr.mutate(e,n,s),F=(e,n)=>t.swr.revalidate(e,n),G=(e,n)=>t.swr.clear(e,n);t.SSWR=q,t.clear=G,t.createDefaultSWR=A,t.createSWR=m,t.get=I,t.getOrWait=L,t.mutate=B,t.revalidate=F,t.subscribe=P,t.subscribeErrors=C,t.use=V,t.useSWR=$,Object.defineProperty(t,"__esModule",{value:!0}),t[Symbol.toStringTag]="Module"}); | ||
(function(t,h){typeof exports=="object"&&typeof module!="undefined"?h(exports,require("swrev"),require("svelte")):typeof define=="function"&&define.amd?define(["exports","swrev","svelte"],h):(t=typeof globalThis!="undefined"?globalThis:t||self,h(t.sswr={},t.swrev,t.svelte))})(this,function(t,h,y){"use strict";function b(){}function q(e){return e()}function E(e){e.forEach(q)}function O(e){return typeof e=="function"}function D(e,r){return e!=e?r==r:e!==r||e&&typeof e=="object"||typeof e=="function"}function j(e,...r){if(e==null)return b;const s=e.subscribe(...r);return s.unsubscribe?()=>s.unsubscribe():s}Promise.resolve();const v=[];function K(e,r){return{subscribe:W(e,r).subscribe}}function W(e,r=b){let s;const n=new Set;function o(c){if(D(e,c)&&(e=c,s)){const l=!v.length;for(const u of n)u[1](),v.push(u,e);if(l){for(let u=0;u<v.length;u+=2)v[u][0](v[u+1]);v.length=0}}}function f(c){o(c(e))}function w(c,l=b){const u=[c,l];return n.add(u),n.size===1&&(s=r(o)||b),c(e),()=>{n.delete(u),n.size===0&&(s(),s=null)}}return{set:o,update:f,subscribe:w}}function R(e,r,s){const n=!Array.isArray(e),o=n?[e]:e,f=r.length<2;return K(s,w=>{let c=!1;const l=[];let u=0,S=b;const i=()=>{if(u)return;S();const a=r(n?l[0]:l,w);f?w(a):S=O(a)?a:b},d=o.map((a,m)=>j(a,$=>{l[m]=$,u&=~(1<<m),c&&i()},()=>{u|=1<<m}));return c=!0,i(),function(){E(d),S()}})}class _ extends h.SWR{useSWR(r,s){let n;const o=W(void 0,()=>()=>n==null?void 0:n()),f=W(void 0,()=>()=>n==null?void 0:n());y.beforeUpdate(()=>{const i=a=>{f.set(void 0),o.set(a)},d=a=>f.set(a);n||(n=this.use(r,i,d,{loadInitialCache:!0,...s}).unsubscribe)}),y.onDestroy(()=>n==null?void 0:n());const w=(i,d)=>this.mutate(this.resolveKey(r),i,{revalidateOptions:s,...d}),c=i=>this.revalidate(this.resolveKey(r),{...s,...i}),l=i=>this.clear(this.resolveKey(r),i),u=R([o,f],([i,d])=>i===void 0&&d===void 0),S=R([o,f],([i,d])=>i!==void 0&&d===void 0);return{data:o,error:f,mutate:w,revalidate:c,clear:l,isLoading:u,isValid:S}}}const g=e=>new _(e);t.swr=g();const T=e=>(t.swr=g(e),t.swr),z=(e,r)=>t.swr.subscribe(e,r),A=(e,r)=>t.swr.subscribeErrors(e,r),M=e=>t.swr.get(e),P=e=>t.swr.getOrWait(e),C=(e,r,s,n)=>t.swr.use(e,r,s,n),I=(e,r)=>t.swr.useSWR(e,r),L=(e,r,s)=>t.swr.mutate(e,r,s),U=(e,r)=>t.swr.revalidate(e,r),V=(e,r)=>t.swr.clear(e,r);t.SSWR=_,t.clear=V,t.createDefaultSWR=T,t.createSWR=g,t.get=M,t.getOrWait=P,t.mutate=L,t.revalidate=U,t.subscribe=z,t.subscribeErrors=A,t.use=C,t.useSWR=I,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); |
{ | ||
"name": "sswr", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"description": "Svelte stale while revalidate (SWR) data fetching strategy", | ||
@@ -5,0 +5,0 @@ "repository": "github.com/ConsoleTVs/sswr", |
@@ -393,39 +393,39 @@ # SSWR (Svelte Stale While Revalidate) | ||
<script lang="ts" context="module"> | ||
import type { Load } from '@sveltejs/kit'; | ||
import type { Load } from '@sveltejs/kit'; | ||
const url = 'https://jsonplaceholder.typicode.com/posts'; | ||
const url = 'https://jsonplaceholder.typicode.com/posts'; | ||
export const load: Load = async ({ fetch }) => { | ||
const response = await fetch(url); | ||
export const load: Load = async ({ fetch }) => { | ||
const response = await fetch(url); | ||
return { | ||
props: { | ||
initialData: await response.json() | ||
} | ||
}; | ||
}; | ||
return { | ||
props: { | ||
initialData: await response.json() | ||
} | ||
}; | ||
}; | ||
</script> | ||
<script lang="ts"> | ||
import { useSWR } from 'sswr'; | ||
import { useSWR } from 'sswr'; | ||
interface Post { | ||
id: number; | ||
title: string; | ||
body: string; | ||
} | ||
interface Post { | ||
id: number; | ||
title: string; | ||
body: string; | ||
} | ||
export let initialData: Post[]; | ||
export let initialData: Post[]; | ||
const { data: posts } = useSWR<Post[]>(url, { | ||
initialData, | ||
revalidateOnStart: false | ||
}); | ||
const { data: posts } = useSWR<Post[]>(url, { | ||
initialData, | ||
revalidateOnStart: false | ||
}); | ||
</script> | ||
{#if $posts} | ||
{#each $posts as post (post.id)} | ||
<h1>{post.title}</h1> | ||
<p>{post.body}</p> | ||
{/each} | ||
{#each $posts as post (post.id)} | ||
<h1>{post.title}</h1> | ||
<p>{post.body}</p> | ||
{/each} | ||
{/if} | ||
@@ -432,0 +432,0 @@ ``` |
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
28343
284