Skip to content

Commit 9185c11

Browse files
committed
extract "asResult"
1 parent cb64a52 commit 9185c11

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

.changeset/serious-mails-hammer.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@effect-app/vue": minor
3+
---
4+
5+
add "asResult"

packages/vue/src/mutate.ts

+30-31
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import * as Result from "@effect-rx/rx/Result"
33
import type { InvalidateOptions, InvalidateQueryFilters } from "@tanstack/vue-query"
44
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"
67
import type { RequestHandler, RequestHandlerWithInput, TaggedRequestClassAny } from "effect-app/client/clientFor"
78
import { tuple } from "effect-app/Function"
89
import type { ComputedRef, Ref } from "vue"
9-
import { computed, ref, shallowRef } from "vue"
10+
import { computed, shallowRef } from "vue"
1011
import { makeQueryKey, reportRuntimeError } from "./lib.js"
1112

1213
export const getQueryKey = (h: { name: string }) => {
@@ -69,8 +70,6 @@ export function make<A, E, R>(self: Effect<A, E, R>) {
6970
return tuple(result, latestSuccess, execute)
7071
}
7172

72-
73-
7473
export type MaybeRef<T> = Ref<T> | ComputedRef<T> | T
7574
type MaybeRefDeep<T> = MaybeRef<
7675
// 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> {
108107
// }
109108
*/
110109

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+
111128
export const makeMutation = () => {
112129
/**
113130
* Pass a function that returns an Effect, e.g from a client action, or an Effect
@@ -133,24 +150,12 @@ export const makeMutation = () => {
133150
options?: MutationOptions<A, E, R, A2, E2, R2, I>
134151
) => {
135152
const queryClient = useQueryClient()
136-
const state: Ref<Result.Result<A2, E2>> = ref<Result.Result<A2, E2>>(Result.initial()) as any
137153

138154
const invalidateQueries = (
139155
filters?: MaybeRefDeep<InvalidateQueryFilters>,
140156
options?: MaybeRefDeep<InvalidateOptions>
141157
) => Effect.promise(() => queryClient.invalidateQueries(filters, options))
142158

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-
154159
const invalidateCache = Effect.suspend(() => {
155160
const queryKey = getQueryKey(self)
156161

@@ -179,22 +184,16 @@ export const makeMutation = () => {
179184

180185
const mapHandler = options?.mapHandler ?? ((_) => _)
181186

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+
182192
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+
)
198197

199198
const handler = self.handler
200199
const r = tuple(

0 commit comments

Comments
 (0)