@effect-app/vue 4.0.0-beta.23 → 4.0.0-beta.231
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 +1700 -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 +79 -35
- package/dist/mutate.d.ts.map +1 -1
- package/dist/mutate.js +166 -49
- package/dist/query.d.ts +24 -39
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +156 -78
- 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 +300 -130
- package/src/query.ts +241 -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,23 +1,57 @@
|
|
|
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
|
-
export
|
|
16
|
+
export type GetQueryKey = (h: { id: string; options?: ClientForOptions }) => string[]
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Default heuristic: invalidate the parent namespace of the action.
|
|
20
|
+
* e.g. `$project/$configuration.get` -> `["$project"]`
|
|
21
|
+
* e.g. `$project/$configuration/$something.get` -> `["$project","$configuration"]`
|
|
22
|
+
*/
|
|
23
|
+
export const defaultGetQueryKey: GetQueryKey = (h) => {
|
|
12
24
|
const key = makeQueryKey(h)
|
|
13
25
|
const ns = key.filter((_) => _.startsWith("$"))
|
|
14
|
-
// we invalidate the parent namespace e.g $project/$configuration.get, we invalidate $project
|
|
15
|
-
// for $project/$configuration/$something.get, we invalidate $project/$configuration
|
|
16
26
|
const k = ns.length ? ns.length > 1 ? ns.slice(0, ns.length - 1) : ns : undefined
|
|
17
27
|
if (!k) throw new Error("empty query key for: " + h.id)
|
|
18
28
|
return k
|
|
19
29
|
}
|
|
20
30
|
|
|
31
|
+
let activeGetQueryKey: GetQueryKey = defaultGetQueryKey
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Override the default query-key heuristic used by mutations for cache
|
|
35
|
+
* invalidation. Call once at app bootstrap. Pass `undefined` to restore the
|
|
36
|
+
* built-in default.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* // invalidate the full namespace of the action (no parent collapse)
|
|
41
|
+
* setDefaultGetQueryKey((h) => {
|
|
42
|
+
* const key = makeQueryKey(h)
|
|
43
|
+
* const ns = key.filter((_) => _.startsWith("$"))
|
|
44
|
+
* if (!ns.length) throw new Error("empty query key for: " + h.id)
|
|
45
|
+
* return ns
|
|
46
|
+
* })
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export const setDefaultGetQueryKey = (fn: GetQueryKey | undefined) => {
|
|
50
|
+
activeGetQueryKey = fn ?? defaultGetQueryKey
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const getQueryKey: GetQueryKey = (h) => activeGetQueryKey(h)
|
|
54
|
+
|
|
21
55
|
export function mutationResultToVue<A, E>(
|
|
22
56
|
mutationResult: AsyncResult.AsyncResult<A, E>
|
|
23
57
|
): Res<A, E> {
|
|
@@ -67,87 +101,116 @@ export function make<A, E, R>(self: Effect.Effect<A, E, R>) {
|
|
|
67
101
|
return tuple(result, latestSuccess, execute)
|
|
68
102
|
}
|
|
69
103
|
|
|
70
|
-
export interface MutationOptionsBase {
|
|
104
|
+
export interface MutationOptionsBase<A = unknown, B = A, E2 = never, R2 = never> {
|
|
71
105
|
/**
|
|
72
106
|
* By default we invalidate one level of the query key, e.g $project/$configuration.get, we invalidate $project.
|
|
73
107
|
* This can be overridden by providing a function that returns an array of filters and options.
|
|
74
108
|
*/
|
|
75
|
-
queryInvalidation?: (defaultKey: string[], name: string) => {
|
|
109
|
+
queryInvalidation?: (defaultKey: string[], name: string, input?: unknown, output?: Exit.Exit<unknown, unknown>) => {
|
|
76
110
|
filters?: InvalidateQueryFilters | undefined
|
|
77
111
|
options?: InvalidateOptions | undefined
|
|
78
112
|
}[]
|
|
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
113
|
/**
|
|
84
|
-
*
|
|
85
|
-
*
|
|
114
|
+
* Run an additional Effect after the mutation succeeds. Its output becomes the
|
|
115
|
+
* final result returned to the caller. Query cache is invalidated once on
|
|
116
|
+
* mutation exit and again after this Effect completes. Useful for long-running
|
|
117
|
+
* operations (e.g. polling a background job) where you want the caller to
|
|
118
|
+
* receive the downstream result and the cache to refresh once it is ready.
|
|
86
119
|
*
|
|
87
|
-
* @
|
|
120
|
+
* @example
|
|
121
|
+
* ```ts
|
|
122
|
+
* useMutation(startExportCommand, {
|
|
123
|
+
* select: (result) => pollUntilDone(result.jobId)
|
|
124
|
+
* // caller receives the pollUntilDone output, not the original result
|
|
125
|
+
* })
|
|
126
|
+
* ```
|
|
88
127
|
*/
|
|
89
|
-
|
|
128
|
+
select?: (result: A) => Effect.Effect<B, E2, R2>
|
|
90
129
|
}
|
|
91
130
|
|
|
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
|
-
) => {
|
|
131
|
+
export const asResult = <Args extends readonly any[], A, E, R>(
|
|
132
|
+
handler: (...args: Args) => Effect.Effect<A, E, R>
|
|
133
|
+
): readonly [
|
|
134
|
+
ComputedRef<AsyncResult.AsyncResult<A, E>>,
|
|
135
|
+
(...args: Args) => Effect.Effect<Exit.Exit<A, E>, never, R>
|
|
136
|
+
] => {
|
|
113
137
|
const state = shallowRef<AsyncResult.AsyncResult<A, E>>(AsyncResult.initial())
|
|
114
138
|
|
|
115
|
-
const act =
|
|
116
|
-
|
|
139
|
+
const act = (...args: Args) =>
|
|
140
|
+
Effect
|
|
117
141
|
.sync(() => {
|
|
118
142
|
state.value = AsyncResult.initial(true)
|
|
119
143
|
})
|
|
120
144
|
.pipe(
|
|
121
145
|
Effect.andThen(Effect.suspend(() =>
|
|
122
|
-
handler.pipe(
|
|
146
|
+
handler(...args).pipe(
|
|
123
147
|
Effect.exit,
|
|
124
148
|
Effect.tap((exit) => Effect.sync(() => (state.value = AsyncResult.fromExit(exit))))
|
|
125
149
|
)
|
|
126
150
|
))
|
|
127
151
|
)
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
152
|
+
|
|
153
|
+
return tuple(computed(() => state.value), act) as any
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Like `asResult`, but for streams. The ref is updated with each emitted value
|
|
158
|
+
* (keeping `waiting: true`) and is finalised (with `waiting: false`) once the
|
|
159
|
+
* stream terminates successfully. Errors are surfaced as `AsyncResult.failure`.
|
|
160
|
+
*/
|
|
161
|
+
export const asStreamResult = <Args extends readonly any[], A, E, R>(
|
|
162
|
+
handler: (...args: Args) => Stream.Stream<A, E, R>
|
|
163
|
+
): readonly [ComputedRef<AsyncResult.AsyncResult<A, E>>, (...args: Args) => Effect.Effect<void, never, R>] => {
|
|
164
|
+
const state = shallowRef<AsyncResult.AsyncResult<A, E>>(AsyncResult.initial())
|
|
165
|
+
|
|
166
|
+
const runStream = (stream: Stream.Stream<A, E, R>): Effect.Effect<void, never, R> =>
|
|
167
|
+
Effect
|
|
168
|
+
.sync(() => {
|
|
169
|
+
state.value = AsyncResult.initial(true)
|
|
170
|
+
})
|
|
171
|
+
.pipe(
|
|
172
|
+
Effect.andThen(
|
|
173
|
+
stream.pipe(
|
|
174
|
+
Stream.runForEach((value) =>
|
|
175
|
+
Effect.sync(() => {
|
|
176
|
+
state.value = AsyncResult.success(value, { waiting: true })
|
|
177
|
+
})
|
|
178
|
+
),
|
|
179
|
+
Effect.exit,
|
|
180
|
+
Effect.flatMap((exit) =>
|
|
181
|
+
Effect.sync(() => {
|
|
182
|
+
if (exit._tag === "Success") {
|
|
183
|
+
const current = state.value
|
|
184
|
+
if (AsyncResult.isSuccess(current)) {
|
|
185
|
+
state.value = AsyncResult.success(current.value, { waiting: false })
|
|
186
|
+
} else {
|
|
187
|
+
state.value = AsyncResult.initial(false)
|
|
188
|
+
}
|
|
189
|
+
} else {
|
|
190
|
+
state.value = AsyncResult.failure(exit.cause)
|
|
191
|
+
}
|
|
192
|
+
})
|
|
138
193
|
)
|
|
139
|
-
)
|
|
194
|
+
)
|
|
140
195
|
)
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
const act = (...args: Args) => runStream(handler(...args))
|
|
141
199
|
|
|
142
200
|
return tuple(computed(() => state.value), act) as any
|
|
143
201
|
}
|
|
144
202
|
|
|
145
|
-
|
|
203
|
+
const buildInvalidateCache = (
|
|
146
204
|
queryClient: QueryClient,
|
|
147
205
|
self: { id: string; options?: ClientForOptions },
|
|
148
|
-
|
|
206
|
+
queryInvalidation?: MutationOptionsBase["queryInvalidation"]
|
|
149
207
|
) => {
|
|
150
|
-
|
|
208
|
+
type InvalidationTarget = {
|
|
209
|
+
readonly filters: InvalidateQueryFilters | undefined
|
|
210
|
+
readonly options: InvalidateOptions | undefined
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const invalidateQueriesFn = (
|
|
151
214
|
filters?: InvalidateQueryFilters,
|
|
152
215
|
options?: InvalidateOptions
|
|
153
216
|
) =>
|
|
@@ -158,71 +221,149 @@ export const invalidateQueries = (
|
|
|
158
221
|
)
|
|
159
222
|
)
|
|
160
223
|
|
|
161
|
-
const
|
|
224
|
+
const getClientInvalidationTargets = (
|
|
225
|
+
input: unknown,
|
|
226
|
+
output: Exit.Exit<unknown, unknown>
|
|
227
|
+
): ReadonlyArray<InvalidationTarget> => {
|
|
162
228
|
const queryKey = getQueryKey(self)
|
|
163
229
|
|
|
164
|
-
if (
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
230
|
+
if (queryInvalidation) {
|
|
231
|
+
return queryInvalidation(queryKey, self.id, input, output).map((_) => ({
|
|
232
|
+
filters: _.filters,
|
|
233
|
+
options: _.options
|
|
234
|
+
}))
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (!queryKey) {
|
|
238
|
+
return []
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return [{ filters: { queryKey }, options: undefined }]
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const invalidateCache = (
|
|
245
|
+
input: unknown,
|
|
246
|
+
output: Exit.Exit<unknown, unknown>,
|
|
247
|
+
serverKeys: ReadonlyArray<InvalidationKey>
|
|
248
|
+
) =>
|
|
249
|
+
Effect.suspend(() => {
|
|
250
|
+
const clientTargets = getClientInvalidationTargets(input, output)
|
|
251
|
+
const serverTargets: ReadonlyArray<InvalidationTarget> = serverKeys.map((queryKey) => ({
|
|
252
|
+
filters: { queryKey },
|
|
253
|
+
options: undefined
|
|
254
|
+
}))
|
|
255
|
+
const allTargets: ReadonlyArray<InvalidationTarget> = [...clientTargets, ...serverTargets]
|
|
256
|
+
|
|
257
|
+
if (!allTargets.length) return Effect.void
|
|
258
|
+
|
|
259
|
+
// Group targets by refetchType + options so each group can be merged into a single
|
|
260
|
+
// invalidateQueries call using a predicate, reducing N calls to 1 in the common case.
|
|
261
|
+
type Group = {
|
|
262
|
+
targets: Array<InvalidationTarget>
|
|
263
|
+
refetchType: InvalidateQueryFilters["refetchType"]
|
|
264
|
+
options: InvalidateOptions | undefined
|
|
168
265
|
}
|
|
266
|
+
const groups = new Map<string, Group>()
|
|
267
|
+
for (const target of allTargets) {
|
|
268
|
+
const key = `${target.filters?.refetchType ?? ""}|${target.options?.cancelRefetch ?? ""}|${
|
|
269
|
+
target.options?.throwOnError?.toString() ?? ""
|
|
270
|
+
}`
|
|
271
|
+
const existing = groups.get(key)
|
|
272
|
+
if (existing) {
|
|
273
|
+
existing.targets.push(target)
|
|
274
|
+
} else {
|
|
275
|
+
groups.set(key, { targets: [target], refetchType: target.filters?.refetchType, options: target.options })
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
169
279
|
return Effect
|
|
170
280
|
.andThen(
|
|
171
|
-
Effect.annotateCurrentSpan({
|
|
172
|
-
Effect.forEach(
|
|
281
|
+
Effect.annotateCurrentSpan({ clientTargets, serverKeys }),
|
|
282
|
+
Effect.forEach(
|
|
283
|
+
groups.values(),
|
|
284
|
+
({ options, refetchType, targets }) =>
|
|
285
|
+
invalidateQueriesFn(
|
|
286
|
+
{
|
|
287
|
+
...(refetchType !== undefined ? { refetchType } : {}),
|
|
288
|
+
predicate: (query) => targets.some((t) => t.filters ? matchQuery(t.filters, query) : true)
|
|
289
|
+
},
|
|
290
|
+
options
|
|
291
|
+
),
|
|
292
|
+
{ discard: true, concurrency: "inherit" }
|
|
293
|
+
)
|
|
173
294
|
)
|
|
174
|
-
.pipe(
|
|
175
|
-
|
|
295
|
+
.pipe(
|
|
296
|
+
Effect.tap(
|
|
297
|
+
// hand over control back to the event loop so that state can be updated..
|
|
298
|
+
// TODO: should we do this in general on any mutation, regardless of invalidation?
|
|
299
|
+
Effect.sleep(0)
|
|
300
|
+
),
|
|
301
|
+
Effect.withSpan("client.query.invalidation", {}, { captureStackTrace: false })
|
|
302
|
+
)
|
|
303
|
+
})
|
|
176
304
|
|
|
177
|
-
|
|
305
|
+
return invalidateCache
|
|
306
|
+
}
|
|
178
307
|
|
|
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
|
-
})
|
|
308
|
+
export const invalidateQueries = (
|
|
309
|
+
queryClient: QueryClient,
|
|
310
|
+
self: { id: string; options?: ClientForOptions },
|
|
311
|
+
options?: MutationOptionsBase
|
|
312
|
+
) => {
|
|
313
|
+
const invalidateCache = buildInvalidateCache(queryClient, self, options?.queryInvalidation)
|
|
314
|
+
|
|
315
|
+
const select = options?.select
|
|
193
316
|
|
|
194
|
-
const handle = <A, E, R>(
|
|
317
|
+
const handle = <A, E, R>(eff: Effect.Effect<A, E, R>, input?: unknown) =>
|
|
318
|
+
Effect.gen(function*() {
|
|
319
|
+
const keysRef = yield* Ref.make<ReadonlyArray<InvalidationKey>>([])
|
|
320
|
+
const result = yield* eff.pipe(
|
|
321
|
+
Effect.provideService(InvalidationKeysFromServer, makeInvalidationKeysService(keysRef)),
|
|
322
|
+
Effect.onExit((exit) =>
|
|
323
|
+
Effect.gen(function*() {
|
|
324
|
+
const serverKeys = yield* Ref.get(keysRef)
|
|
325
|
+
yield* invalidateCache(input, exit, serverKeys)
|
|
326
|
+
})
|
|
327
|
+
)
|
|
328
|
+
)
|
|
329
|
+
if (select) {
|
|
330
|
+
return yield* select(result).pipe(
|
|
331
|
+
Effect.onExit((exit) =>
|
|
332
|
+
Effect.gen(function*() {
|
|
333
|
+
const serverKeys = yield* Ref.get(keysRef)
|
|
334
|
+
yield* invalidateCache(input, exit, serverKeys)
|
|
335
|
+
})
|
|
336
|
+
)
|
|
337
|
+
)
|
|
338
|
+
}
|
|
339
|
+
return result
|
|
340
|
+
})
|
|
195
341
|
|
|
196
342
|
return handle
|
|
197
343
|
}
|
|
198
344
|
|
|
345
|
+
/**
|
|
346
|
+
* A callable mutation result. When `I = void` the input argument may be omitted.
|
|
347
|
+
*/
|
|
348
|
+
export interface MutationFn<I, A, E, R, Id extends string> {
|
|
349
|
+
<B = A, E2 = never, R2 = never>(
|
|
350
|
+
input: I,
|
|
351
|
+
options?: MutationOptionsBase<A, B, E2, R2>
|
|
352
|
+
): Effect.Effect<B, E | E2, R | R2>
|
|
353
|
+
readonly id: Id
|
|
354
|
+
}
|
|
355
|
+
|
|
199
356
|
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
|
-
) => {
|
|
357
|
+
/**
|
|
358
|
+
* Pass a function that returns an Effect, e.g from a client action.
|
|
359
|
+
* Executes query cache invalidation based on default rules or provided option.
|
|
360
|
+
* When `I = void` the input argument may be omitted.
|
|
361
|
+
*/
|
|
362
|
+
const useMutation = <I, E, A, R, Request extends Req, Id extends string>(
|
|
363
|
+
self: RequestHandlerWithInput<I, A, E, R, Request, Id>
|
|
364
|
+
): MutationFn<I, A, E, R, Id> => {
|
|
221
365
|
const queryClient = useQueryClient()
|
|
222
|
-
const
|
|
223
|
-
const handler = self.handler
|
|
224
|
-
const r = Effect.isEffect(handler) ? handle(handler) : (i: I) => handle(handler(i))
|
|
225
|
-
|
|
366
|
+
const r = (i: I, options?: MutationOptionsBase) => invalidateQueries(queryClient, self, options)(self.handler(i), i)
|
|
226
367
|
return Object.assign(r, { id: self.id }) as any
|
|
227
368
|
}
|
|
228
369
|
return useMutation
|
|
@@ -232,32 +373,61 @@ export const makeMutation = () => {
|
|
|
232
373
|
export const useMakeMutation = () => {
|
|
233
374
|
const queryClient = useQueryClient()
|
|
234
375
|
|
|
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
|
-
|
|
376
|
+
/**
|
|
377
|
+
* Pass a function that returns an Effect, e.g from a client action.
|
|
378
|
+
* Executes query cache invalidation based on default rules or provided option.
|
|
379
|
+
* When `I = void` the input argument may be omitted.
|
|
380
|
+
*/
|
|
381
|
+
const useMutation = <I, E, A, R, Request extends Req, Id extends string>(
|
|
382
|
+
self: RequestHandlerWithInput<I, A, E, R, Request, Id>
|
|
383
|
+
): MutationFn<I, A, E, R, Id> => {
|
|
384
|
+
const r = (i: I, options?: MutationOptionsBase) => invalidateQueries(queryClient, self, options)(self.handler(i), i)
|
|
260
385
|
return Object.assign(r, { id: self.id }) as any
|
|
261
386
|
}
|
|
262
387
|
return useMutation
|
|
263
388
|
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Returns a stream-based mutation factory for use with `streamFn`.
|
|
392
|
+
* The outer Effect sets up per-invocation invalidation scaffolding
|
|
393
|
+
* and returns a stream that triggers query invalidation via `Stream.ensuring` when it completes.
|
|
394
|
+
*
|
|
395
|
+
* Use with `streamFn` / `Command.streamFn(id)(mutateHandler, ...combinators)` so that
|
|
396
|
+
* the command manages its own reactive state internally.
|
|
397
|
+
*
|
|
398
|
+
* Must be called inside a Vue setup context (uses `useQueryClient` internally).
|
|
399
|
+
*/
|
|
400
|
+
export const makeStreamMutation2 = () => {
|
|
401
|
+
const queryClient = useQueryClient()
|
|
402
|
+
|
|
403
|
+
return (
|
|
404
|
+
self: {
|
|
405
|
+
id: string
|
|
406
|
+
options?: ClientForOptions
|
|
407
|
+
handler: (i: any) => Stream.Stream<any, any, any>
|
|
408
|
+
},
|
|
409
|
+
mergedInvalidation?: MutationOptionsBase["queryInvalidation"]
|
|
410
|
+
) => {
|
|
411
|
+
const invCache = buildInvalidateCache(queryClient, self, mergedInvalidation)
|
|
412
|
+
|
|
413
|
+
const makeInvocationEffect = (input: unknown, source: Stream.Stream<any, any, any>) =>
|
|
414
|
+
Effect.gen(function*() {
|
|
415
|
+
const keysRef = yield* Ref.make<ReadonlyArray<InvalidationKey>>([])
|
|
416
|
+
const invKeys = makeInvalidationKeysService(keysRef, (key) => invCache(input, Exit.succeed(undefined), [key]))
|
|
417
|
+
const lastRef = yield* Ref.make<any>(undefined)
|
|
418
|
+
return source.pipe(
|
|
419
|
+
Stream.provideService(InvalidationKeysFromServer, invKeys),
|
|
420
|
+
Stream.tap((v) => Ref.set(lastRef, v)),
|
|
421
|
+
Stream.ensuring(
|
|
422
|
+
Effect.gen(function*() {
|
|
423
|
+
const lastValue = yield* Ref.get(lastRef)
|
|
424
|
+
const serverKeys = yield* Ref.get(keysRef)
|
|
425
|
+
yield* invCache(input, Exit.succeed(lastValue), serverKeys)
|
|
426
|
+
})
|
|
427
|
+
)
|
|
428
|
+
)
|
|
429
|
+
})
|
|
430
|
+
|
|
431
|
+
return (i: any) => Stream.unwrap(makeInvocationEffect(i, self.handler(i)))
|
|
432
|
+
}
|
|
433
|
+
}
|