Skip to content

Commit 7a2b39e

Browse files
committed
migrate from mergeMap to concat
1 parent b400271 commit 7a2b39e

File tree

7 files changed

+107
-92
lines changed

7 files changed

+107
-92
lines changed

client/src/app/ride/ride.service.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import {
66
import { TestBed } from '@angular/core/testing';
77
import { Subject } from 'rxjs';
88
import { NotificationService } from '../common-components/notification.service';
9-
import { RideService, RideStats } from './ride.service';
109
import { StravaService } from '../strava/strava.service';
10+
import { RideService, RideStats } from './ride.service';
1111

1212
function setup() {
1313
const mockNotificationService: jasmine.SpyObj<NotificationService> =
1414
jasmine.createSpyObj(['showNotification']);
15-
const syncActivities = new Subject<void>();
15+
const syncActivities = new Subject<never>();
1616
const mockStravaService: jasmine.SpyObj<StravaService> = jasmine.createSpyObj(
1717
['syncActivities']
1818
);
@@ -69,7 +69,7 @@ describe('RideService', () => {
6969
service.getRideStats(30).subscribe((rideStats) => {
7070
expect(rideStats).toEqual(mockResponse);
7171
});
72-
syncActivities.next();
72+
syncActivities.complete()
7373
httpTestingController
7474
.expectOne('/api/ride/stats?period=30')
7575
.flush(mockResponse);
@@ -86,7 +86,7 @@ describe('RideService', () => {
8686
service.getRideStats(30).subscribe((rideStats) => {
8787
expect(rideStats).toBeUndefined();
8888
});
89-
syncActivities.next();
89+
syncActivities.complete()
9090
httpTestingController
9191
.expectOne('/api/ride/stats?period=30')
9292
.error(new ProgressEvent(''));
@@ -102,7 +102,7 @@ describe('RideService', () => {
102102
service.getRideStats(30).subscribe((rideStats) => {
103103
expect(rideStats).toEqual(mockResponse);
104104
});
105-
syncActivities.next();
105+
syncActivities.complete()
106106
service.getRideStats(30).subscribe((rideStats) => {
107107
expect(rideStats).toEqual(mockResponse);
108108
});
@@ -120,7 +120,7 @@ describe('RideService', () => {
120120
service.getRideStats(30).subscribe((rideStats) => {
121121
expect(rideStats).toEqual(mockResponse2);
122122
});
123-
syncActivities.next();
123+
syncActivities.complete()
124124
service.getRideStats(10).subscribe((rideStats) => {
125125
expect(rideStats).toEqual(mockResponse);
126126
});

client/src/app/ride/ride.service.ts

+25-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { HttpClient } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3-
import { EMPTY, Observable, catchError, mergeMap, shareReplay } from 'rxjs';
3+
import {
4+
EMPTY,
5+
Observable,
6+
catchError,
7+
concat,
8+
shareReplay
9+
} from 'rxjs';
410
import { NotificationService } from '../common-components/notification.service';
511
import { StravaService } from '../strava/strava.service';
612

@@ -26,25 +32,24 @@ export class RideService {
2632
return this.cache[period];
2733
}
2834

29-
this.cache[period] = this.stravaService.syncActivities().pipe(
30-
mergeMap(() =>
31-
this.http
32-
.get<RideStats>('/api/ride/stats', {
33-
params: {
34-
...(period ? { period } : {}),
35-
},
36-
})
37-
.pipe(
38-
catchError(() => {
39-
this.notificationService.showNotification(
40-
'Unable to fetch ride stats',
41-
'error'
42-
);
43-
return EMPTY;
44-
})
45-
)
46-
),
47-
shareReplay(1)
35+
this.cache[period] = concat(
36+
this.stravaService.syncActivities(),
37+
this.http
38+
.get<RideStats>('/api/ride/stats', {
39+
params: {
40+
...(period ? { period } : {}),
41+
},
42+
})
43+
.pipe(
44+
catchError(() => {
45+
this.notificationService.showNotification(
46+
'Unable to fetch ride stats',
47+
'error'
48+
);
49+
return EMPTY;
50+
}),
51+
shareReplay(1)
52+
)
4853
);
4954

5055
return this.cache[period];

client/src/app/strava/strava.service.spec.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@ import {
44
provideHttpClientTesting,
55
} from '@angular/common/http/testing';
66
import { TestBed } from '@angular/core/testing';
7-
import { StravaService } from './strava.service';
7+
import { EMPTY } from 'rxjs';
88
import { NotificationService } from '../common-components/notification.service';
9+
import { WithingsService } from '../withings/withings.service';
10+
import { StravaService } from './strava.service';
911

1012
function setup() {
1113
const mockNotificationService: jasmine.SpyObj<NotificationService> =
1214
jasmine.createSpyObj(['showNotification']);
15+
const mockWithingsService: jasmine.SpyObj<WithingsService> =
16+
jasmine.createSpyObj(['syncMeasurements']);
17+
18+
mockWithingsService.syncMeasurements.and.returnValue(EMPTY);
19+
1320
TestBed.configureTestingModule({
1421
providers: [
1522
provideHttpClient(),
1623
provideHttpClientTesting(),
1724
StravaService,
1825
{ provide: NotificationService, useValue: mockNotificationService },
26+
{ provide: WithingsService, useValue: mockWithingsService },
1927
],
2028
});
2129
const service = TestBed.inject(StravaService);

client/src/app/strava/strava.service.ts

+21-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { HttpClient } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3-
import { EMPTY, catchError, mergeMap, of, shareReplay } from 'rxjs';
3+
import {
4+
EMPTY,
5+
catchError,
6+
concat,
7+
mergeMap,
8+
shareReplay
9+
} from 'rxjs';
410
import { NotificationService } from '../common-components/notification.service';
511
import { WithingsService } from '../withings/withings.service';
612

@@ -12,22 +18,20 @@ export class StravaService {
1218
private readonly withingsService: WithingsService
1319
) {}
1420

15-
private readonly $syncActivities = this.withingsService
16-
.syncMeasurements()
17-
.pipe(
18-
mergeMap(() =>
19-
this.http.post<void>('/api/strava/activities/sync', undefined).pipe(
20-
catchError(() => {
21-
this.notificationService.showNotification(
22-
'Unable to sync with Strava',
23-
'error'
24-
);
25-
return EMPTY;
26-
}),
27-
shareReplay(1)
28-
)
29-
)
30-
);
21+
private readonly $syncActivities = concat(
22+
this.withingsService.syncMeasurements(),
23+
this.http.post<void>('/api/strava/activities/sync', undefined).pipe(
24+
mergeMap(() => EMPTY),
25+
catchError((e) => {
26+
this.notificationService.showNotification(
27+
'Unable to sync with Strava',
28+
'error'
29+
);
30+
return EMPTY;
31+
}),
32+
shareReplay(1)
33+
)
34+
);
3135

3236
syncActivities() {
3337
return this.$syncActivities;

client/src/app/weight/weight.service.spec.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import {
44
provideHttpClientTesting,
55
} from '@angular/common/http/testing';
66
import { TestBed } from '@angular/core/testing';
7-
import { WeightMeasurement, WeightService } from './weight.service';
8-
import { NotificationService } from '../common-components/notification.service';
97
import { Subject } from 'rxjs';
8+
import { NotificationService } from '../common-components/notification.service';
109
import { WithingsService } from '../withings/withings.service';
10+
import { WeightMeasurement, WeightService } from './weight.service';
1111

1212
function setup() {
1313
const mockNotificationService: jasmine.SpyObj<NotificationService> =
1414
jasmine.createSpyObj(['showNotification']);
15-
const syncMeasurements = new Subject<void>();
15+
const syncMeasurements = new Subject<never>();
1616
const mockWithingsService: jasmine.SpyObj<WithingsService> =
1717
jasmine.createSpyObj(['syncMeasurements']);
1818
mockWithingsService.syncMeasurements.and.returnValue(
@@ -79,7 +79,7 @@ describe('WeightService', () => {
7979
service.getWeight(7).subscribe((measurements) => {
8080
expect(measurements).toEqual(mockResponse);
8181
});
82-
syncMeasurements.next();
82+
syncMeasurements.complete();
8383
httpTestingController
8484
.expectOne('/api/weight?period=7')
8585
.flush(mockResponse);
@@ -106,7 +106,7 @@ describe('WeightService', () => {
106106
service.getWeight(7).subscribe((measurements) => {
107107
expect(measurements).toEqual(mockResponse);
108108
});
109-
syncMeasurements.next();
109+
syncMeasurements.complete();
110110
httpTestingController
111111
.expectOne('/api/weight?period=7')
112112
.error(new ProgressEvent(''));
@@ -122,7 +122,7 @@ describe('WeightService', () => {
122122
service.getWeight(7).subscribe((measurements) => {
123123
expect(measurements).toEqual(mockResponse);
124124
});
125-
syncMeasurements.next();
125+
syncMeasurements.complete();
126126
service.getWeight(7).subscribe((measurements) => {
127127
expect(measurements).toEqual(mockResponse);
128128
});
@@ -140,7 +140,7 @@ describe('WeightService', () => {
140140
service.getWeight(30).subscribe((measurements) => {
141141
expect(measurements).toEqual(mockResponse2);
142142
});
143-
syncMeasurements.next();
143+
syncMeasurements.complete();
144144
service.getWeight(10).subscribe((measurements) => {
145145
expect(measurements).toEqual(mockResponse);
146146
});
@@ -166,7 +166,7 @@ describe('WeightService', () => {
166166
service.getTodayWeight().subscribe((measurement) => {
167167
expect(measurement).toEqual(mockResponse.at(-1));
168168
});
169-
syncMeasurements.next();
169+
syncMeasurements.complete();
170170
httpTestingController
171171
.expectOne('/api/weight?period=1')
172172
.flush([mockResponse.at(-1)]);
@@ -178,7 +178,7 @@ describe('WeightService', () => {
178178
service.getTodayWeight().subscribe((measurement) => {
179179
expect(measurement).toEqual(mockResponse.at(-1));
180180
});
181-
syncMeasurements.next();
181+
syncMeasurements.complete();
182182
httpTestingController
183183
.expectOne('/api/weight?period=1')
184184
.flush(mockResponse);
@@ -190,7 +190,7 @@ describe('WeightService', () => {
190190
service.getTodayWeight().subscribe((measurement) => {
191191
expect(measurement).toBeUndefined();
192192
});
193-
syncMeasurements.next();
193+
syncMeasurements.complete();
194194
httpTestingController.expectOne('/api/weight?period=1').flush([]);
195195
httpTestingController.verify();
196196
});
@@ -213,7 +213,7 @@ describe('WeightService', () => {
213213
service.getTodayWeight().subscribe((measurement) => {
214214
expect(measurement).toEqual(mockResponse.at(-1));
215215
});
216-
syncMeasurements.next();
216+
syncMeasurements.complete();
217217
httpTestingController
218218
.expectOne('/api/weight?period=1')
219219
.error(new ProgressEvent(''));
@@ -229,7 +229,7 @@ describe('WeightService', () => {
229229
service.getTodayWeight().subscribe((measurements) => {
230230
expect(measurements).toEqual(mockResponse.at(-1));
231231
});
232-
syncMeasurements.next();
232+
syncMeasurements.complete();
233233
service.getTodayWeight().subscribe((measurements) => {
234234
expect(measurements).toEqual(mockResponse.at(-1));
235235
});
@@ -248,7 +248,7 @@ describe('WeightService', () => {
248248
expect(diff?.fatMassWeight?.toFixed(3)).toBe('-0.056');
249249
expect(diff?.fatRatio?.toFixed(3)).toBe('-0.033');
250250
});
251-
syncMeasurements.next();
251+
syncMeasurements.complete();
252252
httpTestingController.expectOne('/api/weight?period=7').flush([
253253
{
254254
date: daysBefore(3),
@@ -271,7 +271,7 @@ describe('WeightService', () => {
271271
service.getDiff(7).subscribe((diff) => {
272272
expect(diff?.weight.toFixed(3)).toBe('-0.017');
273273
});
274-
syncMeasurements.next();
274+
syncMeasurements.complete();
275275
httpTestingController.expectOne('/api/weight?period=7').flush([
276276
{
277277
date: daysBefore(3),
@@ -290,7 +290,7 @@ describe('WeightService', () => {
290290
service.getDiff(7).subscribe((diff) => {
291291
expect(diff).toBeUndefined();
292292
});
293-
syncMeasurements.next();
293+
syncMeasurements.complete();
294294
httpTestingController.expectOne('/api/weight?period=7').flush([
295295
{
296296
date: daysBefore(3),
@@ -305,7 +305,7 @@ describe('WeightService', () => {
305305
service.getDiff(7).subscribe((diff) => {
306306
expect(diff).toBeUndefined();
307307
});
308-
syncMeasurements.next();
308+
syncMeasurements.complete();
309309
httpTestingController.expectOne('/api/weight?period=7').flush([]);
310310
httpTestingController.verify();
311311
});
@@ -326,7 +326,7 @@ describe('WeightService', () => {
326326
syncMeasurements,
327327
} = setup();
328328
service.getDiff(7).subscribe();
329-
syncMeasurements.next();
329+
syncMeasurements.complete();
330330
httpTestingController
331331
.expectOne('/api/weight?period=7')
332332
.error(new ProgressEvent(''));
@@ -342,7 +342,7 @@ describe('WeightService', () => {
342342
service.getDiff(7).subscribe((diff) => {
343343
expect(diff?.weight.toFixed(3)).toEqual('-0.010');
344344
});
345-
syncMeasurements.next();
345+
syncMeasurements.complete();
346346
service.getDiff(7).subscribe((diff) => {
347347
expect(diff?.weight.toFixed(3)).toEqual('-0.010');
348348
});
@@ -358,7 +358,7 @@ describe('WeightService', () => {
358358
service.getWeight(1).subscribe();
359359
service.getTodayWeight().subscribe();
360360
service.getDiff(1).subscribe();
361-
syncMeasurements.next();
361+
syncMeasurements.complete();
362362
const request = httpTestingController.expectOne('/api/weight?period=1');
363363
request.flush(mockResponse);
364364
expect(request.request.method).toBe('GET');

0 commit comments

Comments
 (0)