2017-09-25 13:07:52 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Observable } from 'rxjs/Observable';
|
|
|
|
import { BehaviorSubject } from "rxjs/BehaviorSubject";
|
|
|
|
|
|
|
|
import 'rxjs/add/operator/map';
|
|
|
|
import 'rxjs/add/observable/forkJoin';
|
|
|
|
import 'rxjs/add/observable/of';
|
|
|
|
|
2018-06-27 10:29:12 +02:00
|
|
|
import { Symbol } from '../cartography/models/symbol';
|
2017-09-25 13:07:52 +02:00
|
|
|
import { Server } from "../models/server";
|
|
|
|
import { HttpServer } from "./http-server.service";
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
constructor(private httpServer: HttpServer) { }
|
|
|
|
|
|
|
|
get(symbol_id: string): Symbol {
|
|
|
|
return this.symbols
|
|
|
|
.getValue()
|
|
|
|
.find((symbol: Symbol) => symbol.symbol_id === symbol_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
load(server: Server): Observable<Symbol[]> {
|
2018-04-16 08:24:49 +02:00
|
|
|
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));
|
2018-03-20 16:02:41 +01:00
|
|
|
Observable.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);
|
2018-04-16 08:24:49 +02:00
|
|
|
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) {
|
2017-09-25 13:07:52 +02:00
|
|
|
return this.httpServer
|
2018-03-20 16:02:41 +01:00
|
|
|
.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);
|
|
|
|
return this.httpServer
|
2018-03-20 16:02:41 +01:00
|
|
|
.getText(server, `/symbols/${encoded_uri}/raw`);
|
2017-09-25 13:07:52 +02:00
|
|
|
}
|
|
|
|
}
|