From 711c853b3628a80e2a519422a2c075b84d1ee336 Mon Sep 17 00:00:00 2001 From: Philipp Czarnetzki Date: Sun, 5 Nov 2023 09:03:29 +0100 Subject: [PATCH 1/6] =?UTF-8?q?feat(query):=20=F0=9F=94=A5=20add=20`inject?= =?UTF-8?q?IsFetching`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- query/src/lib/is-fetching.ts | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 query/src/lib/is-fetching.ts diff --git a/query/src/lib/is-fetching.ts b/query/src/lib/is-fetching.ts new file mode 100644 index 0000000..41885a4 --- /dev/null +++ b/query/src/lib/is-fetching.ts @@ -0,0 +1,38 @@ +import { QueryFilters, notifyManager } from '@tanstack/query-core'; +import { injectQueryClient } from './query-client'; +import { inject, Injectable, InjectionToken } from '@angular/core'; +import { distinctUntilChanged, Observable } from 'rxjs'; + +@Injectable({ providedIn: 'root' }) +export class IsFetchingService { + private queryClient = injectQueryClient(); + + use(filters?: QueryFilters) { + return new Observable((observer) => { + observer.next(this.queryClient.isFetching(filters)); + this.queryClient.getQueryCache().subscribe( + notifyManager.batchCalls(() => { + observer.next(this.queryClient.isFetching(filters)); + }) + ); + }).pipe(distinctUntilChanged()); + } +} + +const UseIsFetching = new InjectionToken( + 'UseIsFetching', + { + providedIn: 'root', + factory() { + const isFetching = new IsFetchingService(); + return isFetching.use.bind(isFetching); + }, + } +); + +export function injectIsFetching(toSignal: boolean = false) { + return inject(UseIsFetching); +} + +const isFetching = injectIsFetching(); +isFetching() \ No newline at end of file From 05eb7497edd427b4777fee65eb967fdb915f7f06 Mon Sep 17 00:00:00 2001 From: Philipp Czarnetzki Date: Sun, 5 Nov 2023 09:03:53 +0100 Subject: [PATCH 2/6] =?UTF-8?q?feat(query):=20=F0=9F=94=A5=20add=20`inject?= =?UTF-8?q?IsMutating`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- query/src/lib/is-mutating.ts | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 query/src/lib/is-mutating.ts diff --git a/query/src/lib/is-mutating.ts b/query/src/lib/is-mutating.ts new file mode 100644 index 0000000..bcf26e1 --- /dev/null +++ b/query/src/lib/is-mutating.ts @@ -0,0 +1,38 @@ +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'; + +@Injectable({ providedIn: 'root' }) +export class IsMutatingService { + private queryClient = injectQueryClient(); + + use(filters?: MutationFilters) { + return new Observable((observer) => { + observer.next(this.queryClient.isMutating(filters)); + this.queryClient.getMutationCache().subscribe( + notifyManager.batchCalls(() => { + observer.next(this.queryClient.isMutating(filters)); + }) + ); + }).pipe(distinctUntilChanged()); + } +} + +const UseIsMutating = new InjectionToken( + 'UseIsFetching', + { + providedIn: 'root', + factory() { + const isMutating = new IsMutatingService(); + return isMutating.use.bind(isMutating); + }, + } +); + +export function injectIsMutating() { + return inject(UseIsMutating); +} From 08a62b4c76511c226173c562994e0c1d3f9e506f Mon Sep 17 00:00:00 2001 From: Philipp Czarnetzki Date: Sun, 5 Nov 2023 09:08:33 +0100 Subject: [PATCH 3/6] =?UTF-8?q?chore(query):=20=F0=9F=A4=96=20remove=20unn?= =?UTF-8?q?eeded=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- query/src/lib/is-fetching.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/query/src/lib/is-fetching.ts b/query/src/lib/is-fetching.ts index 41885a4..46d10c8 100644 --- a/query/src/lib/is-fetching.ts +++ b/query/src/lib/is-fetching.ts @@ -30,9 +30,6 @@ const UseIsFetching = new InjectionToken( } ); -export function injectIsFetching(toSignal: boolean = false) { +export function injectIsFetching() { return inject(UseIsFetching); } - -const isFetching = injectIsFetching(); -isFetching() \ No newline at end of file From 0969cdefa9caa959ad14ddc9429703f4d5d14461 Mon Sep 17 00:00:00 2001 From: Philipp Czarnetzki Date: Sun, 5 Nov 2023 10:34:04 +0100 Subject: [PATCH 4/6] =?UTF-8?q?refactor(query):=20=F0=9F=92=A1=20expose=20?= =?UTF-8?q?observable=20and=20signal=20in=20`IsMutating`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add teardown logic to dispose the internal subscription received from `MutationCache` - Add `takeUntilDestroyed` to dispose of the observable when the `DestroyRef` signals it --- query/src/lib/is-mutating.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/query/src/lib/is-mutating.ts b/query/src/lib/is-mutating.ts index bcf26e1..35ed890 100644 --- a/query/src/lib/is-mutating.ts +++ b/query/src/lib/is-mutating.ts @@ -3,31 +3,43 @@ import { notifyManager } from '@tanstack/query-core'; import { injectQueryClient } from './query-client'; -import { inject, Injectable, InjectionToken } from '@angular/core'; +import { DestroyRef, inject, Injectable, InjectionToken } from '@angular/core'; import { distinctUntilChanged, Observable } from 'rxjs'; +import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop'; @Injectable({ providedIn: 'root' }) -export class IsMutatingService { +export class IsMutating { private queryClient = injectQueryClient(); + private destroyRef = inject(DestroyRef); use(filters?: MutationFilters) { - return new Observable((observer) => { + const result$ = new Observable((observer) => { observer.next(this.queryClient.isMutating(filters)); - this.queryClient.getMutationCache().subscribe( + const disposeSubscription = this.queryClient.getMutationCache().subscribe( notifyManager.batchCalls(() => { observer.next(this.queryClient.isMutating(filters)); }) ); - }).pipe(distinctUntilChanged()); + + return () => disposeSubscription(); + }).pipe( + distinctUntilChanged(), + takeUntilDestroyed(this.destroyRef) + ); + + return { + result$, + toSignal: () => toSignal(result$), + } } } -const UseIsMutating = new InjectionToken( +const UseIsMutating = new InjectionToken( 'UseIsFetching', { providedIn: 'root', factory() { - const isMutating = new IsMutatingService(); + const isMutating = new IsMutating(); return isMutating.use.bind(isMutating); }, } From f58ff97c8e42e8685669a84a5c9b13559d81b092 Mon Sep 17 00:00:00 2001 From: Philipp Czarnetzki Date: Sun, 5 Nov 2023 10:34:35 +0100 Subject: [PATCH 5/6] =?UTF-8?q?refactor(query):=20=F0=9F=92=A1=20expose=20?= =?UTF-8?q?observable=20and=20signal=20in=20`IsFetching`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add teardown logic to dispose the internal subscription received from `MutationCache` - Add `takeUntilDestroyed` to dispose of the observable when the `DestroyRef` signals it --- query/src/lib/is-fetching.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/query/src/lib/is-fetching.ts b/query/src/lib/is-fetching.ts index 46d10c8..fd05eca 100644 --- a/query/src/lib/is-fetching.ts +++ b/query/src/lib/is-fetching.ts @@ -1,30 +1,42 @@ import { QueryFilters, notifyManager } from '@tanstack/query-core'; import { injectQueryClient } from './query-client'; -import { inject, Injectable, InjectionToken } from '@angular/core'; +import { DestroyRef, inject, Injectable, InjectionToken } from '@angular/core'; import { distinctUntilChanged, Observable } from 'rxjs'; +import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop'; @Injectable({ providedIn: 'root' }) -export class IsFetchingService { +export class IsFetching { private queryClient = injectQueryClient(); + private destroyRef = inject(DestroyRef); use(filters?: QueryFilters) { - return new Observable((observer) => { + const result$ = new Observable((observer) => { observer.next(this.queryClient.isFetching(filters)); - this.queryClient.getQueryCache().subscribe( + const disposeSubscription = this.queryClient.getQueryCache().subscribe( notifyManager.batchCalls(() => { observer.next(this.queryClient.isFetching(filters)); }) ); - }).pipe(distinctUntilChanged()); + + return () => disposeSubscription(); + }).pipe( + distinctUntilChanged(), + takeUntilDestroyed(this.destroyRef) + ); + + return { + result$, + toSignal: () => toSignal(result$), + }; } } -const UseIsFetching = new InjectionToken( +const UseIsFetching = new InjectionToken( 'UseIsFetching', { providedIn: 'root', factory() { - const isFetching = new IsFetchingService(); + const isFetching = new IsFetching(); return isFetching.use.bind(isFetching); }, } From aa3881d18dfc071f3e89876b49bc9d47666ec02c Mon Sep 17 00:00:00 2001 From: Philipp Czarnetzki Date: Sun, 5 Nov 2023 10:47:03 +0100 Subject: [PATCH 6/6] =?UTF-8?q?refactor(query):=20=F0=9F=92=A1=20remove=20?= =?UTF-8?q?`DestroyRef`=20and=20`takeUntilDestroyed`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- query/src/lib/is-fetching.ts | 29 +++++++++++------------------ query/src/lib/is-mutating.ts | 34 ++++++++++++---------------------- 2 files changed, 23 insertions(+), 40 deletions(-) diff --git a/query/src/lib/is-fetching.ts b/query/src/lib/is-fetching.ts index fd05eca..81e16ef 100644 --- a/query/src/lib/is-fetching.ts +++ b/query/src/lib/is-fetching.ts @@ -1,13 +1,12 @@ -import { QueryFilters, notifyManager } from '@tanstack/query-core'; +import { notifyManager, type QueryFilters } from '@tanstack/query-core'; import { injectQueryClient } from './query-client'; -import { DestroyRef, inject, Injectable, InjectionToken } from '@angular/core'; +import { inject, Injectable, InjectionToken } from '@angular/core'; import { distinctUntilChanged, Observable } from 'rxjs'; -import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop'; +import { toSignal } from '@angular/core/rxjs-interop'; @Injectable({ providedIn: 'root' }) export class IsFetching { private queryClient = injectQueryClient(); - private destroyRef = inject(DestroyRef); use(filters?: QueryFilters) { const result$ = new Observable((observer) => { @@ -19,10 +18,7 @@ export class IsFetching { ); return () => disposeSubscription(); - }).pipe( - distinctUntilChanged(), - takeUntilDestroyed(this.destroyRef) - ); + }).pipe(distinctUntilChanged()); return { result$, @@ -31,16 +27,13 @@ export class IsFetching { } } -const UseIsFetching = new InjectionToken( - 'UseIsFetching', - { - providedIn: 'root', - factory() { - const isFetching = new IsFetching(); - return isFetching.use.bind(isFetching); - }, - } -); +const UseIsFetching = new InjectionToken('UseIsFetching', { + providedIn: 'root', + factory() { + const isFetching = new IsFetching(); + return isFetching.use.bind(isFetching); + }, +}); export function injectIsFetching() { return inject(UseIsFetching); diff --git a/query/src/lib/is-mutating.ts b/query/src/lib/is-mutating.ts index 35ed890..fe21514 100644 --- a/query/src/lib/is-mutating.ts +++ b/query/src/lib/is-mutating.ts @@ -1,16 +1,12 @@ -import { - type MutationFilters, - notifyManager -} from '@tanstack/query-core'; +import { type MutationFilters, notifyManager } from '@tanstack/query-core'; import { injectQueryClient } from './query-client'; -import { DestroyRef, inject, Injectable, InjectionToken } from '@angular/core'; +import { inject, Injectable, InjectionToken } from '@angular/core'; import { distinctUntilChanged, Observable } from 'rxjs'; -import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop'; +import { toSignal } from '@angular/core/rxjs-interop'; @Injectable({ providedIn: 'root' }) export class IsMutating { private queryClient = injectQueryClient(); - private destroyRef = inject(DestroyRef); use(filters?: MutationFilters) { const result$ = new Observable((observer) => { @@ -22,28 +18,22 @@ export class IsMutating { ); return () => disposeSubscription(); - }).pipe( - distinctUntilChanged(), - takeUntilDestroyed(this.destroyRef) - ); + }).pipe(distinctUntilChanged()); return { result$, toSignal: () => toSignal(result$), - } + }; } } -const UseIsMutating = new InjectionToken( - 'UseIsFetching', - { - providedIn: 'root', - factory() { - const isMutating = new IsMutating(); - return isMutating.use.bind(isMutating); - }, - } -); +const UseIsMutating = new InjectionToken('UseIsFetching', { + providedIn: 'root', + factory() { + const isMutating = new IsMutating(); + return isMutating.use.bind(isMutating); + }, +}); export function injectIsMutating() { return inject(UseIsMutating);