From 76d0f0d209de5209c476a817b6c37ba459623118 Mon Sep 17 00:00:00 2001 From: piotrpekala7 <31202938+piotrpekala7@users.noreply.github.com> Date: Wed, 19 Feb 2020 15:54:18 +0100 Subject: [PATCH 1/2] Basic cache for some of the services --- src/app/services/symbol.service.ts | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/app/services/symbol.service.ts b/src/app/services/symbol.service.ts index fba64d13..cba041d0 100644 --- a/src/app/services/symbol.service.ts +++ b/src/app/services/symbol.service.ts @@ -1,13 +1,17 @@ import { Injectable } from '@angular/core'; -import { BehaviorSubject, forkJoin, Observable } from 'rxjs'; +import { BehaviorSubject, forkJoin, Observable, of } from 'rxjs'; import { Symbol } from '../models/symbol'; import { Server } from '../models/server'; import { HttpServer } from './http-server.service'; +import { shareReplay } from 'rxjs/operators'; + +const CACHE_SIZE = 1; @Injectable() export class SymbolService { public symbols: BehaviorSubject = new BehaviorSubject([]); + private cache: Observable; constructor(private httpServer: HttpServer) {} @@ -16,25 +20,22 @@ export class SymbolService { } add(server: Server, symbolName: string, symbol: string) { + this.cache = null; return this.httpServer.post(server, `/symbols/${symbolName}/raw`, symbol) } load(server: Server): Observable { - const subscription = this.list(server).subscribe((symbols: Symbol[]) => { - const streams = symbols.map(symbol => this.raw(server, symbol.symbol_id)); - forkJoin(streams).subscribe(results => { - symbols.forEach((symbol: Symbol, i: number) => { - symbol.raw = results[i]; - }); - this.symbols.next(symbols); - subscription.unsubscribe(); - }); - }); - return this.symbols.asObservable(); + return this.httpServer.get(server, '/symbols'); } list(server: Server) { - return this.httpServer.get(server, '/symbols'); + if(!this.cache) { + this.cache = this.load(server).pipe( + shareReplay(CACHE_SIZE) + ); + } + + return this.cache; } raw(server: Server, symbol_id: string) { From e69b4684e74e71e39755300feb60a025fa5d120c Mon Sep 17 00:00:00 2001 From: piotrpekala7 <31202938+piotrpekala7@users.noreply.github.com> Date: Thu, 27 Feb 2020 10:08:53 +0100 Subject: [PATCH 2/2] Update symbol.service.spec.ts --- src/app/services/symbol.service.spec.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/app/services/symbol.service.spec.ts b/src/app/services/symbol.service.spec.ts index af58eee0..6ef893a6 100644 --- a/src/app/services/symbol.service.spec.ts +++ b/src/app/services/symbol.service.spec.ts @@ -8,6 +8,7 @@ import { getTestServer } from './testing'; import { SymbolService } from './symbol.service'; import { Symbol } from '../models/symbol'; import { AppTestingModule } from '../testing/app-testing/app-testing.module'; +import { of } from 'rxjs'; describe('SymbolService', () => { let httpClient: HttpClient; @@ -50,20 +51,11 @@ describe('SymbolService', () => { })); it('should load symbols', inject([SymbolService], (service: SymbolService) => { - service.load(server).subscribe(); + spyOn(service, 'load').and.returnValue(of([])); - const req = httpTestingController.expectOne('http://127.0.0.1:3080/v2/symbols'); + service.list(server).subscribe(); - req.flush([{ symbol_id: 'myid' }]); - - const raw = httpTestingController.expectOne('http://127.0.0.1:3080/v2/symbols/myid/raw'); - raw.flush('myraw'); - - service.symbols.subscribe(symbols => { - expect(symbols.length).toEqual(1); - expect(symbols[0].symbol_id).toEqual('myid'); - expect(symbols[0].raw).toEqual('myraw'); - }); + expect(service.load).toHaveBeenCalled(); })); it('should get symbols', inject([SymbolService], (service: SymbolService) => {