stunk 3.0.0-rc → 3.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -64,11 +64,18 @@ interface UseAsyncChunkResultWithPagination<T, E extends Error, P extends Record
|
|
|
64
64
|
interface UseAsyncChunkResultWithParamsAndPagination<T, E extends Error, P extends Record<string, any>> extends UseAsyncChunkResultWithParams<T, E, P>, Omit<UseAsyncChunkResultWithPagination<T, E, P>, keyof UseAsyncChunkResult<T, E, P>> {
|
|
65
65
|
}
|
|
66
66
|
interface UseAsyncChunkOptions<P extends Record<string, any> = {}> {
|
|
67
|
-
/**
|
|
67
|
+
/**
|
|
68
|
+
* Parameters to pass to the fetcher. When these change between renders,
|
|
69
|
+
* the chunk automatically re-fetches with the new values.
|
|
70
|
+
*/
|
|
71
|
+
params?: Partial<P>;
|
|
72
|
+
/**
|
|
73
|
+
* @deprecated Use `params` instead. Will be removed in v3 stable.
|
|
74
|
+
*/
|
|
68
75
|
initialParams?: Partial<P>;
|
|
69
76
|
/**
|
|
70
77
|
* Force a fetch on mount even when the chunk has no params.
|
|
71
|
-
* Ignored if
|
|
78
|
+
* Ignored if params is provided.
|
|
72
79
|
* (default: false)
|
|
73
80
|
*/
|
|
74
81
|
fetchOnMount?: boolean;
|
package/dist/use-react/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {a}from'../chunk-7PLFRQ2T.js';import {b}from'../chunk-PZ7A72TA.js';import {useState,useEffect,useCallback,useRef}from'react';function
|
|
1
|
+
import {a}from'../chunk-7PLFRQ2T.js';import {b}from'../chunk-PZ7A72TA.js';import {useState,useEffect,useCallback,useRef}from'react';function S(e,r,t={}){let{useShallowEqual:i=false}=t,u=e.get(),n=r(u),a$1=a(n),l=()=>{let m=e.get(),c=r(m);(i?!b(c,n):c!==n)&&(n=c,a$1.set(c));},o=e.subscribe(l),{set:d,reset:T,...E}=a$1;return {...E,derive:m=>S(a$1,m,t),destroy:()=>{o(),a$1.destroy();}}}function M(e,r){let t=r?S(e,r):e,[i,u]=useState(()=>t.get());useEffect(()=>{let o=t.subscribe(d=>{u(()=>d);});return ()=>o()},[t]);let n=useCallback(o=>{"set"in e&&e.set(o);},[e]),a=useCallback(()=>{"reset"in e&&e.reset();},[e]),l=useCallback(()=>{e.destroy();},[e]);return [i,n,a,l]}function K(e,r){let[t]=M(e,r);return t}function y(e){return "nextPage"in e}function A(e){return "setParams"in e}function B(e){return "clearParams"in e}function O(e,r={}){let t=r.params??r.initialParams,{fetchOnMount:i=false}=r,[u,n]=useState(()=>e.get()),a=useRef({resolvedParams:t,fetchOnMount:i});a.current={resolvedParams:t,fetchOnMount:i},useEffect(()=>{n(e.get());let s=e.subscribe(j=>{n(j);}),{resolvedParams:D,fetchOnMount:_}=a.current;return D&&A(e)?e.setParams(D):_&&e.reload(),()=>{s(),e.cleanup();}},[e]);let l=useRef(false),o=t?JSON.stringify(t):null;useEffect(()=>{if(!l.current){l.current=true;return}t&&A(e)&&e.setParams(t);},[o]);let d=useCallback(s=>e.reload(s),[e]),T=useCallback(s=>e.refresh(s),[e]),E=useCallback(s=>e.mutate(s),[e]),m=useCallback(()=>e.reset(),[e]),c=useCallback(s=>{A(e)&&e.setParams(s);},[e]),f=useCallback(()=>{B(e)&&e.clearParams();},[e]),h=useCallback(()=>y(e)?e.nextPage():Promise.resolve(),[e]),x=useCallback(()=>y(e)?e.prevPage():Promise.resolve(),[e]),v=useCallback(s=>y(e)?e.goToPage(s):Promise.resolve(),[e]),C=useCallback(()=>y(e)?e.resetPagination():Promise.resolve(),[e]),{data:p,loading:g,error:k,lastFetched:q,isPlaceholderData:L=false,pagination:H}=u,R={data:p,loading:g,error:k,lastFetched:q,isPlaceholderData:L,reload:d,refresh:T,mutate:E,reset:m};if(A(e)&&(R.setParams=c,R.clearParams=f),y(e)){let s=R;s.pagination=H,s.nextPage=h,s.prevPage=x,s.goToPage=v,s.resetPagination=C;}return R}function X(e,r={}){let{initialParams:t,autoLoad:i=true,threshold:u=1,fetchOnMount:n}=r,a=O(e,{...t&&{initialParams:t},fetchOnMount:n}),{loading:l,pagination:o,nextPage:d,data:T,error:E,isPlaceholderData:m}=a,c=useRef(l),f=useRef(o?.hasMore??false),h=useRef(d);c.current=l,f.current=o?.hasMore??false,h.current=d;let x=useRef(null);useEffect(()=>{if(!i||typeof window>"u"||!("IntersectionObserver"in window))return;let p=new IntersectionObserver(k=>{k[0].isIntersecting&&!c.current&&f.current&&h.current();},{threshold:u}),g=x.current;return g&&p.observe(g),()=>{g&&p.unobserve(g),p.disconnect();}},[i,u]);let v=useCallback(()=>{!c.current&&f.current&&h.current();},[]),C=l&&T!==null&&T.length>0&&(o?.page??1)>1;return {...a,data:T,error:E,isPlaceholderData:m??false,isFetchingMore:C,hasMore:o?.hasMore??false,loadMore:v,observerTarget:x}}function $(e){let[r,t]=useState(()=>e.get());useEffect(()=>(t(e.get()),e.subscribe(a=>{t(a);})),[e]);let i=useCallback((...n)=>e.mutate(...n),[e]),u=useCallback(()=>e.reset(),[e]);return {loading:r.loading,data:r.data,error:r.error,isSuccess:r.isSuccess,mutate:i,reset:u}}export{O as useAsyncChunk,M as useChunk,K as useChunkValue,X as useInfiniteAsyncChunk,$ as useMutation};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "stunk",
|
|
3
|
-
"version": "3.0.0-rc",
|
|
3
|
+
"version": "3.0.0-rc.1",
|
|
4
4
|
"description": "Stunk is a lightweight, framework-agnostic state management library for JavaScript and TypeScript. It uses chunk-based state units for efficient updates, reactivity, and performance optimization in React, Vue(WIP), Svelte(Coming soon), and Vanilla JS/TS applications.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|