TKDev
TKDev

Reputation: 503

Combining multiple/redundent ngrx Store select statement

Im developing an application using ngrx/angular4+ . A design pattern thats being developed here seems very redundent, and it has to do with how you subscribe to the Store reducers.

In a component the following situation is not uncommon:

HOFDetails$:Observable<any>;
HOFDetails$SUB:ISubscription;
HOFMobile$:Observable<any>;
HOFMobile$SUB:ISubscription;
HOFMobile:any;
HOFHome$:Observable<any>;
HOFHome$SUB:ISubscription;
HOFHome:any;
SPDetails$:Observable<any>;
SPDetails$SUB:ISubscription;
SPMobile$:Observable<any>;
SPMobile$SUB:ISubscription;
SPMobile:any;
SPHome$:Observable<any>;
SPHome$SUB:ISubscription;
SPHome:any;
EmergencyDetails$:Observable<any>;
EmergencyDetails$SUB:ISubscription;
EmergencyDetails:any;
EmergencyMobile$:Observable<any>;
EmergencyMobile$SUB:ISubscription;
EmergencyMobile:any;
EmergencyHome$:Observable<any>;
EmergencyHome$SUB:ISubscription;
EmergencyHome:any;
EmergencyList$:Observable<any>;
EmergencyList$SUB:ISubscription;
EmergencyList:any;
countries$:Observable<any>;
countries$SUB:ISubscription;
countries:any;
valueIds$:Observable<any>;
valueIds$SUB:ISubscription;
valueIds:any;
lang$:Observable<any>;
lang$SUB:ISubscription; 
//the subscriptions
    this.lang$ =  this.store.select(fromroot.getLanguage);
    this.lang$SUB = this.lang$.subscribe((v:string)=>this.lang = v);
    this.HOFDetails$ = this.store.select(from_costumer.getHOF);
    this.HOFDetails$SUB = this.HOFDetails$.subscribe((v)=> {});
    this.HOFMobile$ = this.store.select(from_costumer.getHOFMobile);
    this.HOFMobile$SUB = this.HOFMobile$.subscribe((v)=> {});
    this.HOFHome$ = this.store.select(from_costumer.getHOFHome);
    this.HOFHome$SUB = this.HOFHome$.subscribe((v)=> {});
    this.SPDetails$ = this.store.select(from_costumer.getSP);
    this.SPDetails$SUB = this.SPDetails$.subscribe((v)=> {});
    this.SPMobile$ = this.store.select(from_costumer.getSPMobile);
    this.SPMobile$SUB = this.SPMobile$.subscribe((v)=> {});
    this.SPHome$ = this.store.select(from_costumer.getSPHome);
    this.SPHome$SUB = this.SPHome$.subscribe((v)=> {});
    this.EmergencyDetails$ = this.store.select(from_costumer.getEmergency);
    this.EmergencyDetails$SUB = this.EmergencyDetails$.subscribe((v)=>this.EmergencyDetails = v);
    this.EmergencyList$ = this.store.select(from_costumer.getEmergencyList);
    this.EmergencyList$SUB = this.EmergencyList$.subscribe((v:any)=> {});
    this.EmergencyMobile$ = this.store.select(from_costumer.getEmergencyMobile);
    this.EmergencyMobile$SUB = this.EmergencyMobile$.subscribe((v:any)=> {});
    this.EmergencyHome$ = this.store.select(from_costumer.getEmergencyHome);
    this.EmergencyHome$SUB = this.EmergencyHome$.subscribe((v:any)=> {});
    this.countries$ = this.store.select(from_geo.getCountries);
    this.countries$SUB = this.countries$.subscribe((v:any)=> this.countries = v);
    this.valueIds$ = this.store.select(from_geo.getCountryIds);
    this.valueIds$SUB = this.valueIds$.subscribe((v:any)=>this.valueIds = v);

All of this code for only 13 properties on this, not including the assignment logic inside the subscriptions & the unsubscribing .

This seems to me incredibally repetative & redundent.

Is there a way to pack all this up into somekind of a reusable package? Maybe component based, or route based?

Upvotes: 0

Views: 390

Answers (2)

Hinrich
Hinrich

Reputation: 14013

Shorten it to this in your container component:

// inject ngrx store in constructor
constructor(private store: Store) {}

// lang$ as class property, no extra code needed.
lang$: Observable<any> = this.store.select(fromroot.getLanguage);

You probably do not need to set local prop this.lang, just pass the observables to any of your presentational components and use async pipes.

Upvotes: 1

tlt
tlt

Reputation: 15271

remove subscriptions, remove unpacked variables (e.g. countries:any;). what you're left with are Observables only. have them in container (smart) components and bind those observables to your (dumb) presentational components using async pipe.

for example:

lang$ : Observable<whatevertype>;

constructor(){
   this.lang$ =  this.store.select(fromroot.getLanguage);
}

in template:

<your-presentational-component [lang]="lang$ | async"><your-presentational-component>

then async pipe takes over subscribing, unsibscribing and assigning actual values to child component where you actually use them.

Upvotes: 2

Related Questions