2018-09-04 12:33:21 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
2019-07-26 11:46:59 +02:00
|
|
|
import { BehaviorSubject, Observable } from 'rxjs';
|
2019-04-15 13:30:18 +02:00
|
|
|
|
2018-09-04 12:33:21 +02:00
|
|
|
import { Config } from '../../shared/models/core/config';
|
2019-01-31 13:40:27 +01:00
|
|
|
import { DataStoreService } from '../core-services/data-store.service';
|
2018-09-04 12:33:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for config variables.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* this.configService.get('general_event_name').subscribe(value => {
|
|
|
|
* console.log(value);
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* const value = this.configService.instant('general_event_name');
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2019-02-08 17:24:32 +01:00
|
|
|
export class ConfigService {
|
2018-09-04 12:33:21 +02:00
|
|
|
/**
|
|
|
|
* Stores a subject per key. Values are published, if the DataStore gets an update.
|
|
|
|
*/
|
|
|
|
private configSubjects: { [key: string]: BehaviorSubject<any> } = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen for changes of config variables.
|
|
|
|
*/
|
2018-09-13 14:40:04 +02:00
|
|
|
public constructor(private DS: DataStoreService) {
|
2019-05-06 11:44:56 +02:00
|
|
|
this.DS.getChangeObservable(Config).subscribe(data => {
|
2018-09-04 12:33:21 +02:00
|
|
|
// on changes notify the observers for specific keys.
|
2019-05-06 11:44:56 +02:00
|
|
|
if (this.configSubjects[data.key]) {
|
2018-09-04 12:33:21 +02:00
|
|
|
this.configSubjects[data.key].next(data.value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the constant named by key from the DataStore. If the DataStore isn't up to date or
|
|
|
|
* not filled via autoupdates the results may be wrong/empty. Use this with caution.
|
|
|
|
*
|
|
|
|
* Usefull for synchronos code, e.g. during generation of PDFs, when the DataStore is filled.
|
|
|
|
*
|
|
|
|
* @param key The config value to get from.
|
|
|
|
*/
|
2019-01-22 11:59:16 +01:00
|
|
|
public instant<T>(key: string): T {
|
2018-09-04 12:33:21 +02:00
|
|
|
const values = this.DS.filter<Config>('core/config', value => value.key === key);
|
|
|
|
if (values.length > 1) {
|
|
|
|
throw new Error('More keys found then expected');
|
|
|
|
} else if (values.length === 1) {
|
2019-01-22 11:59:16 +01:00
|
|
|
return values[0].value as T;
|
2018-09-04 12:33:21 +02:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an observer for the config value given by the key.
|
|
|
|
*
|
|
|
|
* @param key The config value to get from.
|
|
|
|
*/
|
2019-01-31 08:20:15 +01:00
|
|
|
public get<T>(key: string): Observable<T> {
|
2018-09-04 12:33:21 +02:00
|
|
|
if (!this.configSubjects[key]) {
|
|
|
|
this.configSubjects[key] = new BehaviorSubject<any>(this.instant(key));
|
|
|
|
}
|
2018-11-22 12:33:40 +01:00
|
|
|
return this.configSubjects[key].asObservable() as Observable<T>;
|
2018-09-04 12:33:21 +02:00
|
|
|
}
|
|
|
|
}
|