Skip to content

Commit b4722f2

Browse files
fix(angular-query): ensure initial mutation pending state is emitted
1 parent 18af643 commit b4722f2

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
@@ -62,8 +62,6 @@ describe('injectMutation', () => {
6262
}))
6363
})
6464

65-
TestBed.flushEffects()
66-
6765
mutation.mutate(result)
6866
vi.advanceTimersByTime(1)
6967

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

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

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

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

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

118127
effect(
119128
() => {
120-
// observer.trackResult is not used as this optimization is not needed for Angular
121129
const observer = observerSignal()
130+
const observerOptions = optionsSignal()
122131

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

0 commit comments

Comments
 (0)