@effect-app/vue 4.0.0-beta.22 → 4.0.0-beta.221
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1613 -0
- package/dist/commander.d.ts +634 -0
- package/dist/commander.d.ts.map +1 -0
- package/dist/commander.js +1070 -0
- package/dist/confirm.d.ts +21 -0
- package/dist/confirm.d.ts.map +1 -0
- package/dist/confirm.js +26 -0
- package/dist/errorReporter.d.ts +7 -5
- package/dist/errorReporter.d.ts.map +1 -1
- package/dist/errorReporter.js +14 -19
- package/dist/form.d.ts +15 -6
- package/dist/form.d.ts.map +1 -1
- package/dist/form.js +46 -13
- package/dist/index.d.ts +1 -1
- package/dist/intl.d.ts +15 -0
- package/dist/intl.d.ts.map +1 -0
- package/dist/intl.js +9 -0
- package/dist/lib.d.ts +8 -10
- package/dist/lib.d.ts.map +1 -1
- package/dist/lib.js +35 -10
- package/dist/makeClient.d.ts +156 -339
- package/dist/makeClient.d.ts.map +1 -1
- package/dist/makeClient.js +225 -376
- package/dist/makeContext.d.ts +1 -1
- package/dist/makeContext.d.ts.map +1 -1
- package/dist/makeIntl.d.ts +1 -1
- package/dist/makeIntl.d.ts.map +1 -1
- package/dist/makeUseCommand.d.ts +9 -0
- package/dist/makeUseCommand.d.ts.map +1 -0
- package/dist/makeUseCommand.js +13 -0
- package/dist/mutate.d.ts +54 -34
- package/dist/mutate.d.ts.map +1 -1
- package/dist/mutate.js +139 -46
- package/dist/query.d.ts +20 -39
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +133 -72
- package/dist/routeParams.d.ts +3 -2
- package/dist/routeParams.d.ts.map +1 -1
- package/dist/routeParams.js +4 -3
- package/dist/runtime.d.ts +10 -6
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +32 -18
- package/dist/toast.d.ts +51 -0
- package/dist/toast.d.ts.map +1 -0
- package/dist/toast.js +34 -0
- package/dist/withToast.d.ts +30 -0
- package/dist/withToast.d.ts.map +1 -0
- package/dist/withToast.js +64 -0
- package/examples/streamMutation.ts +72 -0
- package/package.json +48 -50
- package/src/commander.ts +3406 -0
- package/src/{experimental/confirm.ts → confirm.ts} +12 -14
- package/src/errorReporter.ts +65 -75
- package/src/form.ts +61 -18
- package/src/intl.ts +12 -0
- package/src/lib.ts +48 -20
- package/src/makeClient.ts +574 -1134
- package/src/{experimental/makeUseCommand.ts → makeUseCommand.ts} +8 -5
- package/src/mutate.ts +268 -127
- package/src/query.ts +203 -183
- package/src/routeParams.ts +3 -2
- package/src/runtime.ts +46 -21
- package/src/{experimental/toast.ts → toast.ts} +15 -27
- package/src/{experimental/withToast.ts → withToast.ts} +46 -12
- package/test/Mutation.test.ts +181 -24
- package/test/dist/form.test.d.ts.map +1 -1
- package/test/dist/lib.test.d.ts.map +1 -0
- package/test/dist/streamFinal.test.d.ts.map +1 -0
- package/test/dist/streamFn.test.d.ts.map +1 -0
- package/test/dist/stubs.d.ts +3531 -122
- package/test/dist/stubs.d.ts.map +1 -1
- package/test/dist/stubs.js +187 -32
- package/test/form-validation-errors.test.ts +25 -20
- package/test/form.test.ts +22 -3
- package/test/lib.test.ts +240 -0
- package/test/makeClient.test.ts +292 -38
- package/test/streamFinal.test.ts +64 -0
- package/test/streamFn.test.ts +457 -0
- package/test/stubs.ts +223 -43
- package/tsconfig.examples.json +20 -0
- package/tsconfig.json +0 -1
- package/tsconfig.json.bak +5 -2
- package/tsconfig.src.json +34 -34
- package/tsconfig.test.json +2 -2
- package/vitest.config.ts +5 -5
- package/dist/experimental/commander.d.ts +0 -359
- package/dist/experimental/commander.d.ts.map +0 -1
- package/dist/experimental/commander.js +0 -557
- package/dist/experimental/confirm.d.ts +0 -19
- package/dist/experimental/confirm.d.ts.map +0 -1
- package/dist/experimental/confirm.js +0 -28
- package/dist/experimental/intl.d.ts +0 -16
- package/dist/experimental/intl.d.ts.map +0 -1
- package/dist/experimental/intl.js +0 -5
- package/dist/experimental/makeUseCommand.d.ts +0 -8
- package/dist/experimental/makeUseCommand.d.ts.map +0 -1
- package/dist/experimental/makeUseCommand.js +0 -13
- package/dist/experimental/toast.d.ts +0 -47
- package/dist/experimental/toast.d.ts.map +0 -1
- package/dist/experimental/toast.js +0 -41
- package/dist/experimental/withToast.d.ts +0 -25
- package/dist/experimental/withToast.d.ts.map +0 -1
- package/dist/experimental/withToast.js +0 -45
- package/eslint.config.mjs +0 -24
- package/src/experimental/commander.ts +0 -1835
- package/src/experimental/intl.ts +0 -9
|
@@ -1,26 +1,29 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as Effect from "effect-app/Effect"
|
|
2
|
+
import type * as Layer from "effect-app/Layer"
|
|
2
3
|
import { Commander, type CommanderImpl, CommanderStatic } from "./commander.js"
|
|
3
4
|
|
|
4
5
|
type X<X> = X
|
|
5
6
|
|
|
6
7
|
// helps retain JSDoc
|
|
7
8
|
export interface CommanderResolved<RT, RTHooks>
|
|
8
|
-
extends
|
|
9
|
+
extends
|
|
10
|
+
X<typeof CommanderStatic>,
|
|
11
|
+
Pick<CommanderImpl<RT, RTHooks>, "fn" | "wrap" | "streamWrap" | "streamFn" | "alt" | "alt2">
|
|
9
12
|
{
|
|
10
13
|
}
|
|
11
14
|
|
|
12
15
|
export const makeUseCommand = Effect.fnUntraced(
|
|
13
16
|
function*<R = never, RTHooks = never>(rtHooks: Layer.Layer<RTHooks, never, R>) {
|
|
14
17
|
const cmndr = yield* Commander
|
|
15
|
-
const runtime = yield* Effect.
|
|
18
|
+
const runtime = yield* Effect.context<R>()
|
|
16
19
|
|
|
17
20
|
const comm = cmndr(runtime, rtHooks)
|
|
18
21
|
|
|
19
|
-
const command = {
|
|
22
|
+
const command: CommanderResolved<R, RTHooks> = {
|
|
20
23
|
...comm,
|
|
21
24
|
...CommanderStatic
|
|
22
25
|
}
|
|
23
26
|
|
|
24
|
-
return command
|
|
27
|
+
return command
|
|
25
28
|
}
|
|
26
29
|
)
|
package/src/mutate.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { matchQuery } from "@tanstack/query-core"
|
|
2
3
|
import { type InvalidateOptions, type InvalidateQueryFilters, type QueryClient, useQueryClient } from "@tanstack/vue-query"
|
|
3
|
-
import { type
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
4
|
+
import { type InvalidationKey, InvalidationKeysFromServer, makeInvalidationKeysService, makeQueryKey, type Req } from "effect-app/client"
|
|
5
|
+
import type { ClientForOptions, RequestHandlerWithInput } from "effect-app/client/clientFor"
|
|
6
|
+
import * as Effect from "effect-app/Effect"
|
|
6
7
|
import { tuple } from "effect-app/Function"
|
|
8
|
+
import * as Option from "effect-app/Option"
|
|
9
|
+
import type * as Cause from "effect/Cause"
|
|
10
|
+
import * as Exit from "effect/Exit"
|
|
11
|
+
import * as Ref from "effect/Ref"
|
|
12
|
+
import * as Stream from "effect/Stream"
|
|
7
13
|
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"
|
|
8
14
|
import { computed, type ComputedRef, shallowRef } from "vue"
|
|
9
|
-
import { makeQueryKey } from "./lib.js"
|
|
10
15
|
|
|
11
16
|
export const getQueryKey = (h: { id: string; options?: ClientForOptions }) => {
|
|
12
17
|
const key = makeQueryKey(h)
|
|
@@ -67,87 +72,116 @@ export function make<A, E, R>(self: Effect.Effect<A, E, R>) {
|
|
|
67
72
|
return tuple(result, latestSuccess, execute)
|
|
68
73
|
}
|
|
69
74
|
|
|
70
|
-
export interface MutationOptionsBase {
|
|
75
|
+
export interface MutationOptionsBase<A = unknown, B = A, E2 = never, R2 = never> {
|
|
71
76
|
/**
|
|
72
77
|
* By default we invalidate one level of the query key, e.g $project/$configuration.get, we invalidate $project.
|
|
73
78
|
* This can be overridden by providing a function that returns an array of filters and options.
|
|
74
79
|
*/
|
|
75
|
-
queryInvalidation?: (defaultKey: string[], name: string) => {
|
|
80
|
+
queryInvalidation?: (defaultKey: string[], name: string, input?: unknown, output?: Exit.Exit<unknown, unknown>) => {
|
|
76
81
|
filters?: InvalidateQueryFilters | undefined
|
|
77
82
|
options?: InvalidateOptions | undefined
|
|
78
83
|
}[]
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** @deprecated prefer more basic @see MutationOptionsBase and separate useMutation from Command.fn */
|
|
82
|
-
export interface MutationOptions<A, E, R, A2 = A, E2 = E, R2 = R, I = void> extends MutationOptionsBase {
|
|
83
84
|
/**
|
|
84
|
-
*
|
|
85
|
-
*
|
|
85
|
+
* Run an additional Effect after the mutation succeeds. Its output becomes the
|
|
86
|
+
* final result returned to the caller. Query cache is invalidated once on
|
|
87
|
+
* mutation exit and again after this Effect completes. Useful for long-running
|
|
88
|
+
* operations (e.g. polling a background job) where you want the caller to
|
|
89
|
+
* receive the downstream result and the cache to refresh once it is ready.
|
|
86
90
|
*
|
|
87
|
-
* @
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* useMutation(startExportCommand, {
|
|
94
|
+
* select: (result) => pollUntilDone(result.jobId)
|
|
95
|
+
* // caller receives the pollUntilDone output, not the original result
|
|
96
|
+
* })
|
|
97
|
+
* ```
|
|
88
98
|
*/
|
|
89
|
-
|
|
99
|
+
select?: (result: A) => Effect.Effect<B, E2, R2>
|
|
90
100
|
}
|
|
91
101
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
// for (let i = 0; i < ns.length; i++) {
|
|
99
|
-
// nses.push(ns.slice(0, i + 1).join("/"))
|
|
100
|
-
// }
|
|
101
|
-
*/
|
|
102
|
-
|
|
103
|
-
export const asResult: {
|
|
104
|
-
<A, E, R>(
|
|
105
|
-
handler: Effect.Effect<A, E, R>
|
|
106
|
-
): readonly [ComputedRef<AsyncResult.AsyncResult<A, E>>, Effect.Effect<Exit.Exit<A, E>, never, R>]
|
|
107
|
-
<Args extends readonly any[], A, E, R>(
|
|
108
|
-
handler: (...args: Args) => Effect.Effect<A, E, R>
|
|
109
|
-
): readonly [ComputedRef<AsyncResult.AsyncResult<A, E>>, (...args: Args) => Effect.Effect<Exit.Exit<A, E>, never, R>]
|
|
110
|
-
} = <Args extends readonly any[], A, E, R>(
|
|
111
|
-
handler: Effect.Effect<A, E, R> | ((...args: Args) => Effect.Effect<A, E, R>)
|
|
112
|
-
) => {
|
|
102
|
+
export const asResult = <Args extends readonly any[], A, E, R>(
|
|
103
|
+
handler: (...args: Args) => Effect.Effect<A, E, R>
|
|
104
|
+
): readonly [
|
|
105
|
+
ComputedRef<AsyncResult.AsyncResult<A, E>>,
|
|
106
|
+
(...args: Args) => Effect.Effect<Exit.Exit<A, E>, never, R>
|
|
107
|
+
] => {
|
|
113
108
|
const state = shallowRef<AsyncResult.AsyncResult<A, E>>(AsyncResult.initial())
|
|
114
109
|
|
|
115
|
-
const act =
|
|
116
|
-
|
|
110
|
+
const act = (...args: Args) =>
|
|
111
|
+
Effect
|
|
117
112
|
.sync(() => {
|
|
118
113
|
state.value = AsyncResult.initial(true)
|
|
119
114
|
})
|
|
120
115
|
.pipe(
|
|
121
116
|
Effect.andThen(Effect.suspend(() =>
|
|
122
|
-
handler.pipe(
|
|
117
|
+
handler(...args).pipe(
|
|
123
118
|
Effect.exit,
|
|
124
119
|
Effect.tap((exit) => Effect.sync(() => (state.value = AsyncResult.fromExit(exit))))
|
|
125
120
|
)
|
|
126
121
|
))
|
|
127
122
|
)
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
123
|
+
|
|
124
|
+
return tuple(computed(() => state.value), act) as any
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Like `asResult`, but for streams. The ref is updated with each emitted value
|
|
129
|
+
* (keeping `waiting: true`) and is finalised (with `waiting: false`) once the
|
|
130
|
+
* stream terminates successfully. Errors are surfaced as `AsyncResult.failure`.
|
|
131
|
+
*/
|
|
132
|
+
export const asStreamResult = <Args extends readonly any[], A, E, R>(
|
|
133
|
+
handler: (...args: Args) => Stream.Stream<A, E, R>
|
|
134
|
+
): readonly [ComputedRef<AsyncResult.AsyncResult<A, E>>, (...args: Args) => Effect.Effect<void, never, R>] => {
|
|
135
|
+
const state = shallowRef<AsyncResult.AsyncResult<A, E>>(AsyncResult.initial())
|
|
136
|
+
|
|
137
|
+
const runStream = (stream: Stream.Stream<A, E, R>): Effect.Effect<void, never, R> =>
|
|
138
|
+
Effect
|
|
139
|
+
.sync(() => {
|
|
140
|
+
state.value = AsyncResult.initial(true)
|
|
141
|
+
})
|
|
142
|
+
.pipe(
|
|
143
|
+
Effect.andThen(
|
|
144
|
+
stream.pipe(
|
|
145
|
+
Stream.runForEach((value) =>
|
|
146
|
+
Effect.sync(() => {
|
|
147
|
+
state.value = AsyncResult.success(value, { waiting: true })
|
|
148
|
+
})
|
|
149
|
+
),
|
|
150
|
+
Effect.exit,
|
|
151
|
+
Effect.flatMap((exit) =>
|
|
152
|
+
Effect.sync(() => {
|
|
153
|
+
if (exit._tag === "Success") {
|
|
154
|
+
const current = state.value
|
|
155
|
+
if (AsyncResult.isSuccess(current)) {
|
|
156
|
+
state.value = AsyncResult.success(current.value, { waiting: false })
|
|
157
|
+
} else {
|
|
158
|
+
state.value = AsyncResult.initial(false)
|
|
159
|
+
}
|
|
160
|
+
} else {
|
|
161
|
+
state.value = AsyncResult.failure(exit.cause)
|
|
162
|
+
}
|
|
163
|
+
})
|
|
138
164
|
)
|
|
139
|
-
)
|
|
165
|
+
)
|
|
140
166
|
)
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
const act = (...args: Args) => runStream(handler(...args))
|
|
141
170
|
|
|
142
171
|
return tuple(computed(() => state.value), act) as any
|
|
143
172
|
}
|
|
144
173
|
|
|
145
|
-
|
|
174
|
+
const buildInvalidateCache = (
|
|
146
175
|
queryClient: QueryClient,
|
|
147
176
|
self: { id: string; options?: ClientForOptions },
|
|
148
|
-
|
|
177
|
+
queryInvalidation?: MutationOptionsBase["queryInvalidation"]
|
|
149
178
|
) => {
|
|
150
|
-
|
|
179
|
+
type InvalidationTarget = {
|
|
180
|
+
readonly filters: InvalidateQueryFilters | undefined
|
|
181
|
+
readonly options: InvalidateOptions | undefined
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const invalidateQueriesFn = (
|
|
151
185
|
filters?: InvalidateQueryFilters,
|
|
152
186
|
options?: InvalidateOptions
|
|
153
187
|
) =>
|
|
@@ -158,71 +192,149 @@ export const invalidateQueries = (
|
|
|
158
192
|
)
|
|
159
193
|
)
|
|
160
194
|
|
|
161
|
-
const
|
|
195
|
+
const getClientInvalidationTargets = (
|
|
196
|
+
input: unknown,
|
|
197
|
+
output: Exit.Exit<unknown, unknown>
|
|
198
|
+
): ReadonlyArray<InvalidationTarget> => {
|
|
162
199
|
const queryKey = getQueryKey(self)
|
|
163
200
|
|
|
164
|
-
if (
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
201
|
+
if (queryInvalidation) {
|
|
202
|
+
return queryInvalidation(queryKey, self.id, input, output).map((_) => ({
|
|
203
|
+
filters: _.filters,
|
|
204
|
+
options: _.options
|
|
205
|
+
}))
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (!queryKey) {
|
|
209
|
+
return []
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return [{ filters: { queryKey }, options: undefined }]
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const invalidateCache = (
|
|
216
|
+
input: unknown,
|
|
217
|
+
output: Exit.Exit<unknown, unknown>,
|
|
218
|
+
serverKeys: ReadonlyArray<InvalidationKey>
|
|
219
|
+
) =>
|
|
220
|
+
Effect.suspend(() => {
|
|
221
|
+
const clientTargets = getClientInvalidationTargets(input, output)
|
|
222
|
+
const serverTargets: ReadonlyArray<InvalidationTarget> = serverKeys.map((queryKey) => ({
|
|
223
|
+
filters: { queryKey },
|
|
224
|
+
options: undefined
|
|
225
|
+
}))
|
|
226
|
+
const allTargets: ReadonlyArray<InvalidationTarget> = [...clientTargets, ...serverTargets]
|
|
227
|
+
|
|
228
|
+
if (!allTargets.length) return Effect.void
|
|
229
|
+
|
|
230
|
+
// Group targets by refetchType + options so each group can be merged into a single
|
|
231
|
+
// invalidateQueries call using a predicate, reducing N calls to 1 in the common case.
|
|
232
|
+
type Group = {
|
|
233
|
+
targets: Array<InvalidationTarget>
|
|
234
|
+
refetchType: InvalidateQueryFilters["refetchType"]
|
|
235
|
+
options: InvalidateOptions | undefined
|
|
236
|
+
}
|
|
237
|
+
const groups = new Map<string, Group>()
|
|
238
|
+
for (const target of allTargets) {
|
|
239
|
+
const key = `${target.filters?.refetchType ?? ""}|${target.options?.cancelRefetch ?? ""}|${
|
|
240
|
+
target.options?.throwOnError?.toString() ?? ""
|
|
241
|
+
}`
|
|
242
|
+
const existing = groups.get(key)
|
|
243
|
+
if (existing) {
|
|
244
|
+
existing.targets.push(target)
|
|
245
|
+
} else {
|
|
246
|
+
groups.set(key, { targets: [target], refetchType: target.filters?.refetchType, options: target.options })
|
|
247
|
+
}
|
|
168
248
|
}
|
|
249
|
+
|
|
169
250
|
return Effect
|
|
170
251
|
.andThen(
|
|
171
|
-
Effect.annotateCurrentSpan({
|
|
172
|
-
Effect.forEach(
|
|
252
|
+
Effect.annotateCurrentSpan({ clientTargets, serverKeys }),
|
|
253
|
+
Effect.forEach(
|
|
254
|
+
groups.values(),
|
|
255
|
+
({ options, refetchType, targets }) =>
|
|
256
|
+
invalidateQueriesFn(
|
|
257
|
+
{
|
|
258
|
+
...(refetchType !== undefined ? { refetchType } : {}),
|
|
259
|
+
predicate: (query) => targets.some((t) => t.filters ? matchQuery(t.filters, query) : true)
|
|
260
|
+
},
|
|
261
|
+
options
|
|
262
|
+
),
|
|
263
|
+
{ discard: true, concurrency: "inherit" }
|
|
264
|
+
)
|
|
173
265
|
)
|
|
174
|
-
.pipe(
|
|
175
|
-
|
|
266
|
+
.pipe(
|
|
267
|
+
Effect.tap(
|
|
268
|
+
// hand over control back to the event loop so that state can be updated..
|
|
269
|
+
// TODO: should we do this in general on any mutation, regardless of invalidation?
|
|
270
|
+
Effect.sleep(0)
|
|
271
|
+
),
|
|
272
|
+
Effect.withSpan("client.query.invalidation", {}, { captureStackTrace: false })
|
|
273
|
+
)
|
|
274
|
+
})
|
|
176
275
|
|
|
177
|
-
|
|
276
|
+
return invalidateCache
|
|
277
|
+
}
|
|
178
278
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
// TODO: should we do this in general on any mutation, regardless of invalidation?
|
|
188
|
-
Effect.sleep(0)
|
|
189
|
-
),
|
|
190
|
-
Effect.withSpan("client.query.invalidation", {}, { captureStackTrace: false })
|
|
191
|
-
)
|
|
192
|
-
})
|
|
279
|
+
export const invalidateQueries = (
|
|
280
|
+
queryClient: QueryClient,
|
|
281
|
+
self: { id: string; options?: ClientForOptions },
|
|
282
|
+
options?: MutationOptionsBase
|
|
283
|
+
) => {
|
|
284
|
+
const invalidateCache = buildInvalidateCache(queryClient, self, options?.queryInvalidation)
|
|
285
|
+
|
|
286
|
+
const select = options?.select
|
|
193
287
|
|
|
194
|
-
const handle = <A, E, R>(
|
|
288
|
+
const handle = <A, E, R>(eff: Effect.Effect<A, E, R>, input?: unknown) =>
|
|
289
|
+
Effect.gen(function*() {
|
|
290
|
+
const keysRef = yield* Ref.make<ReadonlyArray<InvalidationKey>>([])
|
|
291
|
+
const result = yield* eff.pipe(
|
|
292
|
+
Effect.provideService(InvalidationKeysFromServer, makeInvalidationKeysService(keysRef)),
|
|
293
|
+
Effect.onExit((exit) =>
|
|
294
|
+
Effect.gen(function*() {
|
|
295
|
+
const serverKeys = yield* Ref.get(keysRef)
|
|
296
|
+
yield* invalidateCache(input, exit, serverKeys)
|
|
297
|
+
})
|
|
298
|
+
)
|
|
299
|
+
)
|
|
300
|
+
if (select) {
|
|
301
|
+
return yield* select(result).pipe(
|
|
302
|
+
Effect.onExit((exit) =>
|
|
303
|
+
Effect.gen(function*() {
|
|
304
|
+
const serverKeys = yield* Ref.get(keysRef)
|
|
305
|
+
yield* invalidateCache(input, exit, serverKeys)
|
|
306
|
+
})
|
|
307
|
+
)
|
|
308
|
+
)
|
|
309
|
+
}
|
|
310
|
+
return result
|
|
311
|
+
})
|
|
195
312
|
|
|
196
313
|
return handle
|
|
197
314
|
}
|
|
198
315
|
|
|
316
|
+
/**
|
|
317
|
+
* A callable mutation result. When `I = void` the input argument may be omitted.
|
|
318
|
+
*/
|
|
319
|
+
export interface MutationFn<I, A, E, R, Id extends string> {
|
|
320
|
+
<B = A, E2 = never, R2 = never>(
|
|
321
|
+
input: I,
|
|
322
|
+
options?: MutationOptionsBase<A, B, E2, R2>
|
|
323
|
+
): Effect.Effect<B, E | E2, R | R2>
|
|
324
|
+
readonly id: Id
|
|
325
|
+
}
|
|
326
|
+
|
|
199
327
|
export const makeMutation = () => {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
): ((i: I) => Effect.Effect<A, E, R>) & { readonly id: Id }
|
|
209
|
-
/**
|
|
210
|
-
* Pass an Effect, e.g from a client action
|
|
211
|
-
* Executes query cache invalidation based on default rules or provided option.
|
|
212
|
-
*/
|
|
213
|
-
<E, A, R, Request extends Req, Id extends string>(
|
|
214
|
-
self: RequestHandler<A, E, R, Request, Id>,
|
|
215
|
-
options?: MutationOptionsBase
|
|
216
|
-
): Effect.Effect<A, E, R> & { readonly id: Id }
|
|
217
|
-
} = <I, E, A, R, Request extends Req, Id extends string>(
|
|
218
|
-
self: RequestHandlerWithInput<I, A, E, R, Request, Id> | RequestHandler<A, E, R, Request, Id>,
|
|
219
|
-
options?: MutationOptionsBase
|
|
220
|
-
) => {
|
|
328
|
+
/**
|
|
329
|
+
* Pass a function that returns an Effect, e.g from a client action.
|
|
330
|
+
* Executes query cache invalidation based on default rules or provided option.
|
|
331
|
+
* When `I = void` the input argument may be omitted.
|
|
332
|
+
*/
|
|
333
|
+
const useMutation = <I, E, A, R, Request extends Req, Id extends string>(
|
|
334
|
+
self: RequestHandlerWithInput<I, A, E, R, Request, Id>
|
|
335
|
+
): MutationFn<I, A, E, R, Id> => {
|
|
221
336
|
const queryClient = useQueryClient()
|
|
222
|
-
const
|
|
223
|
-
const handler = self.handler
|
|
224
|
-
const r = Effect.isEffect(handler) ? handle(handler) : (i: I) => handle(handler(i))
|
|
225
|
-
|
|
337
|
+
const r = (i: I, options?: MutationOptionsBase) => invalidateQueries(queryClient, self, options)(self.handler(i), i)
|
|
226
338
|
return Object.assign(r, { id: self.id }) as any
|
|
227
339
|
}
|
|
228
340
|
return useMutation
|
|
@@ -232,32 +344,61 @@ export const makeMutation = () => {
|
|
|
232
344
|
export const useMakeMutation = () => {
|
|
233
345
|
const queryClient = useQueryClient()
|
|
234
346
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Pass an Effect, e.g from a client action
|
|
246
|
-
* Executes query cache invalidation based on default rules or provided option.
|
|
247
|
-
*/
|
|
248
|
-
<E, A, R, Request extends Req, Id extends string>(
|
|
249
|
-
self: RequestHandler<A, E, R, Request, Id>,
|
|
250
|
-
options?: MutationOptionsBase
|
|
251
|
-
): Effect.Effect<A, E, R> & { readonly id: Id }
|
|
252
|
-
} = <I, E, A, R, Request extends Req, Id extends string>(
|
|
253
|
-
self: RequestHandlerWithInput<I, A, E, R, Request, Id> | RequestHandler<A, E, R, Request, Id>,
|
|
254
|
-
options?: MutationOptionsBase
|
|
255
|
-
) => {
|
|
256
|
-
const handle = invalidateQueries(queryClient, self, options?.queryInvalidation)
|
|
257
|
-
const handler = self.handler
|
|
258
|
-
const r = Effect.isEffect(handler) ? handle(handler) : (i: I) => handle(handler(i))
|
|
259
|
-
|
|
347
|
+
/**
|
|
348
|
+
* Pass a function that returns an Effect, e.g from a client action.
|
|
349
|
+
* Executes query cache invalidation based on default rules or provided option.
|
|
350
|
+
* When `I = void` the input argument may be omitted.
|
|
351
|
+
*/
|
|
352
|
+
const useMutation = <I, E, A, R, Request extends Req, Id extends string>(
|
|
353
|
+
self: RequestHandlerWithInput<I, A, E, R, Request, Id>
|
|
354
|
+
): MutationFn<I, A, E, R, Id> => {
|
|
355
|
+
const r = (i: I, options?: MutationOptionsBase) => invalidateQueries(queryClient, self, options)(self.handler(i), i)
|
|
260
356
|
return Object.assign(r, { id: self.id }) as any
|
|
261
357
|
}
|
|
262
358
|
return useMutation
|
|
263
359
|
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Returns a stream-based mutation factory for use with `streamFn`.
|
|
363
|
+
* The outer Effect sets up per-invocation invalidation scaffolding
|
|
364
|
+
* and returns a stream that triggers query invalidation via `Stream.ensuring` when it completes.
|
|
365
|
+
*
|
|
366
|
+
* Use with `streamFn` / `Command.streamFn(id)(mutateHandler, ...combinators)` so that
|
|
367
|
+
* the command manages its own reactive state internally.
|
|
368
|
+
*
|
|
369
|
+
* Must be called inside a Vue setup context (uses `useQueryClient` internally).
|
|
370
|
+
*/
|
|
371
|
+
export const makeStreamMutation2 = () => {
|
|
372
|
+
const queryClient = useQueryClient()
|
|
373
|
+
|
|
374
|
+
return (
|
|
375
|
+
self: {
|
|
376
|
+
id: string
|
|
377
|
+
options?: ClientForOptions
|
|
378
|
+
handler: (i: any) => Stream.Stream<any, any, any>
|
|
379
|
+
},
|
|
380
|
+
mergedInvalidation?: MutationOptionsBase["queryInvalidation"]
|
|
381
|
+
) => {
|
|
382
|
+
const invCache = buildInvalidateCache(queryClient, self, mergedInvalidation)
|
|
383
|
+
|
|
384
|
+
const makeInvocationEffect = (input: unknown, source: Stream.Stream<any, any, any>) =>
|
|
385
|
+
Effect.gen(function*() {
|
|
386
|
+
const keysRef = yield* Ref.make<ReadonlyArray<InvalidationKey>>([])
|
|
387
|
+
const invKeys = makeInvalidationKeysService(keysRef, (key) => invCache(input, Exit.succeed(undefined), [key]))
|
|
388
|
+
const lastRef = yield* Ref.make<any>(undefined)
|
|
389
|
+
return source.pipe(
|
|
390
|
+
Stream.provideService(InvalidationKeysFromServer, invKeys),
|
|
391
|
+
Stream.tap((v) => Ref.set(lastRef, v)),
|
|
392
|
+
Stream.ensuring(
|
|
393
|
+
Effect.gen(function*() {
|
|
394
|
+
const lastValue = yield* Ref.get(lastRef)
|
|
395
|
+
const serverKeys = yield* Ref.get(keysRef)
|
|
396
|
+
yield* invCache(input, Exit.succeed(lastValue), serverKeys)
|
|
397
|
+
})
|
|
398
|
+
)
|
|
399
|
+
)
|
|
400
|
+
})
|
|
401
|
+
|
|
402
|
+
return (i: any) => Stream.unwrap(makeInvocationEffect(i, self.handler(i)))
|
|
403
|
+
}
|
|
404
|
+
}
|