Skip to content

Commit 0969cde

Browse files
committed
refactor(query): 💡 expose observable and signal in IsMutating
- Add teardown logic to dispose the internal subscription received from `MutationCache` - Add `takeUntilDestroyed` to dispose of the observable when the `DestroyRef` signals it
1 parent 08a62b4 commit 0969cde

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

query/src/lib/is-mutating.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,43 @@ import {
33
notifyManager
44
} from '@tanstack/query-core';
55
import { injectQueryClient } from './query-client';
6-
import { inject, Injectable, InjectionToken } from '@angular/core';
6+
import { DestroyRef, inject, Injectable, InjectionToken } from '@angular/core';
77
import { distinctUntilChanged, Observable } from 'rxjs';
8+
import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
89

910
@Injectable({ providedIn: 'root' })
10-
export class IsMutatingService {
11+
export class IsMutating {
1112
private queryClient = injectQueryClient();
13+
private destroyRef = inject(DestroyRef);
1214

1315
use(filters?: MutationFilters) {
14-
return new Observable<number>((observer) => {
16+
const result$ = new Observable<number>((observer) => {
1517
observer.next(this.queryClient.isMutating(filters));
16-
this.queryClient.getMutationCache().subscribe(
18+
const disposeSubscription = this.queryClient.getMutationCache().subscribe(
1719
notifyManager.batchCalls(() => {
1820
observer.next(this.queryClient.isMutating(filters));
1921
})
2022
);
21-
}).pipe(distinctUntilChanged());
23+
24+
return () => disposeSubscription();
25+
}).pipe(
26+
distinctUntilChanged(),
27+
takeUntilDestroyed(this.destroyRef)
28+
);
29+
30+
return {
31+
result$,
32+
toSignal: () => toSignal(result$),
33+
}
2234
}
2335
}
2436

25-
const UseIsMutating = new InjectionToken<IsMutatingService['use']>(
37+
const UseIsMutating = new InjectionToken<IsMutating['use']>(
2638
'UseIsFetching',
2739
{
2840
providedIn: 'root',
2941
factory() {
30-
const isMutating = new IsMutatingService();
42+
const isMutating = new IsMutating();
3143
return isMutating.use.bind(isMutating);
3244
},
3345
}

0 commit comments

Comments
 (0)