stunk 3.0.0-rc.2 → 3.0.0-rc.3
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.
- package/dist/use-react/index.d.ts +15 -5
- package/dist/use-react/index.js +1 -1
- package/package.json +1 -1
|
@@ -63,7 +63,7 @@ interface UseAsyncChunkResultWithPagination<T, E extends Error, P extends Record
|
|
|
63
63
|
}
|
|
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
|
-
interface UseAsyncChunkOptions<P extends Record<string, any> = {}> {
|
|
66
|
+
interface UseAsyncChunkOptions<T = any, E extends Error = Error, P extends Record<string, any> = {}> {
|
|
67
67
|
/**
|
|
68
68
|
* Parameters to pass to the fetcher. When these change between renders,
|
|
69
69
|
* the chunk automatically re-fetches with the new values.
|
|
@@ -79,15 +79,25 @@ interface UseAsyncChunkOptions<P extends Record<string, any> = {}> {
|
|
|
79
79
|
* (default: false)
|
|
80
80
|
*/
|
|
81
81
|
fetchOnMount?: boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Called after every successful fetch at the hook level.
|
|
84
|
+
* Has full access to React context — safe to call navigate(), setState(), etc.
|
|
85
|
+
*/
|
|
86
|
+
onSuccess?: (data: T) => void;
|
|
87
|
+
/**
|
|
88
|
+
* Called when a fetch fails at the hook level.
|
|
89
|
+
* Has full access to React context — safe to call navigate(), setState(), etc.
|
|
90
|
+
*/
|
|
91
|
+
onError?: (error: E) => void;
|
|
82
92
|
}
|
|
83
93
|
declare function useAsyncChunk<T, E extends Error = Error, P extends Record<string, any> = {}>(asyncChunk: PaginatedAsyncChunk<T, E> & {
|
|
84
94
|
setParams: (params: Partial<P>) => void;
|
|
85
|
-
}, options?: UseAsyncChunkOptions<P>): UseAsyncChunkResultWithParamsAndPagination<T, E, P>;
|
|
86
|
-
declare function useAsyncChunk<T, E extends Error = Error, P extends Record<string, any> = {}>(asyncChunk: PaginatedAsyncChunk<T, E>, options?: UseAsyncChunkOptions<P>): UseAsyncChunkResultWithPagination<T, E, P>;
|
|
95
|
+
}, options?: UseAsyncChunkOptions<T, E, P>): UseAsyncChunkResultWithParamsAndPagination<T, E, P>;
|
|
96
|
+
declare function useAsyncChunk<T, E extends Error = Error, P extends Record<string, any> = {}>(asyncChunk: PaginatedAsyncChunk<T, E>, options?: UseAsyncChunkOptions<T, E, P>): UseAsyncChunkResultWithPagination<T, E, P>;
|
|
87
97
|
declare function useAsyncChunk<T, E extends Error = Error, P extends Record<string, any> = {}>(asyncChunk: AsyncChunk<T, E> & {
|
|
88
98
|
setParams: (params: Partial<P>) => void;
|
|
89
|
-
}, options?: UseAsyncChunkOptions<P>): UseAsyncChunkResultWithParams<T, E, P>;
|
|
90
|
-
declare function useAsyncChunk<T, E extends Error = Error, P extends Record<string, any> = {}>(asyncChunk: AsyncChunk<T, E>, options?: UseAsyncChunkOptions<P>): UseAsyncChunkResult<T, E, P>;
|
|
99
|
+
}, options?: UseAsyncChunkOptions<T, E, P>): UseAsyncChunkResultWithParams<T, E, P>;
|
|
100
|
+
declare function useAsyncChunk<T, E extends Error = Error, P extends Record<string, any> = {}>(asyncChunk: AsyncChunk<T, E>, options?: UseAsyncChunkOptions<T, E, P>): UseAsyncChunkResult<T, E, P>;
|
|
91
101
|
|
|
92
102
|
interface UseInfiniteAsyncChunkOptions<P extends Record<string, any>> extends Omit<UseAsyncChunkOptions<P>, 'initialParams'> {
|
|
93
103
|
/** Initial parameters — page and pageSize are managed automatically */
|
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 U(e,t,r={}){let{useShallowEqual:i=false}=r,u=e.get(),a$1=t(u),o=a(a$1),l=()=>{let f=e.get(),c=t(f);(i?!b(c,a$1):c!==a$1)&&(a$1=c,o.set(c));},n=e.subscribe(l),{set:d,reset:m,...h}=o;return {...h,derive:f=>U(o,f,r),destroy:()=>{n(),o.destroy();}}}function O(e,t){let r=t?U(e,t):e,[i,u]=useState(()=>r.get());useEffect(()=>{let n=r.subscribe(d=>{u(()=>d);});return ()=>n()},[r]);let a=useCallback(n=>{"set"in e&&e.set(n);},[e]),o=useCallback(()=>{"reset"in e&&e.reset();},[e]),l=useCallback(()=>{e.destroy();},[e]);return [i,a,o,l]}function X(e,t){let[r]=O(e,t);return r}function x(e){return "nextPage"in e}function v(e){return "setParams"in e}function Z(e){return "clearParams"in e}function W(e,t={}){let r=t.params??t.initialParams,{fetchOnMount:i=false,onSuccess:u,onError:a}=t,[o,l]=useState(()=>e.get()),n=useRef({resolvedParams:r,fetchOnMount:i,onSuccess:u,onError:a});n.current={resolvedParams:r,fetchOnMount:i,onSuccess:u,onError:a};let d=useRef(e.get());useEffect(()=>{let s=e.get();l(s),d.current=s;let N=e.subscribe(T=>{let I=d.current;I.loading&&!T.loading&&!T.error&&T.data!==null&&n.current.onSuccess?.(T.data),I.loading&&!T.loading&&T.error&&n.current.onError?.(T.error),d.current=T,l(T);}),{resolvedParams:V,fetchOnMount:B}=n.current;return V&&v(e)?e.setParams(V):B&&e.reload(),()=>{N(),e.cleanup();}},[e]);let m=useRef(false),h=r?JSON.stringify(r):null;useEffect(()=>{if(!m.current){m.current=true;return}r&&v(e)&&e.setParams(r);},[h]);let f=useCallback(s=>e.reload(s),[e]),c=useCallback(s=>e.refresh(s),[e]),E=useCallback(s=>e.mutate(s),[e]),p=useCallback(()=>e.reset(),[e]),R=useCallback(s=>{v(e)&&e.setParams(s);},[e]),C=useCallback(()=>{Z(e)&&e.clearParams();},[e]),k=useCallback(()=>x(e)?e.nextPage():Promise.resolve(),[e]),y=useCallback(()=>x(e)?e.prevPage():Promise.resolve(),[e]),g=useCallback(s=>x(e)?e.goToPage(s):Promise.resolve(),[e]),S=useCallback(()=>x(e)?e.resetPagination():Promise.resolve(),[e]),{data:_,loading:j,error:w,lastFetched:z,isPlaceholderData:J=false,pagination:K}=o,A={data:_,loading:j,error:w,lastFetched:z,isPlaceholderData:J,reload:f,refresh:c,mutate:E,reset:p};if(v(e)&&(A.setParams=R,A.clearParams=C),x(e)){let s=A;s.pagination=K,s.nextPage=k,s.prevPage=y,s.goToPage=g,s.resetPagination=S;}return A}function re(e,t={}){let{initialParams:r,autoLoad:i=true,threshold:u=1,fetchOnMount:a}=t,o=W(e,{...r&&{initialParams:r},fetchOnMount:a}),{loading:l,pagination:n,nextPage:d,data:m,error:h,isPlaceholderData:f}=o,c=useRef(l),E=useRef(n?.hasMore??false),p=useRef(d);c.current=l,E.current=n?.hasMore??false,p.current=d;let R=useRef(null);useEffect(()=>{if(!i||typeof window>"u"||!("IntersectionObserver"in window))return;let y=new IntersectionObserver(S=>{S[0].isIntersecting&&!c.current&&E.current&&p.current();},{threshold:u}),g=R.current;return g&&y.observe(g),()=>{g&&y.unobserve(g),y.disconnect();}},[i,u]);let C=useCallback(()=>{!c.current&&E.current&&p.current();},[]),k=l&&m!==null&&m.length>0&&(n?.page??1)>1;return {...o,data:m,error:h,isPlaceholderData:f??false,isFetchingMore:k,hasMore:n?.hasMore??false,loadMore:C,observerTarget:R}}function ne(e){let[t,r]=useState(()=>e.get());useEffect(()=>(r(e.get()),e.subscribe(o=>{r(o);})),[e]);let i=useCallback((...a)=>e.mutate(...a),[e]),u=useCallback(()=>e.reset(),[e]);return {loading:t.loading,data:t.data,error:t.error,isSuccess:t.isSuccess,mutate:i,reset:u}}export{W as useAsyncChunk,O as useChunk,X as useChunkValue,re as useInfiniteAsyncChunk,ne 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.3",
|
|
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": {
|