i have some experience with JavaScript and angular but i cant quite get my head around some concepts. I am trying to Build a service to store some settings that is able to notify components that implement it of changes have occurred.
Setting up setters that call a onChange function for each property seemed the to be the old fashioned way and i got it working. But i does not feel like the angular way, as it bloats the class and for each property i want to add i would need to implement new get & set methods. So i'd like to have a interface that calls the onChange function each time a property was set to something new. But i cant find the syntax for this.
import { Injectable} from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class serviceSettings {
private constructed: boolean = false;
private _targetFPS: number = 30;
private _showFPS: boolean = true;
private _viewDistance: number = 7000;
private _enabelAntialiasing: boolean = true;
private settingsChanged = new Subject();
constructor() {
if (this.constructed) {
console.log('serviceSettings aready exists');
return this
}
this.constructed = true;
}
onChange() {
this.settingsChanged.next();
//save setting in local storage
//send them to a api to store them server sided
console.log('settings changed');
}
onChangeNotice():Observable<any> {
return this.settingsChanged.asObservable();
}
set targetFPS(val:number) {
this._targetFPS = val
this.onChange()
}
get targetFPS():number{
return this._targetFPS
}
set showFPS(val:boolean) {
this._showFPS = val
this.onChange()
}
get showFPS():boolean {
return this._showFPS
}
set enabelAntialiasing(val:boolean) {
this._enabelAntialiasing = val
this.onChange()
}
get enabelAntialiasing():boolean {
return this._enabelAntialiasing
}
set viewDistance(val:number) {
this._viewDistance = val
this.onChange()
}
get viewDistance():number{
return this._viewDistance
}
}
In this particular context it is a settings service for a little game-"engine" i am working on with THREE.js. It needs to reinstatiate the renderer if i want to enable/disable anti-aliasing but not if any other settings have changed.
TL:DR: I have to react on changes differently depending on what has changed.