gns3-web-ui/src/app/services/symbol.service.ts

45 lines
1.4 KiB
TypeScript
Raw Normal View History

2017-09-25 13:07:52 +02:00
import { Injectable } from '@angular/core';
2018-07-06 11:31:44 +02:00
import { BehaviorSubject, forkJoin, Observable } from 'rxjs';
2017-09-25 13:07:52 +02:00
2018-07-19 10:00:09 +02:00
import { Symbol } from '../models/symbol';
2019-01-15 11:15:54 +01:00
import { Server } from '../models/server';
import { HttpServer } from './http-server.service';
2017-09-25 13:07:52 +02:00
@Injectable()
export class SymbolService {
2017-12-01 13:01:03 +01:00
public symbols: BehaviorSubject<Symbol[]> = new BehaviorSubject<Symbol[]>([]);
2017-09-25 13:07:52 +02:00
2019-01-15 11:15:54 +01:00
constructor(private httpServer: HttpServer) {}
2017-09-25 13:07:52 +02:00
get(symbol_id: string): Symbol {
2019-01-15 11:15:54 +01:00
return this.symbols.getValue().find((symbol: Symbol) => symbol.symbol_id === symbol_id);
2017-09-25 13:07:52 +02:00
}
2019-09-03 08:22:55 -07:00
add(server: Server, symbolName: string, symbol: string) {
return this.httpServer.post(server, `/symbols/${symbolName}/raw`, symbol)
}
2017-09-25 13:07:52 +02:00
load(server: Server): Observable<Symbol[]> {
const subscription = this.list(server).subscribe((symbols: Symbol[]) => {
2017-09-25 13:07:52 +02:00
const streams = symbols.map(symbol => this.raw(server, symbol.symbol_id));
2019-01-15 11:15:54 +01:00
forkJoin(streams).subscribe(results => {
2017-09-25 13:07:52 +02:00
symbols.forEach((symbol: Symbol, i: number) => {
2018-03-21 10:06:39 +01:00
symbol.raw = results[i];
2017-09-25 13:07:52 +02:00
});
this.symbols.next(symbols);
subscription.unsubscribe();
2017-09-25 13:07:52 +02:00
});
});
return this.symbols.asObservable();
}
2018-03-20 16:02:41 +01:00
list(server: Server) {
2019-01-15 11:15:54 +01:00
return this.httpServer.get<Symbol[]>(server, '/symbols');
2017-09-25 13:07:52 +02:00
}
2018-03-20 16:02:41 +01:00
raw(server: Server, symbol_id: string) {
2017-09-25 13:07:52 +02:00
const encoded_uri = encodeURI(symbol_id);
2019-01-15 11:15:54 +01:00
return this.httpServer.getText(server, `/symbols/${encoded_uri}/raw`);
2017-09-25 13:07:52 +02:00
}
}