I am at a loss here - no doubt due to my lack of depth of understanding of the store and RxJS. I have converted an existing part of my app using the store as a way to understand the benefits. My first lack of understanding was that, if my component subscribes to store state updates, the subscriptions get fired EVERY time the state changes even though I may only be interested in a single state property which may not have changed. I want it to behave more like an observable so that I react when changes I am interested in happen, but ignore it otherwise. Enter RxJS filtering with distinctUntilChanged and distinctUntilKeyChanged. Fantastic, but I simply cannot get it to work in a consistent way.
My State Definition
export interface IAppState {
profile: UserProfile;
agreements: Array<Agreement>;
newAgreement:Agreement;
newAgreementState:NewAgreementState;
}
NewAgreementState object property
export class NewAgreementState {
public agreementId:string = "";
public currentState:number = 0;
}
When I subscript to state changes in a component using the distinctUntilChanged filter I only see changes when I observe the primitive values. The subscription below shows the correct comparison between previous and new value.
const sub3 = this.store.state.pipe(pluck("newAgreementState"),pluck("currentState"),distinctUntilChanged((x,y) => {
this.logger.debug("aurelia store - compare primitive values: ", x, y);
return x === y;
}))
.subscribe((s) => {
this.logger.debug("aurelia store - primitive value changed: ", s);
});
this.subscriptions.push(sub3);
If I create a second subscription and use distinctUntilKeyChanged I do not detect any changes. It sees previous and current values the same - both the updated state values
const sub2 = this.store.state.pipe(pluck("newAgreementState"),distinctUntilKeyChanged("currentState",(x,y) => {
this.logger.debug("aurelia store - compare key values: ", x, y);
return x === y;
}))
.subscribe((s) => {
this.logger.debug("aurelia store - key values changed: ", s.currentState);
});
this.subscriptions.push(sub2);
Can anyone point me the the right direction??? Is there something I need to do to my State classes which will make it work>
Any help very much appreciated.
Jonathan