import { Injectable, InjectionToken, Inject } from '@angular/core'; import { BehaviorSubject, Observable } from 'rxjs'; import { HttpConfig } from '../types'; export const HTTP_CONFIG = new InjectionToken('HttpCustomConfig'); @Injectable() export class MwHttpConfigService { private configSubject$: BehaviorSubject; private readonly defaultConfig: any; constructor(@Inject(HTTP_CONFIG) config: any) { // Set the default config from the user provided config (from forRoot) this.defaultConfig = config; this.configSubject$ = new BehaviorSubject( Object.assign({}, this.defaultConfig) ); } set config(value) { this.setConfig(value); } get config(): any | Observable { return this.getConfig(); } setConfig(value: HttpConfig, opts = { emitEvent: true }): void { // Get the value from the behavior subject let config = this.configSubject$.getValue(); // Merge the new config config = Object.assign({}, config, value); // If emitEvent option is true... if (opts.emitEvent === true) { // Notify the observers this.configSubject$.next(config); } } getConfig(): Observable { return this.configSubject$.asObservable(); } }