@@ -52,22 +52,22 @@ export function useSubmission<T extends Array<any>, U>(
52
52
53
53
export function useAction < T extends Array < any > , U > ( action : Action < T , U > ) {
54
54
const r = useRouter ( ) ;
55
- return ( ...args : Parameters < Action < T , U > > ) => action . apply ( { r } , args ) ;
55
+ return ( ...args : Parameters < Action < T , U > > ) => action . apply ( { r } , args ) ;
56
56
}
57
57
58
58
export function action < T extends Array < any > , U = void > (
59
59
fn : ( ...args : T ) => Promise < U > ,
60
60
name ?: string
61
61
) : Action < T , U > {
62
- function mutate ( this : { r : RouterContext , f ?: HTMLFormElement } , ...variables : T ) {
62
+ function mutate ( this : { r : RouterContext ; f ?: HTMLFormElement } , ...variables : T ) {
63
63
const router = this . r ;
64
64
const form = this . f ;
65
65
const p = (
66
66
router . singleFlight && ( fn as any ) . withOptions
67
67
? ( fn as any ) . withOptions ( { headers : { "X-Single-Flight" : "true" } } )
68
68
: fn
69
69
) ( ...variables ) ;
70
- const [ result , setResult ] = createSignal < { data ?: U , error ?: any } > ( ) ;
70
+ const [ result , setResult ] = createSignal < { data ?: U ; error ?: any } > ( ) ;
71
71
let submission : Submission < T , U > ;
72
72
function handler ( error ?: boolean ) {
73
73
return async ( res : any ) => {
@@ -144,22 +144,18 @@ const hashString = (s: string) =>
144
144
145
145
async function handleResponse ( response : unknown , error : boolean | undefined , navigate : Navigator ) {
146
146
let data : any ;
147
+ let custom : any ;
147
148
let keys : string [ ] | undefined ;
148
- let invalidateKeys : string [ ] | undefined ;
149
+ let flightKeys : string [ ] | undefined ;
149
150
if ( response instanceof Response ) {
150
151
if ( response . headers . has ( "X-Revalidate" ) )
151
- keys = invalidateKeys = response . headers . get ( "X-Revalidate" ) ! . split ( "," ) ;
152
+ keys = response . headers . get ( "X-Revalidate" ) ! . split ( "," ) ;
152
153
if ( ( response as any ) . customBody ) {
153
- data = await ( response as any ) . customBody ( ) ;
154
+ data = custom = await ( response as any ) . customBody ( ) ;
154
155
if ( response . headers . has ( "X-Single-Flight" ) ) {
155
- keys || ( keys = [ ] ) ;
156
- invalidateKeys || ( invalidateKeys = [ ] ) ;
157
- Object . keys ( data ) . forEach ( key => {
158
- if ( key === "_$value" ) return ;
159
- keys ! . push ( key ) ;
160
- cache . set ( key , data [ key ] ) ;
161
- } ) ;
162
156
data = data . _$value ;
157
+ delete custom . _$value ;
158
+ flightKeys = Object . keys ( custom ) ;
163
159
}
164
160
}
165
161
if ( response . headers . has ( "Location" ) ) {
@@ -173,7 +169,9 @@ async function handleResponse(response: unknown, error: boolean | undefined, nav
173
169
} else if ( error ) return { error : response } ;
174
170
else data = response ;
175
171
// invalidate
176
- cacheKeyOp ( invalidateKeys , entry => ( entry [ 0 ] = 0 ) ) ;
172
+ cacheKeyOp ( keys , entry => ( entry [ 0 ] = 0 ) ) ;
173
+ // set cache
174
+ flightKeys && flightKeys . forEach ( k => cache . set ( k , custom [ k ] ) ) ;
177
175
// trigger revalidation
178
176
await revalidate ( keys , false ) ;
179
177
return data != null ? { data } : undefined ;
0 commit comments