6d8a5f6821a61bad1b9696de54c8aa6d67155f26
[src/xds/xds-agent.git] / webapp / src / app / services / sdk.service.ts
1 import { Injectable, SecurityContext } from '@angular/core';
2 import { Observable } from 'rxjs/Observable';
3 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
4
5 import { XDSAgentService } from "../services/xdsagent.service";
6
7 export interface ISdk {
8     id: string;
9     profile: string;
10     version: string;
11     arch: number;
12     path: string;
13 }
14
15 @Injectable()
16 export class SdkService {
17     public Sdks$: Observable<ISdk[]>;
18
19     private _sdksList = [];
20     private current: ISdk;
21     private sdksSubject = <BehaviorSubject<ISdk[]>>new BehaviorSubject(this._sdksList);
22
23     constructor(private xdsSvr: XDSAgentService) {
24         this.current = null;
25         this.Sdks$ = this.sdksSubject.asObservable();
26
27         this.xdsSvr.XdsConfig$.subscribe(cfg => {
28             if (!cfg || cfg.servers.length < 1) {
29                 return;
30             }
31             // FIXME support multiple server
32             //cfg.servers.forEach(svr => {
33             this.xdsSvr.getSdks(cfg.servers[0].id).subscribe((s) => {
34                 this._sdksList = s;
35                 this.sdksSubject.next(s);
36             });
37         });
38     }
39
40     public setCurrent(s: ISdk) {
41         this.current = s;
42     }
43
44     public getCurrent(): ISdk {
45         return this.current;
46     }
47
48     public getCurrentId(): string {
49         if (this.current && this.current.id) {
50             return this.current.id;
51         }
52         return "";
53     }
54 }