Skip to content

Commit 843d73f

Browse files
committed
feat: remove the need to explictly unsubscribe
1 parent fcff9b9 commit 843d73f

File tree

3 files changed

+18
-35
lines changed

3 files changed

+18
-35
lines changed

packages/ng-query/src/lib/base-query.ts

+9-18
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@ import {
66
QueryObserverOptions,
77
QueryObserverResult,
88
} from '@tanstack/query-core';
9-
import {
10-
Observable,
11-
shareReplay,
12-
Subject,
13-
Subscription,
14-
takeUntil,
15-
Unsubscribable,
16-
} from 'rxjs';
9+
import { Observable, shareReplay, Subscription } from 'rxjs';
1710

1811
export function baseQuery<
1912
TQueryFnData = unknown,
@@ -51,13 +44,6 @@ export function baseQuery<
5144

5245
console.log('NEW OBSERVER INSTANCE');
5346

54-
const destroy = new Subject();
55-
56-
(queryObserver as unknown as Unsubscribable).unsubscribe = () => {
57-
destroy.next(true);
58-
destroy.complete();
59-
};
60-
6147
(
6248
queryObserver as unknown as {
6349
updateQueryKey: (queryKey: QueryKey) => void;
@@ -75,12 +61,18 @@ export function baseQuery<
7561

7662
(queryObserver as unknown as { result$: Observable<unknown> }).result$ =
7763
new Observable<QueryObserverResult<TData, TError>>((observer) => {
64+
const mergedOptions = client.defaultQueryOptions({
65+
...options,
66+
// The query key can be changed, so we need to rebuild it each time
67+
...queryObserver.options,
68+
});
69+
7870
console.log(
7971
'NEW OBSERVER SUBSCRIPTION',
8072
queryObserver.getCurrentQuery().queryKey
8173
);
8274

83-
observer.next(queryObserver.getOptimisticResult(defaultedOptions));
75+
observer.next(queryObserver.getOptimisticResult(mergedOptions));
8476

8577
const queryObserverDispose = queryObserver.subscribe((result) => {
8678
observer.next(
@@ -99,10 +91,9 @@ export function baseQuery<
9991
queryObserverDispose();
10092
};
10193
}).pipe(
102-
takeUntil(destroy.asObservable()),
10394
shareReplay({
10495
bufferSize: 1,
105-
refCount: !defaultedOptions.keepPreviousData,
96+
refCount: true,
10697
})
10798
);
10899

packages/playground/src/app/pagination-page/pagination-page.component.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { CommonModule } from '@angular/common';
2-
import {
3-
ChangeDetectionStrategy,
4-
Component,
5-
inject,
6-
OnDestroy,
7-
} from '@angular/core';
2+
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
83
import { SubscribeModule } from '@ngneat/subscribe';
94
import { BehaviorSubject, switchMap, tap } from 'rxjs';
105
import { SpinnerComponent } from '../spinner/spinner.component';
@@ -44,15 +39,20 @@ import { PaginationService } from './pagination.service';
4439
Previous Page
4540
</button>
4641
47-
<button (click)="nextPage()">Next Page</button>
42+
<button
43+
(click)="nextPage()"
44+
[disabled]="projects.isPreviousData || !projects.data?.hasMore"
45+
>
46+
Next Page
47+
</button>
4848
{{ projects.isFetching ? 'Loading...' : '' }}
4949
</div>
5050
</ng-container>
5151
`,
5252
styles: [],
5353
changeDetection: ChangeDetectionStrategy.OnPush,
5454
})
55-
export class PaginationPageComponent implements OnDestroy {
55+
export class PaginationPageComponent {
5656
private page = new BehaviorSubject(0);
5757
page$ = this.page.asObservable();
5858
projectsService = inject(PaginationService);
@@ -75,10 +75,6 @@ export class PaginationPageComponent implements OnDestroy {
7575
this.page.next(this.page.getValue() - 1);
7676
}
7777

78-
ngOnDestroy() {
79-
this.projectsService.destroy();
80-
}
81-
8278
trackBy(_: number, project: { id: number }) {
8379
return project.id;
8480
}

packages/playground/src/app/pagination-page/pagination.service.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,12 @@ export class PaginationService {
2727
);
2828
}
2929

30-
destroy() {
31-
this.projectsObserver?.unsubscribe();
32-
this.projectsObserver = null;
33-
}
34-
3530
private fetchProjects(page = 0) {
3631
return this.useQuery(
3732
['projects', page] as const,
3833
({ queryKey }) => fetchProjects(queryKey[1]),
3934
{
35+
staleTime: 5000,
4036
keepPreviousData: true,
4137
}
4238
);

0 commit comments

Comments
 (0)