mirror of
https://github.com/GNS3/gns3-web-ui.git
synced 2025-02-01 08:48:04 +00:00
Merge pull request #702 from GNS3/Basic-cache-for-some-of-the-services
Basic cache for some of the services
This commit is contained in:
commit
f9938db312
@ -8,6 +8,7 @@ import { getTestServer } from './testing';
|
|||||||
import { SymbolService } from './symbol.service';
|
import { SymbolService } from './symbol.service';
|
||||||
import { Symbol } from '../models/symbol';
|
import { Symbol } from '../models/symbol';
|
||||||
import { AppTestingModule } from '../testing/app-testing/app-testing.module';
|
import { AppTestingModule } from '../testing/app-testing/app-testing.module';
|
||||||
|
import { of } from 'rxjs';
|
||||||
|
|
||||||
describe('SymbolService', () => {
|
describe('SymbolService', () => {
|
||||||
let httpClient: HttpClient;
|
let httpClient: HttpClient;
|
||||||
@ -50,20 +51,11 @@ describe('SymbolService', () => {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
it('should load symbols', inject([SymbolService], (service: 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' }]);
|
expect(service.load).toHaveBeenCalled();
|
||||||
|
|
||||||
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');
|
|
||||||
});
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it('should get symbols', inject([SymbolService], (service: SymbolService) => {
|
it('should get symbols', inject([SymbolService], (service: SymbolService) => {
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { BehaviorSubject, forkJoin, Observable } from 'rxjs';
|
import { BehaviorSubject, forkJoin, Observable, of } from 'rxjs';
|
||||||
|
|
||||||
import { Symbol } from '../models/symbol';
|
import { Symbol } from '../models/symbol';
|
||||||
import { Server } from '../models/server';
|
import { Server } from '../models/server';
|
||||||
import { HttpServer } from './http-server.service';
|
import { HttpServer } from './http-server.service';
|
||||||
|
import { shareReplay } from 'rxjs/operators';
|
||||||
|
|
||||||
|
const CACHE_SIZE = 1;
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SymbolService {
|
export class SymbolService {
|
||||||
public symbols: BehaviorSubject<Symbol[]> = new BehaviorSubject<Symbol[]>([]);
|
public symbols: BehaviorSubject<Symbol[]> = new BehaviorSubject<Symbol[]>([]);
|
||||||
|
private cache: Observable<Symbol[]>;
|
||||||
|
|
||||||
constructor(private httpServer: HttpServer) {}
|
constructor(private httpServer: HttpServer) {}
|
||||||
|
|
||||||
@ -16,25 +20,22 @@ export class SymbolService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
add(server: Server, symbolName: string, symbol: string) {
|
add(server: Server, symbolName: string, symbol: string) {
|
||||||
|
this.cache = null;
|
||||||
return this.httpServer.post(server, `/symbols/${symbolName}/raw`, symbol)
|
return this.httpServer.post(server, `/symbols/${symbolName}/raw`, symbol)
|
||||||
}
|
}
|
||||||
|
|
||||||
load(server: Server): Observable<Symbol[]> {
|
load(server: Server): Observable<Symbol[]> {
|
||||||
const subscription = this.list(server).subscribe((symbols: Symbol[]) => {
|
return this.httpServer.get<Symbol[]>(server, '/symbols');
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
list(server: Server) {
|
list(server: Server) {
|
||||||
return this.httpServer.get<Symbol[]>(server, '/symbols');
|
if(!this.cache) {
|
||||||
|
this.cache = this.load(server).pipe(
|
||||||
|
shareReplay(CACHE_SIZE)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
raw(server: Server, symbol_id: string) {
|
raw(server: Server, symbol_id: string) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user