Skip to content

Commit 2c402cb

Browse files
committed
Refactor initial task retrieval to use retryBackoff and Promise like call to get total
1 parent 5a6572e commit 2c402cb

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

src/app/app.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ export class AppComponent {
101101

102102
ngOnInit(): void {
103103
this.loadTasks().subscribe();
104-
this.onGoClick() // Generate the first task
105104
}
106105

107106
async ngAfterViewInit() {

src/app/services/task.service.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import {
2424
} from '@angular/fire/auth';
2525
import { getApp } from '@angular/fire/app';
2626
import { MatSnackBar } from '@angular/material/snack-bar';
27-
27+
import { retryBackoff } from 'backoff-rxjs';
2828
import { switchMap, tap, take, catchError } from 'rxjs/operators';
29-
import { Observable, of, Subject } from 'rxjs';
29+
import { Observable, of, defer, Subject } from 'rxjs';
3030
import {
3131
doc,
3232
Firestore,
@@ -35,6 +35,7 @@ import {
3535
deleteDoc,
3636
collectionData,
3737
collectionCount,
38+
getCountFromServer,
3839
query,
3940
orderBy,
4041
Timestamp,
@@ -176,10 +177,15 @@ export class TaskService {
176177
where('priority', '!=', 'null'),
177178
orderBy('createdTime', 'desc')
178179
);
179-
return this.loadTaskCount().pipe(
180-
take(1),
180+
return defer(() => this.loadTaskCount()).pipe(
181+
retryBackoff({
182+
initialInterval: 500,
183+
maxInterval: 2000,
184+
maxRetries: 10,
185+
}
186+
),
181187
switchMap((taskCount) => {
182-
if (taskCount === 0) {
188+
if (taskCount.data().count === 0) {
183189
return of([] as Task[]);
184190
}
185191
return collectionData(taskQuery, { idField: 'id' }) as Observable<
@@ -193,12 +199,12 @@ export class TaskService {
193199
);
194200
}
195201

196-
loadTaskCount(): Observable<number> {
202+
loadTaskCount() {
197203
const taskQuery = query(
198204
collection(this.firestore, 'todos'),
199205
where('priority', '!=', 'null')
200206
);
201-
return collectionCount(taskQuery);
207+
return getCountFromServer(taskQuery);
202208
}
203209

204210
loadSubtasks(maintaskId: string): Observable<Task[]> {

0 commit comments

Comments
 (0)