openaire-library/utils/properties/environment-specific.servic...

61 lines
1.8 KiB
TypeScript

import { Injectable, OnInit } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable, Subscription, BehaviorSubject } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/toPromise';
import { EnvProperties } from './env-properties';
@Injectable()
export class EnvironmentSpecificService {
public envSpecific: EnvProperties;
public envSpecificNull: EnvProperties = null;
private envSpecificSubject: BehaviorSubject<EnvProperties> = new BehaviorSubject<EnvProperties>(null);
constructor(private http: Http) {
console.log('EnvironmentSpecificService created');
}
public loadEnvironment() {
// Only want to do this once - if root page is revisited, it calls this again.
if (this.envSpecific === null || this.envSpecific === undefined) {
console.log('Loading env-properties.json');
return this.http.get('./assets/env-properties.json')
.map((data) => data.json())
.toPromise<EnvProperties>();
}
return Promise.resolve(this.envSpecificNull);
}
public setEnvProperties(es: EnvProperties) {
// This has already been set so bail out.
if (es === null || es === undefined) {
return;
}
this.envSpecific = es;
console.log(this.envSpecific);
if (this.envSpecificSubject) {
this.envSpecificSubject.next(this.envSpecific);
}
}
/*
Call this if you want to know when EnvProperties is set.
*/
public subscribe(caller: any, callback: (caller: any, es: EnvProperties) => void) {
this.envSpecificSubject
.subscribe((es) => {
if (es === null) {
return;
}
callback(caller, es);
});
}
}