-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from luii/feat/injectIs-helpers
feat(query): 🔥 add `injectIsMutating` and `injectIsFetching`
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { notifyManager, type QueryFilters } from '@tanstack/query-core'; | ||
import { injectQueryClient } from './query-client'; | ||
import { inject, Injectable, InjectionToken } from '@angular/core'; | ||
import { distinctUntilChanged, Observable } from 'rxjs'; | ||
import { toSignal } from '@angular/core/rxjs-interop'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class IsFetching { | ||
private queryClient = injectQueryClient(); | ||
|
||
use(filters?: QueryFilters) { | ||
const result$ = new Observable<number>((observer) => { | ||
observer.next(this.queryClient.isFetching(filters)); | ||
const disposeSubscription = this.queryClient.getQueryCache().subscribe( | ||
notifyManager.batchCalls(() => { | ||
observer.next(this.queryClient.isFetching(filters)); | ||
}) | ||
); | ||
|
||
return () => disposeSubscription(); | ||
}).pipe(distinctUntilChanged()); | ||
|
||
return { | ||
result$, | ||
toSignal: () => toSignal(result$), | ||
}; | ||
} | ||
} | ||
|
||
const UseIsFetching = new InjectionToken<IsFetching['use']>('UseIsFetching', { | ||
providedIn: 'root', | ||
factory() { | ||
const isFetching = new IsFetching(); | ||
return isFetching.use.bind(isFetching); | ||
}, | ||
}); | ||
|
||
export function injectIsFetching() { | ||
return inject(UseIsFetching); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { type MutationFilters, notifyManager } from '@tanstack/query-core'; | ||
import { injectQueryClient } from './query-client'; | ||
import { inject, Injectable, InjectionToken } from '@angular/core'; | ||
import { distinctUntilChanged, Observable } from 'rxjs'; | ||
import { toSignal } from '@angular/core/rxjs-interop'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class IsMutating { | ||
private queryClient = injectQueryClient(); | ||
|
||
use(filters?: MutationFilters) { | ||
const result$ = new Observable<number>((observer) => { | ||
observer.next(this.queryClient.isMutating(filters)); | ||
const disposeSubscription = this.queryClient.getMutationCache().subscribe( | ||
notifyManager.batchCalls(() => { | ||
observer.next(this.queryClient.isMutating(filters)); | ||
}) | ||
); | ||
|
||
return () => disposeSubscription(); | ||
}).pipe(distinctUntilChanged()); | ||
|
||
return { | ||
result$, | ||
toSignal: () => toSignal(result$), | ||
}; | ||
} | ||
} | ||
|
||
const UseIsMutating = new InjectionToken<IsMutating['use']>('UseIsFetching', { | ||
providedIn: 'root', | ||
factory() { | ||
const isMutating = new IsMutating(); | ||
return isMutating.use.bind(isMutating); | ||
}, | ||
}); | ||
|
||
export function injectIsMutating() { | ||
return inject(UseIsMutating); | ||
} |