Skip to content

Commit 5c6a9de

Browse files
fix(angular-query): ensure initial mutation pending state is emitted
1 parent 1b9837f commit 5c6a9de

File tree

2 files changed

+31
-41
lines changed

2 files changed

+31
-41
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ describe('injectMutation', () => {
5858
}))
5959
})
6060

61-
TestBed.tick()
62-
6361
mutation.mutate(result)
6462
vi.advanceTimersByTime(1)
6563

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

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,6 @@ export function injectMutation<
8484
}
8585
})
8686

87-
/**
88-
* Computed signal that gets result from mutation cache based on passed options
89-
*/
90-
const resultFromInitialOptionsSignal = computed(() => {
91-
const observer = observerSignal()
92-
return observer.getCurrentResult()
93-
})
94-
9587
/**
9688
* Signal that contains result set by subscriber
9789
*/
@@ -102,44 +94,44 @@ export function injectMutation<
10294
TContext
10395
> | null>(null)
10496

105-
effect(
106-
() => {
107-
const observer = observerSignal()
108-
const observerOptions = optionsSignal()
97+
/**
98+
* Computed signal that gets result from mutation cache based on passed options
99+
*/
100+
const resultFromInitialOptionsSignal = computed(() => {
101+
const observer = observerSignal()
109102

110-
untracked(() => {
111-
observer.setOptions(observerOptions)
112-
})
113-
},
114-
{
115-
injector,
116-
},
117-
)
103+
untracked(() => {
104+
const unsubscribe = ngZone.runOutsideAngular(() =>
105+
// observer.trackResult is not used as this optimization is not needed for Angular
106+
observer.subscribe(
107+
notifyManager.batchCalls((state) => {
108+
ngZone.run(() => {
109+
if (
110+
state.isError &&
111+
shouldThrowError(observer.options.throwOnError, [state.error])
112+
) {
113+
ngZone.onError.emit(state.error)
114+
throw state.error
115+
}
116+
117+
resultFromSubscriberSignal.set(state)
118+
})
119+
}),
120+
),
121+
)
122+
destroyRef.onDestroy(unsubscribe)
123+
})
124+
125+
return observer.getCurrentResult()
126+
})
118127

119128
effect(
120129
() => {
121-
// observer.trackResult is not used as this optimization is not needed for Angular
122130
const observer = observerSignal()
131+
const observerOptions = optionsSignal()
123132

124133
untracked(() => {
125-
const unsubscribe = ngZone.runOutsideAngular(() =>
126-
observer.subscribe(
127-
notifyManager.batchCalls((state) => {
128-
ngZone.run(() => {
129-
if (
130-
state.isError &&
131-
shouldThrowError(observer.options.throwOnError, [state.error])
132-
) {
133-
ngZone.onError.emit(state.error)
134-
throw state.error
135-
}
136-
137-
resultFromSubscriberSignal.set(state)
138-
})
139-
}),
140-
),
141-
)
142-
destroyRef.onDestroy(unsubscribe)
134+
observer.setOptions(observerOptions)
143135
})
144136
},
145137
{

0 commit comments

Comments
 (0)