2
2
import * as Result from "@effect-rx/rx/Result"
3
3
import type { InvalidateOptions , InvalidateQueryFilters } from "@tanstack/vue-query"
4
4
import { useQueryClient } from "@tanstack/vue-query"
5
- import { Cause , Effect , Exit , Option } from "effect-app"
5
+ import type { Cause } from "effect-app"
6
+ import { Effect , Option } from "effect-app"
6
7
import type { RequestHandler , RequestHandlerWithInput , TaggedRequestClassAny } from "effect-app/client/clientFor"
7
8
import { tuple } from "effect-app/Function"
8
9
import type { ComputedRef , Ref } from "vue"
9
- import { computed , ref , shallowRef } from "vue"
10
+ import { computed , shallowRef } from "vue"
10
11
import { makeQueryKey , reportRuntimeError } from "./lib.js"
11
12
12
13
export const getQueryKey = ( h : { name : string } ) => {
@@ -69,8 +70,6 @@ export function make<A, E, R>(self: Effect<A, E, R>) {
69
70
return tuple ( result , latestSuccess , execute )
70
71
}
71
72
72
-
73
-
74
73
export type MaybeRef < T > = Ref < T > | ComputedRef < T > | T
75
74
type MaybeRefDeep < T > = MaybeRef <
76
75
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
@@ -108,6 +107,24 @@ export interface MutationOptions<A, E, R, A2 = A, E2 = E, R2 = R, I = void> {
108
107
// }
109
108
*/
110
109
110
+ export const asResult = < Args extends readonly any [ ] , A , E , R > (
111
+ handler : ( ...args : Args ) => Effect < A , E , R >
112
+ ) => {
113
+ const state = shallowRef < Result . Result < A , E > > ( Result . initial ( ) )
114
+
115
+ const act = ( ...args : Args ) =>
116
+ Effect
117
+ . sync ( ( ) => {
118
+ state . value = Result . initial ( true )
119
+ } )
120
+ . pipe (
121
+ Effect . zipRight ( Effect . suspend ( ( ) => handler ( ...args ) ) ) ,
122
+ Effect . onExit ( ( exit ) => Effect . sync ( ( ) => ( state . value = Result . fromExit ( exit ) ) ) )
123
+ )
124
+
125
+ return tuple ( state , act )
126
+ }
127
+
111
128
export const makeMutation = ( ) => {
112
129
/**
113
130
* Pass a function that returns an Effect, e.g from a client action, or an Effect
@@ -133,24 +150,12 @@ export const makeMutation = () => {
133
150
options ?: MutationOptions < A , E , R , A2 , E2 , R2 , I >
134
151
) => {
135
152
const queryClient = useQueryClient ( )
136
- const state : Ref < Result . Result < A2 , E2 > > = ref < Result . Result < A2 , E2 > > ( Result . initial ( ) ) as any
137
153
138
154
const invalidateQueries = (
139
155
filters ?: MaybeRefDeep < InvalidateQueryFilters > ,
140
156
options ?: MaybeRefDeep < InvalidateOptions >
141
157
) => Effect . promise ( ( ) => queryClient . invalidateQueries ( filters , options ) )
142
158
143
- function handleExit ( exit : Exit . Exit < A2 , E2 > ) {
144
- return Effect . sync ( ( ) => {
145
- if ( Exit . isSuccess ( exit ) ) {
146
- state . value = Result . success ( exit . value )
147
- return
148
- }
149
-
150
- state . value = Result . failure ( exit . cause )
151
- } )
152
- }
153
-
154
159
const invalidateCache = Effect . suspend ( ( ) => {
155
160
const queryKey = getQueryKey ( self )
156
161
@@ -179,22 +184,16 @@ export const makeMutation = () => {
179
184
180
185
const mapHandler = options ?. mapHandler ?? ( ( _ ) => _ )
181
186
187
+ const [ state , handle_ ] = asResult ( ( self : Effect < A , E , R > , i : I | void = void 0 ) => ( mapHandler (
188
+ Effect . tapBoth ( self , { onFailure : ( ) => invalidateCache , onSuccess : ( ) => invalidateCache } ) ,
189
+ i as I
190
+ ) as Effect < A2 , E2 , R2 > ) )
191
+
182
192
const handle = ( self : Effect < A , E , R > , name : string , i : I | void = void 0 ) =>
183
- Effect
184
- . sync ( ( ) => {
185
- state . value = Result . initial ( true )
186
- } )
187
- . pipe (
188
- Effect . zipRight (
189
- mapHandler (
190
- Effect . tapBoth ( self , { onFailure : ( ) => invalidateCache , onSuccess : ( ) => invalidateCache } ) ,
191
- i as I
192
- ) as Effect < A2 , E2 , R2 >
193
- ) ,
194
- Effect . tapDefect ( reportRuntimeError ) ,
195
- Effect . onExit ( handleExit ) ,
196
- Effect . withSpan ( `mutation ${ name } ` , { captureStackTrace : false } )
197
- )
193
+ handle_ ( self , i ) . pipe (
194
+ Effect . tapDefect ( reportRuntimeError ) ,
195
+ Effect . withSpan ( `mutation ${ name } ` , { captureStackTrace : false } )
196
+ )
198
197
199
198
const handler = self . handler
200
199
const r = tuple (
0 commit comments