@tanstack/query-core 4.10.3 → 4.12.0

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/query-core",
3
- "version": "4.10.3",
4
- "description": "TODO",
3
+ "version": "4.12.0",
4
+ "description": "The framework agnostic core that powers TanStack Query",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
7
7
  "repository": "tanstack/query",
@@ -29,6 +29,8 @@
29
29
  ],
30
30
  "scripts": {
31
31
  "clean": "rm -rf ./build",
32
- "test:eslint": "../../node_modules/.bin/eslint --ext .ts,.tsx ./src"
32
+ "test:eslint": "../../node_modules/.bin/eslint --ext .ts,.tsx ./src",
33
+ "test:jest": "../../node_modules/.bin/jest --config ./jest.config.ts",
34
+ "test:dev": "pnpm run test:jest --watch"
33
35
  }
34
36
  }
package/src/mutation.ts CHANGED
@@ -193,7 +193,7 @@ export class Mutation<
193
193
  if (!restored) {
194
194
  this.dispatch({ type: 'loading', variables: this.options.variables! })
195
195
  // Notify cache callback
196
- this.mutationCache.config.onMutate?.(
196
+ await this.mutationCache.config.onMutate?.(
197
197
  this.state.variables,
198
198
  this as Mutation<unknown, unknown, unknown, unknown>,
199
199
  )
@@ -209,7 +209,7 @@ export class Mutation<
209
209
  const data = await executeMutation()
210
210
 
211
211
  // Notify cache callback
212
- this.mutationCache.config.onSuccess?.(
212
+ await this.mutationCache.config.onSuccess?.(
213
213
  data,
214
214
  this.state.variables,
215
215
  this.state.context,
@@ -234,7 +234,7 @@ export class Mutation<
234
234
  } catch (error) {
235
235
  try {
236
236
  // Notify cache callback
237
- this.mutationCache.config.onError?.(
237
+ await this.mutationCache.config.onError?.(
238
238
  error,
239
239
  this.state.variables,
240
240
  this.state.context,
@@ -16,17 +16,17 @@ interface MutationCacheConfig {
16
16
  variables: unknown,
17
17
  context: unknown,
18
18
  mutation: Mutation<unknown, unknown, unknown>,
19
- ) => void
19
+ ) => Promise<unknown> | unknown
20
20
  onSuccess?: (
21
21
  data: unknown,
22
22
  variables: unknown,
23
23
  context: unknown,
24
24
  mutation: Mutation<unknown, unknown, unknown>,
25
- ) => void
25
+ ) => Promise<unknown> | unknown
26
26
  onMutate?: (
27
27
  variables: unknown,
28
28
  mutation: Mutation<unknown, unknown, unknown, unknown>,
29
- ) => void
29
+ ) => Promise<unknown> | unknown
30
30
  }
31
31
 
32
32
  interface NotifyEventMutationAdded {
@@ -22,6 +22,33 @@ describe('mutationCache', () => {
22
22
  const mutation = testCache.getAll()[0]
23
23
  expect(onError).toHaveBeenCalledWith('error', 'vars', 'context', mutation)
24
24
  })
25
+
26
+ test('should be awaited', async () => {
27
+ const key = queryKey()
28
+ const states: Array<number> = []
29
+ const onError = async () => {
30
+ states.push(1)
31
+ await sleep(1)
32
+ states.push(2)
33
+ }
34
+ const testCache = new MutationCache({ onError })
35
+ const testClient = createQueryClient({ mutationCache: testCache })
36
+
37
+ try {
38
+ await executeMutation(testClient, {
39
+ mutationKey: key,
40
+ variables: 'vars',
41
+ mutationFn: () => Promise.reject('error'),
42
+ onError: async () => {
43
+ states.push(3)
44
+ await sleep(1)
45
+ states.push(4)
46
+ },
47
+ })
48
+ } catch {}
49
+
50
+ expect(states).toEqual([1, 2, 3, 4])
51
+ })
25
52
  })
26
53
  describe('MutationCacheConfig.onSuccess', () => {
27
54
  test('should be called when a mutation is successful', async () => {
@@ -47,6 +74,30 @@ describe('mutationCache', () => {
47
74
  mutation,
48
75
  )
49
76
  })
77
+ test('should be awaited', async () => {
78
+ const key = queryKey()
79
+ const states: Array<number> = []
80
+ const onSuccess = async () => {
81
+ states.push(1)
82
+ await sleep(1)
83
+ states.push(2)
84
+ }
85
+ const testCache = new MutationCache({ onSuccess })
86
+ const testClient = createQueryClient({ mutationCache: testCache })
87
+
88
+ await executeMutation(testClient, {
89
+ mutationKey: key,
90
+ variables: 'vars',
91
+ mutationFn: () => Promise.resolve({ data: 5 }),
92
+ onSuccess: async () => {
93
+ states.push(3)
94
+ await sleep(1)
95
+ states.push(4)
96
+ },
97
+ })
98
+
99
+ expect(states).toEqual([1, 2, 3, 4])
100
+ })
50
101
  })
51
102
  describe('MutationCacheConfig.onMutate', () => {
52
103
  test('should be called before a mutation executes', async () => {
@@ -67,6 +118,31 @@ describe('mutationCache', () => {
67
118
  const mutation = testCache.getAll()[0]
68
119
  expect(onMutate).toHaveBeenCalledWith('vars', mutation)
69
120
  })
121
+
122
+ test('should be awaited', async () => {
123
+ const key = queryKey()
124
+ const states: Array<number> = []
125
+ const onMutate = async () => {
126
+ states.push(1)
127
+ await sleep(1)
128
+ states.push(2)
129
+ }
130
+ const testCache = new MutationCache({ onMutate })
131
+ const testClient = createQueryClient({ mutationCache: testCache })
132
+
133
+ await executeMutation(testClient, {
134
+ mutationKey: key,
135
+ variables: 'vars',
136
+ mutationFn: () => Promise.resolve({ data: 5 }),
137
+ onMutate: async () => {
138
+ states.push(3)
139
+ await sleep(1)
140
+ states.push(4)
141
+ },
142
+ })
143
+
144
+ expect(states).toEqual([1, 2, 3, 4])
145
+ })
70
146
  })
71
147
 
72
148
  describe('find', () => {
@@ -65,11 +65,13 @@ export const executeMutation = (
65
65
  // so that we can pretend to be in a server environment
66
66
  export function setIsServer(isServer: boolean) {
67
67
  const original = utils.isServer
68
- // @ts-ignore
69
- utils.isServer = isServer
68
+ Object.defineProperty(utils, 'isServer', {
69
+ get: () => isServer,
70
+ })
70
71
 
71
72
  return () => {
72
- // @ts-ignore
73
- utils.isServer = original
73
+ Object.defineProperty(utils, 'isServer', {
74
+ get: () => original,
75
+ })
74
76
  }
75
77
  }