Skip to content

Commit 0f3a56c

Browse files
committed
fix: use global options for mutations
1 parent d083bc8 commit 0f3a56c

File tree

6 files changed

+73
-31
lines changed

6 files changed

+73
-31
lines changed

devtools/src/pc-devtools-info-plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export function createMutationEntryPayload(entry: UseMutationEntry): UseMutation
234234
when: entry.when,
235235
vars: entry.vars,
236236
options: entry.options && {
237-
gcTime: entry.options.gcTime === false ? undefined : entry.options.gcTime,
237+
gcTime: entry.options.gcTime,
238238
},
239239
gcTimeout: typeof entry.gcTimeout === 'number' ? (entry.gcTimeout as number) : null,
240240
active: entry[DEVTOOLS_INFO_KEY].inactiveAt === 0,

src/mutation-options.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ export const USE_MUTATION_DEFAULTS = {
107107

108108
export type UseMutationOptionsWithDefaults<
109109
TData = unknown,
110+
TVars = void,
110111
TError = ErrorDefault,
111-
TDataInitial extends TData | undefined = undefined,
112-
> = UseMutationOptions<TData, TError, TDataInitial> & typeof USE_MUTATION_DEFAULTS
112+
TContext extends Record<any, any> = _EmptyObject,
113+
> = UseMutationOptions<TData, TVars, TError, TContext> & typeof USE_MUTATION_DEFAULTS
113114

114115
/**
115116
* Options to create a mutation.
@@ -249,5 +250,5 @@ export const USE_MUTATION_OPTIONS_KEY: InjectionKey<UseMutationOptionsGlobalDefa
249250
*
250251
* @internal
251252
*/
252-
export const useMutationOptions = (): UseMutationOptionsGlobal =>
253+
export const useMutationOptions = (): UseMutationOptionsGlobalDefaults =>
253254
inject(USE_MUTATION_OPTIONS_KEY, USE_MUTATION_DEFAULTS)

src/mutation-store.spec.ts

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { createPinia, getActivePinia, setActivePinia } from 'pinia'
22
import { describe, beforeEach, it, expect, vi } from 'vitest'
33
import { createApp } from 'vue'
44
import { useMutationCache, isMutationCache } from './mutation-store'
5-
import type { UseMutationEntry, MutationCache } from './mutation-store'
5+
import type { UseMutationEntry } from './mutation-store'
66
import { flushPromises } from '@vue/test-utils'
7-
import type { UseMutationOptions } from './mutation-options'
7+
import {
8+
USE_MUTATION_DEFAULTS,
9+
type UseMutationOptions,
10+
type UseMutationOptionsWithDefaults,
11+
} from './mutation-options'
812
import { mockConsoleError, mockWarn } from '../test-utils/mock-warn'
913
import { useMutation } from './use-mutation'
1014

@@ -21,9 +25,10 @@ describe('Mutation Cache store', () => {
2125
const mutationCache = useMutationCache()
2226
for (const key of keys) {
2327
const options = {
28+
...USE_MUTATION_DEFAULTS,
2429
key,
2530
mutation: async () => 'ok',
26-
} satisfies UseMutationOptions
31+
} satisfies UseMutationOptionsWithDefaults
2732
const entry = mutationCache.create(options)
2833
mutationCache.ensure(entry, undefined)
2934
}
@@ -116,8 +121,9 @@ describe('Mutation Cache store', () => {
116121
const mutationCache = useMutationCache()
117122

118123
const options = {
124+
...USE_MUTATION_DEFAULTS,
119125
mutation: async () => 'test',
120-
} satisfies UseMutationOptions
126+
} satisfies UseMutationOptionsWithDefaults
121127

122128
const entry = mutationCache.create(options)
123129

@@ -133,6 +139,7 @@ describe('Mutation Cache store', () => {
133139
const mutationCache = useMutationCache()
134140
const e1 = mutationCache.ensure(
135141
mutationCache.create({
142+
...USE_MUTATION_DEFAULTS,
136143
key: ['a', 'b', 'c'],
137144
mutation: async () => 'abc',
138145
}),
@@ -142,6 +149,7 @@ describe('Mutation Cache store', () => {
142149

143150
const e2 = mutationCache.ensure(
144151
mutationCache.create({
152+
...USE_MUTATION_DEFAULTS,
145153
key: ['a', 'b', 'd'],
146154
mutation: async () => 'abd',
147155
}),
@@ -151,6 +159,7 @@ describe('Mutation Cache store', () => {
151159

152160
const e3 = mutationCache.ensure(
153161
mutationCache.create({
162+
...USE_MUTATION_DEFAULTS,
154163
key: ['a', 'b'],
155164
mutation: async () => 'ab',
156165
}),
@@ -168,6 +177,7 @@ describe('Mutation Cache store', () => {
168177
const mutationCache = useMutationCache()
169178
const entry = mutationCache.ensure(
170179
mutationCache.create({
180+
...USE_MUTATION_DEFAULTS,
171181
key: ['a', 'b', 'c'],
172182
mutation: async () => 'abc',
173183
}),
@@ -208,7 +218,10 @@ describe('Mutation Cache store', () => {
208218
describe('extensibility', () => {
209219
it('has an ext property that starts empty', () => {
210220
const mutationCache = useMutationCache()
211-
const entry = mutationCache.create({ mutation: async () => 'ok' })
221+
const entry = mutationCache.create({
222+
...USE_MUTATION_DEFAULTS,
223+
mutation: async () => 'ok',
224+
})
212225
expect(entry.ext).toBeDefined()
213226
})
214227

@@ -223,7 +236,10 @@ describe('Mutation Cache store', () => {
223236
}
224237
})
225238

226-
const entry = mutationCache.create({ mutation: async () => 'ok' })
239+
const entry = mutationCache.create({
240+
...USE_MUTATION_DEFAULTS,
241+
mutation: async () => 'ok',
242+
})
227243
mutationCache.ensure(entry, undefined)
228244

229245
expect(extendSpy).toHaveBeenCalledTimes(1)
@@ -240,10 +256,16 @@ describe('Mutation Cache store', () => {
240256
}
241257
})
242258

243-
const entry = mutationCache.create({ mutation: async () => 'ok' })
259+
const entry = mutationCache.create({
260+
...USE_MUTATION_DEFAULTS,
261+
mutation: async () => 'ok',
262+
})
244263
mutationCache.ensure(entry, undefined)
245264
// Ensure the same entry again
246-
const entry2 = mutationCache.create({ mutation: async () => 'ok' })
265+
const entry2 = mutationCache.create({
266+
...USE_MUTATION_DEFAULTS,
267+
mutation: async () => 'ok',
268+
})
247269
mutationCache.ensure(entry2, undefined)
248270

249271
// extend should be called twice (once per unique entry)
@@ -262,7 +284,10 @@ describe('Mutation Cache store', () => {
262284
}
263285
})
264286

265-
const entry = mutationCache.create({ mutation: async () => 'ok' })
287+
const entry = mutationCache.create({
288+
...USE_MUTATION_DEFAULTS,
289+
mutation: async () => 'ok',
290+
})
266291
mutationCache.ensure(entry, undefined)
267292

268293
expect(entry.ext).toHaveProperty('customProperty', 'test-value')
@@ -300,6 +325,7 @@ describe('Mutation Cache store', () => {
300325
const mutationCache = useMutationCache()
301326
const entry = mutationCache.ensure(
302327
mutationCache.create({
328+
...USE_MUTATION_DEFAULTS,
303329
key: ['a', 'b', 'c'],
304330
mutation: async () => 'abc',
305331
}),
@@ -313,9 +339,10 @@ describe('Mutation Cache store', () => {
313339
it('can store and retrieve multiple entries with the same user key', () => {
314340
const mutationCache = useMutationCache()
315341
const options = {
342+
...USE_MUTATION_DEFAULTS,
316343
key: ['same', 'key'],
317344
mutation: async () => 'ok',
318-
} satisfies UseMutationOptions
345+
} satisfies UseMutationOptionsWithDefaults
319346

320347
const entry1 = mutationCache.ensure(mutationCache.create(options), undefined)
321348
const entry2 = mutationCache.ensure(mutationCache.create(options), undefined)
@@ -342,11 +369,17 @@ describe('Mutation Cache store', () => {
342369
const mutationCache = useMutationCache()
343370

344371
const entry1 = mutationCache.ensure(
345-
mutationCache.create({ mutation: async () => 'ok' }),
372+
mutationCache.create({
373+
...USE_MUTATION_DEFAULTS,
374+
mutation: async () => 'ok',
375+
}),
346376
undefined,
347377
)
348378
const entry2 = mutationCache.ensure(
349-
mutationCache.create({ mutation: async () => 'ok' }),
379+
mutationCache.create({
380+
...USE_MUTATION_DEFAULTS,
381+
mutation: async () => 'ok',
382+
}),
350383
undefined,
351384
)
352385

src/mutation-store.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import type { ShallowRef } from 'vue'
22
import type { AsyncStatus, DataState } from './data-state'
33
import { defineStore, skipHydrate } from 'pinia'
44
import { customRef, getCurrentScope, hasInjectionContext, shallowRef } from 'vue'
5-
import { find, START_EXT, toCacheKey } from './entry-keys'
5+
import { find, START_EXT } from './entry-keys'
66
import type { EntryFilter } from './entry-filter'
77
import type { _EmptyObject } from './utils'
88
import { noop, toValueWithArgs, warnOnce } from './utils'
99
import type { _ReduceContext } from './use-mutation'
1010
import { useMutationOptions } from './mutation-options'
11-
import type { UseMutationOptions } from './mutation-options'
11+
import type { UseMutationOptions, UseMutationOptionsWithDefaults } from './mutation-options'
1212
import type { EntryKey } from './entry-keys'
1313

1414
/**
@@ -68,7 +68,7 @@ export interface UseMutationEntry<
6868
/**
6969
* Options used to create the mutation.
7070
*/
71-
options: UseMutationOptions<TData, TVars, TError, TContext>
71+
options: UseMutationOptionsWithDefaults<TData, TVars, TError, TContext>
7272

7373
/**
7474
* Timeout id that scheduled a garbage collection. It is set here to clear it when the entry is used by a different component.
@@ -154,7 +154,7 @@ export const useMutationCache = /* @__PURE__ */ defineStore(MUTATION_STORE_ID, (
154154
TError = unknown,
155155
TContext extends Record<any, any> = _EmptyObject,
156156
>(
157-
options: UseMutationOptions<TData, TVars, TError, TContext>,
157+
options: UseMutationOptionsWithDefaults<TData, TVars, TError, TContext>,
158158
key?: EntryKey | undefined,
159159
vars?: TVars,
160160
): UseMutationEntry<TData, TVars, TError, TContext> =>

src/use-mutation.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import { useMutationCache } from './mutation-store'
1414
import type { UseMutationEntry } from './mutation-store'
1515
import { noop } from './utils'
1616
import type { _EmptyObject } from './utils'
17-
import { useMutationOptions, type UseMutationOptions } from './mutation-options'
17+
import {
18+
USE_MUTATION_DEFAULTS,
19+
useMutationOptions,
20+
type UseMutationOptions,
21+
} from './mutation-options'
1822

1923
/**
2024
* Valid keys for a mutation. Similar to query keys.
@@ -147,13 +151,17 @@ export function useMutation<
147151
const mutationCache = useMutationCache()
148152
const hasCurrentInstance = getCurrentInstance()
149153
const currentEffect = getCurrentScope()
154+
const optionDefaults = useMutationOptions()
150155

151-
// FIXME: take into account global options
152-
// const optionDefaults = useMutationOptions()
156+
const mergedOptions = {
157+
// NOTE: overriding the hooks, makes the types too complex to infer properly
158+
...(optionDefaults as typeof USE_MUTATION_DEFAULTS),
159+
...options,
160+
}
153161

154162
// always create an initial entry with no key (cannot be computed without vars)
155163
const entry = shallowRef<UseMutationEntry<TData, TVars, TError, TContext>>(
156-
mutationCache.create(options),
164+
mutationCache.create(mergedOptions),
157165
)
158166

159167
// Untrack the mutation entry when component or effect scope is disposed
@@ -187,7 +195,7 @@ export function useMutation<
187195
}
188196

189197
function reset() {
190-
entry.value = mutationCache.create(options)
198+
entry.value = mutationCache.create(mergedOptions)
191199
}
192200

193201
return {

src/use-query.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ export function useQuery<
161161
(params: unknown) => DefineQueryOptions<TData, TError, TDataInitial>,
162162
paramsGetter?: MaybeRefOrGetter<unknown>,
163163
]
164-
// _options:
165-
// | UseQueryOptions<TData, TError, TDataInitial>
166-
// | (() => DefineQueryOptions<TData, TError, TDataInitial>)
167-
// | ((params: unknown) => DefineQueryOptions<TData, TError, TDataInitial>),
168-
// paramsGetter?: MaybeRefOrGetter<unknown>,
169-
): UseQueryReturn<TData, TError, TDataInitial> {
164+
) // _options:
165+
// | UseQueryOptions<TData, TError, TDataInitial>
166+
// | (() => DefineQueryOptions<TData, TError, TDataInitial>)
167+
// | ((params: unknown) => DefineQueryOptions<TData, TError, TDataInitial>),
168+
// paramsGetter?: MaybeRefOrGetter<unknown>,
169+
: UseQueryReturn<TData, TError, TDataInitial> {
170170
if (paramsGetter != null) {
171171
return useQuery(() =>
172172
// NOTE: we manually type cast here because TS cannot infer correctly in overloads

0 commit comments

Comments
 (0)