diff --git a/docs/core/app-config.service.md b/docs/core/app-config.service.md index fb4728092d..05d615c5d6 100644 --- a/docs/core/app-config.service.md +++ b/docs/core/app-config.service.md @@ -98,3 +98,28 @@ The supported variables are: | ------------- | ------------- | | hostname | `location.hostname` | | port | `location.port` | + +## App Config onLoad Stream +When the app config is loaded correctly an onChange event is sent with the whole set app config properties. This comes in handy when a component wants to react to some property change or interact with the app config when it's correctly loaded. + +```ts + + appConfig.onLoad.subscribe((appConfig) => { + console.log(appConfig); //this is the representation of the app-config + }); +``` + +We have added also the `select` method where the user can give the property name which wants to be notified the when the app config is loaded and get the value. + +```json + appconfig : { + logLevel : 'trace' + } +``` + +```ts + + appConfig.select('logLevel').subscribe((logLevelValue) => { + console.log(logLevelValue); //this will be 'trace'; + }); +``` \ No newline at end of file diff --git a/lib/core/app-config/app-config.service.spec.ts b/lib/core/app-config/app-config.service.spec.ts index 755dd0bfa8..17af4a76e6 100644 --- a/lib/core/app-config/app-config.service.spec.ts +++ b/lib/core/app-config/app-config.service.spec.ts @@ -16,7 +16,7 @@ */ import { HttpClientModule } from '@angular/common/http'; -import { inject, TestBed } from '@angular/core/testing'; +import { async, inject, TestBed } from '@angular/core/testing'; import { AppConfigService } from './app-config.service'; import { AppConfigModule } from './app-config.module'; @@ -72,6 +72,20 @@ describe('AppConfigService', () => { expect(appConfigService).toBeDefined(); }); + it('should stream only the selected attribute changes when using select', async(() => { + appConfigService.config.testProp = true; + appConfigService.select('testProp').subscribe((property) => { + expect(property).toBeTruthy(); + }); + })); + + it('should stream the page size value when is set', async(() => { + appConfigService.config.testProp = true; + appConfigService.onLoad.subscribe((config) => { + expect(config.testProp).toBeTruthy(); + }); + })); + it('should skip the optional port number', () => { appConfigService.config.testUrl = 'http://{hostname}{:port}'; diff --git a/lib/core/app-config/app-config.service.ts b/lib/core/app-config/app-config.service.ts index 47cdbc8e64..276842329e 100644 --- a/lib/core/app-config/app-config.service.ts +++ b/lib/core/app-config/app-config.service.ts @@ -18,6 +18,8 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { ObjectUtils } from '../utils/object-utils'; +import { BehaviorSubject } from 'rxjs/BehaviorSubject'; +import { Observable } from 'rxjs/Observable'; @Injectable() export class AppConfigService { @@ -34,7 +36,16 @@ export class AppConfigService { alfrescoRepositoryName: 'alfresco-1' }; + private onLoadSubject: BehaviorSubject; + onLoad: Observable; + constructor(private http: HttpClient) { + this.onLoadSubject = new BehaviorSubject(this.config); + this.onLoad = this.onLoadSubject.asObservable(); + } + + select(property: string): Observable { + return this.onLoadSubject.map((config) => config[property]).distinctUntilChanged(); } get(key: string, defaultValue?: T): T { @@ -65,6 +76,7 @@ export class AppConfigService { this.http.get('app.config.json').subscribe( (data: any) => { this.config = Object.assign({}, this.config, data || {}); + this.onLoadSubject.next(this.config); resolve(this.config); }, () => { diff --git a/lib/core/services/user-preferences.service.ts b/lib/core/services/user-preferences.service.ts index 90b87b16c6..5b4be14a6a 100644 --- a/lib/core/services/user-preferences.service.ts +++ b/lib/core/services/user-preferences.service.ts @@ -61,7 +61,7 @@ export class UserPreferencesService { private storage: StorageService, private apiService: AlfrescoApiService ) { - this.initUserPreferenceStatus(); + this.appConfig.onLoad.subscribe(this.initUserPreferenceStatus.bind(this)); this.localeSubject = new BehaviorSubject(this.defaults.locale); this.locale$ = this.localeSubject.asObservable(); this.onChangeSubject = new BehaviorSubject(this.userPreferenceStatus);