Skip to content

Commit

Permalink
migrate from mergeMap to concat
Browse files Browse the repository at this point in the history
  • Loading branch information
mucsi96 committed Nov 9, 2023
1 parent b400271 commit 7a2b39e
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 92 deletions.
12 changes: 6 additions & 6 deletions client/src/app/ride/ride.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {
import { TestBed } from '@angular/core/testing';
import { Subject } from 'rxjs';
import { NotificationService } from '../common-components/notification.service';
import { RideService, RideStats } from './ride.service';
import { StravaService } from '../strava/strava.service';
import { RideService, RideStats } from './ride.service';

function setup() {
const mockNotificationService: jasmine.SpyObj<NotificationService> =
jasmine.createSpyObj(['showNotification']);
const syncActivities = new Subject<void>();
const syncActivities = new Subject<never>();
const mockStravaService: jasmine.SpyObj<StravaService> = jasmine.createSpyObj(
['syncActivities']
);
Expand Down Expand Up @@ -69,7 +69,7 @@ describe('RideService', () => {
service.getRideStats(30).subscribe((rideStats) => {
expect(rideStats).toEqual(mockResponse);
});
syncActivities.next();
syncActivities.complete()
httpTestingController
.expectOne('/api/ride/stats?period=30')
.flush(mockResponse);
Expand All @@ -86,7 +86,7 @@ describe('RideService', () => {
service.getRideStats(30).subscribe((rideStats) => {
expect(rideStats).toBeUndefined();
});
syncActivities.next();
syncActivities.complete()
httpTestingController
.expectOne('/api/ride/stats?period=30')
.error(new ProgressEvent(''));
Expand All @@ -102,7 +102,7 @@ describe('RideService', () => {
service.getRideStats(30).subscribe((rideStats) => {
expect(rideStats).toEqual(mockResponse);
});
syncActivities.next();
syncActivities.complete()
service.getRideStats(30).subscribe((rideStats) => {
expect(rideStats).toEqual(mockResponse);
});
Expand All @@ -120,7 +120,7 @@ describe('RideService', () => {
service.getRideStats(30).subscribe((rideStats) => {
expect(rideStats).toEqual(mockResponse2);
});
syncActivities.next();
syncActivities.complete()
service.getRideStats(10).subscribe((rideStats) => {
expect(rideStats).toEqual(mockResponse);
});
Expand Down
45 changes: 25 additions & 20 deletions client/src/app/ride/ride.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { EMPTY, Observable, catchError, mergeMap, shareReplay } from 'rxjs';
import {
EMPTY,
Observable,
catchError,
concat,
shareReplay
} from 'rxjs';
import { NotificationService } from '../common-components/notification.service';
import { StravaService } from '../strava/strava.service';

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

this.cache[period] = this.stravaService.syncActivities().pipe(
mergeMap(() =>
this.http
.get<RideStats>('/api/ride/stats', {
params: {
...(period ? { period } : {}),
},
})
.pipe(
catchError(() => {
this.notificationService.showNotification(
'Unable to fetch ride stats',
'error'
);
return EMPTY;
})
)
),
shareReplay(1)
this.cache[period] = concat(
this.stravaService.syncActivities(),
this.http
.get<RideStats>('/api/ride/stats', {
params: {
...(period ? { period } : {}),
},
})
.pipe(
catchError(() => {
this.notificationService.showNotification(
'Unable to fetch ride stats',
'error'
);
return EMPTY;
}),
shareReplay(1)
)
);

return this.cache[period];
Expand Down
10 changes: 9 additions & 1 deletion client/src/app/strava/strava.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@ import {
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { StravaService } from './strava.service';
import { EMPTY } from 'rxjs';
import { NotificationService } from '../common-components/notification.service';
import { WithingsService } from '../withings/withings.service';
import { StravaService } from './strava.service';

function setup() {
const mockNotificationService: jasmine.SpyObj<NotificationService> =
jasmine.createSpyObj(['showNotification']);
const mockWithingsService: jasmine.SpyObj<WithingsService> =
jasmine.createSpyObj(['syncMeasurements']);

mockWithingsService.syncMeasurements.and.returnValue(EMPTY);

TestBed.configureTestingModule({
providers: [
provideHttpClient(),
provideHttpClientTesting(),
StravaService,
{ provide: NotificationService, useValue: mockNotificationService },
{ provide: WithingsService, useValue: mockWithingsService },
],
});
const service = TestBed.inject(StravaService);
Expand Down
38 changes: 21 additions & 17 deletions client/src/app/strava/strava.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { EMPTY, catchError, mergeMap, of, shareReplay } from 'rxjs';
import {
EMPTY,
catchError,
concat,
mergeMap,
shareReplay
} from 'rxjs';
import { NotificationService } from '../common-components/notification.service';
import { WithingsService } from '../withings/withings.service';

Expand All @@ -12,22 +18,20 @@ export class StravaService {
private readonly withingsService: WithingsService
) {}

private readonly $syncActivities = this.withingsService
.syncMeasurements()
.pipe(
mergeMap(() =>
this.http.post<void>('/api/strava/activities/sync', undefined).pipe(
catchError(() => {
this.notificationService.showNotification(
'Unable to sync with Strava',
'error'
);
return EMPTY;
}),
shareReplay(1)
)
)
);
private readonly $syncActivities = concat(
this.withingsService.syncMeasurements(),
this.http.post<void>('/api/strava/activities/sync', undefined).pipe(
mergeMap(() => EMPTY),
catchError((e) => {
this.notificationService.showNotification(
'Unable to sync with Strava',
'error'
);
return EMPTY;
}),
shareReplay(1)
)
);

syncActivities() {
return this.$syncActivities;
Expand Down
38 changes: 19 additions & 19 deletions client/src/app/weight/weight.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import {
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { WeightMeasurement, WeightService } from './weight.service';
import { NotificationService } from '../common-components/notification.service';
import { Subject } from 'rxjs';
import { NotificationService } from '../common-components/notification.service';
import { WithingsService } from '../withings/withings.service';
import { WeightMeasurement, WeightService } from './weight.service';

function setup() {
const mockNotificationService: jasmine.SpyObj<NotificationService> =
jasmine.createSpyObj(['showNotification']);
const syncMeasurements = new Subject<void>();
const syncMeasurements = new Subject<never>();
const mockWithingsService: jasmine.SpyObj<WithingsService> =
jasmine.createSpyObj(['syncMeasurements']);
mockWithingsService.syncMeasurements.and.returnValue(
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('WeightService', () => {
service.getWeight(7).subscribe((measurements) => {
expect(measurements).toEqual(mockResponse);
});
syncMeasurements.next();
syncMeasurements.complete();
httpTestingController
.expectOne('/api/weight?period=7')
.flush(mockResponse);
Expand All @@ -106,7 +106,7 @@ describe('WeightService', () => {
service.getWeight(7).subscribe((measurements) => {
expect(measurements).toEqual(mockResponse);
});
syncMeasurements.next();
syncMeasurements.complete();
httpTestingController
.expectOne('/api/weight?period=7')
.error(new ProgressEvent(''));
Expand All @@ -122,7 +122,7 @@ describe('WeightService', () => {
service.getWeight(7).subscribe((measurements) => {
expect(measurements).toEqual(mockResponse);
});
syncMeasurements.next();
syncMeasurements.complete();
service.getWeight(7).subscribe((measurements) => {
expect(measurements).toEqual(mockResponse);
});
Expand All @@ -140,7 +140,7 @@ describe('WeightService', () => {
service.getWeight(30).subscribe((measurements) => {
expect(measurements).toEqual(mockResponse2);
});
syncMeasurements.next();
syncMeasurements.complete();
service.getWeight(10).subscribe((measurements) => {
expect(measurements).toEqual(mockResponse);
});
Expand All @@ -166,7 +166,7 @@ describe('WeightService', () => {
service.getTodayWeight().subscribe((measurement) => {
expect(measurement).toEqual(mockResponse.at(-1));
});
syncMeasurements.next();
syncMeasurements.complete();
httpTestingController
.expectOne('/api/weight?period=1')
.flush([mockResponse.at(-1)]);
Expand All @@ -178,7 +178,7 @@ describe('WeightService', () => {
service.getTodayWeight().subscribe((measurement) => {
expect(measurement).toEqual(mockResponse.at(-1));
});
syncMeasurements.next();
syncMeasurements.complete();
httpTestingController
.expectOne('/api/weight?period=1')
.flush(mockResponse);
Expand All @@ -190,7 +190,7 @@ describe('WeightService', () => {
service.getTodayWeight().subscribe((measurement) => {
expect(measurement).toBeUndefined();
});
syncMeasurements.next();
syncMeasurements.complete();
httpTestingController.expectOne('/api/weight?period=1').flush([]);
httpTestingController.verify();
});
Expand All @@ -213,7 +213,7 @@ describe('WeightService', () => {
service.getTodayWeight().subscribe((measurement) => {
expect(measurement).toEqual(mockResponse.at(-1));
});
syncMeasurements.next();
syncMeasurements.complete();
httpTestingController
.expectOne('/api/weight?period=1')
.error(new ProgressEvent(''));
Expand All @@ -229,7 +229,7 @@ describe('WeightService', () => {
service.getTodayWeight().subscribe((measurements) => {
expect(measurements).toEqual(mockResponse.at(-1));
});
syncMeasurements.next();
syncMeasurements.complete();
service.getTodayWeight().subscribe((measurements) => {
expect(measurements).toEqual(mockResponse.at(-1));
});
Expand All @@ -248,7 +248,7 @@ describe('WeightService', () => {
expect(diff?.fatMassWeight?.toFixed(3)).toBe('-0.056');
expect(diff?.fatRatio?.toFixed(3)).toBe('-0.033');
});
syncMeasurements.next();
syncMeasurements.complete();
httpTestingController.expectOne('/api/weight?period=7').flush([
{
date: daysBefore(3),
Expand All @@ -271,7 +271,7 @@ describe('WeightService', () => {
service.getDiff(7).subscribe((diff) => {
expect(diff?.weight.toFixed(3)).toBe('-0.017');
});
syncMeasurements.next();
syncMeasurements.complete();
httpTestingController.expectOne('/api/weight?period=7').flush([
{
date: daysBefore(3),
Expand All @@ -290,7 +290,7 @@ describe('WeightService', () => {
service.getDiff(7).subscribe((diff) => {
expect(diff).toBeUndefined();
});
syncMeasurements.next();
syncMeasurements.complete();
httpTestingController.expectOne('/api/weight?period=7').flush([
{
date: daysBefore(3),
Expand All @@ -305,7 +305,7 @@ describe('WeightService', () => {
service.getDiff(7).subscribe((diff) => {
expect(diff).toBeUndefined();
});
syncMeasurements.next();
syncMeasurements.complete();
httpTestingController.expectOne('/api/weight?period=7').flush([]);
httpTestingController.verify();
});
Expand All @@ -326,7 +326,7 @@ describe('WeightService', () => {
syncMeasurements,
} = setup();
service.getDiff(7).subscribe();
syncMeasurements.next();
syncMeasurements.complete();
httpTestingController
.expectOne('/api/weight?period=7')
.error(new ProgressEvent(''));
Expand All @@ -342,7 +342,7 @@ describe('WeightService', () => {
service.getDiff(7).subscribe((diff) => {
expect(diff?.weight.toFixed(3)).toEqual('-0.010');
});
syncMeasurements.next();
syncMeasurements.complete();
service.getDiff(7).subscribe((diff) => {
expect(diff?.weight.toFixed(3)).toEqual('-0.010');
});
Expand All @@ -358,7 +358,7 @@ describe('WeightService', () => {
service.getWeight(1).subscribe();
service.getTodayWeight().subscribe();
service.getDiff(1).subscribe();
syncMeasurements.next();
syncMeasurements.complete();
const request = httpTestingController.expectOne('/api/weight?period=1');
request.flush(mockResponse);
expect(request.request.method).toBe('GET');
Expand Down
Loading

0 comments on commit 7a2b39e

Please sign in to comment.