1- import  {  MutationObserver  }  from  '@tanstack/query-core' 
2- import  {  untrack  }  from  'svelte' 
1+ import  {  onDestroy  }  from  'svelte' 
2+ 
3+ import  {  MutationObserver ,  notifyManager  }  from  '@tanstack/query-core' 
34import  {  useQueryClient  }  from  './useQueryClient.js' 
4- import  {  createRawRef  }  from  './containers.svelte.js' 
55import  type  { 
66  Accessor , 
77  CreateMutateFunction , 
88  CreateMutationOptions , 
99  CreateMutationResult , 
1010}  from  './types.js' 
11+ 
1112import  type  {  DefaultError ,  QueryClient  }  from  '@tanstack/query-core' 
1213
14+ /** 
15+  * @param  options - A function that returns mutation options 
16+  * @param  queryClient - Custom query client which overrides provider 
17+  */ 
1318export  function  createMutation < 
1419  TData  =  unknown , 
1520  TError  =  DefaultError , 
1621  TVariables  =  void , 
1722  TContext  =  unknown , 
1823> ( 
1924  options : Accessor < CreateMutationOptions < TData ,  TError ,  TVariables ,  TContext > > , 
20-   queryClientOption ?: Accessor < QueryClient > , 
25+   queryClient ?: Accessor < QueryClient > , 
2126) : CreateMutationResult < TData ,  TError ,  TVariables ,  TContext >  { 
22-   const  queryClient  =  $derived ( queryClientOption ?.( ) ) 
23-   const  client  =  $derived ( useQueryClient ( queryClient ) ) 
27+   const  client  =  useQueryClient ( queryClient ?.( ) ) 
2428
2529  const  observer  =  $derived ( 
2630    new  MutationObserver < TData ,  TError ,  TVariables ,  TContext > ( 
2731      client , 
28-       untrack ( ( )   =>   options ( ) ) , 
32+       options ( ) , 
2933    ) , 
3034  ) 
3135
@@ -35,23 +39,35 @@ export function createMutation<
3539    observer . mutate ( variables ,  mutateOptions ) . catch ( noop ) 
3640  } ) 
3741
38-   function  createResult ( )  { 
39-     const  result  =  observer . getCurrentResult ( ) 
40-     return  { 
41-       ...result , 
42-       mutateAsync : result . mutate , 
43-       mutate, 
44-     } 
45-   } 
46- 
47-   // svelte-ignore state_referenced_locally 
48-   const  [ mutation ,  update ]  =  createRawRef ( createResult ( ) ) 
49- 
50-   $effect ( ( )  =>  update ( createResult ( ) ) ) 
5142  $effect . pre ( ( )  =>  { 
5243    observer . setOptions ( options ( ) ) 
5344  } ) 
54-   return  mutation 
45+ 
46+   const  result  =  $state ( observer . getCurrentResult ( ) ) 
47+ 
48+   const  unsubscribe  =  observer . subscribe ( ( val )  =>  { 
49+     notifyManager . batchCalls ( ( )  =>  { 
50+       Object . assign ( result ,  val ) 
51+     } ) ( ) 
52+   } ) 
53+ 
54+   onDestroy ( ( )  =>  { 
55+     unsubscribe ( ) 
56+   } ) 
57+ 
58+   // @ts -expect-error 
59+   return  new  Proxy ( result ,  { 
60+     get : ( _ ,  prop )  =>  { 
61+       const  r  =  { 
62+         ...result , 
63+         mutate, 
64+         mutateAsync : result . mutate , 
65+       } 
66+       if  ( prop  ==  'value' )  return  r 
67+       // @ts -expect-error 
68+       return  r [ prop ] 
69+     } , 
70+   } ) 
5571} 
5672
5773function  noop ( )  { } 
0 commit comments