@@ -10,10 +10,10 @@ import { RenderError } from './types/errors';
1010import { createLoadingRoot , createValueRoot , createErrorRoot } from './constructors/data-root' ;
1111
1212
13- export function useDataStateMethods < T , A extends any [ ] , R > (
13+ export function useDataStateMethods < T , A extends any [ ] = any [ ] > (
1414 dataRoot : DataRoot < T > ,
1515 setDataRoot : Dispatch < SetStateAction < DataRoot < T > > > ,
16- config : FetchConfig < T , A , R >
16+ config : FetchConfig < T , A >
1717) {
1818 const setLoading = ( ) => setDataRoot ( createLoadingRoot ( ) ) ;
1919 const setValue = ( dataValue : T ) => setDataRoot ( createValueRoot ( dataValue ) ) ;
@@ -31,25 +31,22 @@ export function useDataStateMethods<T, A extends any[], R>(
3131 // it always KEEPS the FIRST VERSION that it received!
3232 // Those variables have to be provided as ARGUMENTS!
3333 const fetcher = useRef ( config . fetcher ) . current ;
34- const postProcess = useRef ( config . postProcess ) . current ;
3534
3635 // Attach setDataRoot to fetcher so fetching can update the DataState:
3736 const fetch = useCallback ( async ( ) => {
3837 try {
3938 // Skip fetching if one of the arguments is still undefined:
4039 if ( args ?. some ( arg => arg === undefined ) ) return ;
4140
42- let response : R ;
41+ let response : T ;
4342 if ( fetcher ) response = await fetcher ( ...args ) ;
4443 else response = await fetchJson ( args [ 0 ] as string ) ;
4544
46- const result = ( postProcess ? postProcess ( response ) : response ) as T ;
47-
48- setDataRoot ( createValueRoot ( result ) ) ;
45+ setDataRoot ( createValueRoot ( response ) ) ;
4946 } catch ( err ) {
5047 setDataRoot ( createErrorRoot ( err ) ) ;
5148 }
52- } , [ fetcher , args , postProcess , setDataRoot ] ) ;
49+ } , [ fetcher , args , setDataRoot ] ) ;
5350
5451 // Get an actual value by doing initial fetch in useEffect():
5552 // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -109,38 +106,32 @@ export function useDataStateMethods<T, A extends any[], R>(
109106 }
110107
111108 // Create a new DataState containing a subset of the fields of another:
112- const useSubset = < S , B extends any [ ] > (
113- selectorFn : ( data : T , ...args : B ) => S ,
114- selectorArgs ?: B ,
115- ) : DataState < S > => {
109+ const useTransform = < U , B extends any [ ] > (
110+ transformer : ( data : T , ...args : B ) => U ,
111+ transformerArgs ?: B ,
112+ ) : DataState < U > => {
116113 // Stabilize the selector function once - it never changes:
117- const stableSelector = useRef ( selectorFn ) . current ;
114+ const stableTransformer = useRef ( transformer ) . current ;
118115 // Stabilize the arguments:
119116 // eslint-disable-next-line react-hooks/exhaustive-deps
120- const stableArgs = useMemo ( ( ) => selectorArgs || [ ] as unknown as B , selectorArgs || [ ] as unknown as B ) ;
121-
122- const subsetPostProcess = useCallback ( ( response : R ) : S => {
123- const result = ( postProcess ? postProcess ( response ) : response ) as unknown as T ;
124- return stableSelector ( result , ...stableArgs ) ;
125- } , [ stableArgs , stableSelector ] ) ;
117+ const stableArgs = useMemo ( ( ) => transformerArgs || [ ] as unknown as B , transformerArgs || [ ] as unknown as B ) ;
126118
127- const subsetConfig = {
128- fetcher : fetcher ,
129- args : args ,
130- postProcess : subsetPostProcess ,
119+ const transformConfig = {
120+ transformer : stableTransformer ,
121+ args : stableArgs ,
131122 } ;
132123
133124 // Construct the new subset DataState to return:
134- const subsetData = useConfig < S , A , R > ( subsetConfig ) ;
125+ const subsetData = useConfig < U , A > ( transformConfig ) ;
135126
136127 useEffect ( ( ) => {
137128 switch ( dataRoot . status ) {
138129 case 'loading' :
139130 break ;
140131 case 'value' :
141132 try {
142- const selectedValue = stableSelector ( dataRoot . value , ...stableArgs ) ;
143- subsetData . setValue ( selectedValue ) ;
133+ const transformedValue = stableTransformer ( dataRoot . value , ...stableArgs ) ;
134+ subsetData . setValue ( transformedValue ) ;
144135 } catch ( err ) {
145136 // Handle selector errors gracefully:
146137 subsetData . setError (
@@ -160,5 +151,5 @@ export function useDataStateMethods<T, A extends any[], R>(
160151 return subsetData ;
161152 }
162153
163- return { setLoading, setValue, setError, fetch, useInit, Render, useSubset } ;
154+ return { setLoading, setValue, setError, fetch, useInit, Render, useTransform } ;
164155}
0 commit comments