Skip to content

Commit 9fcd7dd

Browse files
Remove effects from injectMutation to avoid synchronisation issues
1 parent 5c6a9de commit 9fcd7dd

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

packages/angular-query-experimental/src/__tests__/inject-mutation.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,17 @@ describe('injectMutation', () => {
131131

132132
mutation.mutate('xyz')
133133

134-
const mutations = mutationCache.find({ mutationKey: ['2'] })
134+
mutationKey.set(['3'])
135135

136-
expect(mutations?.options.mutationKey).toEqual(['2'])
136+
mutation.mutate('xyz')
137+
138+
expect(mutationCache.find({ mutationKey: ['1'] })).toBeUndefined()
139+
expect(
140+
mutationCache.find({ mutationKey: ['2'] })?.options.mutationKey,
141+
).toEqual(['2'])
142+
expect(
143+
mutationCache.find({ mutationKey: ['3'] })?.options.mutationKey,
144+
).toEqual(['3'])
137145
})
138146

139147
test('should reset state after invoking mutation.reset', async () => {

packages/angular-query-experimental/src/inject-mutation.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
NgZone,
55
assertInInjectionContext,
66
computed,
7-
effect,
87
inject,
98
signal,
109
untracked,
@@ -71,7 +70,15 @@ export function injectMutation<
7170
null
7271

7372
return computed(() => {
74-
return (instance ||= new MutationObserver(queryClient, optionsSignal()))
73+
const observerOptions = optionsSignal()
74+
return untracked(() => {
75+
if (instance) {
76+
instance.setOptions(observerOptions)
77+
} else {
78+
instance = new MutationObserver(queryClient, observerOptions)
79+
}
80+
return instance
81+
})
7582
})
7683
})()
7784

@@ -125,20 +132,6 @@ export function injectMutation<
125132
return observer.getCurrentResult()
126133
})
127134

128-
effect(
129-
() => {
130-
const observer = observerSignal()
131-
const observerOptions = optionsSignal()
132-
133-
untracked(() => {
134-
observer.setOptions(observerOptions)
135-
})
136-
},
137-
{
138-
injector,
139-
},
140-
)
141-
142135
const resultSignal = computed(() => {
143136
const resultFromSubscriber = resultFromSubscriberSignal()
144137
const resultFromInitialOptions = resultFromInitialOptionsSignal()

0 commit comments

Comments
 (0)