Antediluvian
Antediluvian

Reputation: 723

ngrx with boolean type

I have a state in Ngrx with a piece of boolean data:

export interface MyState {
    success?: boolean;
}

const initialState: MyState = { };

export const success = createSelector(myFeatureSelector, state => state.success);

So if I do the following:

myStateStore.select(success).subscribe(success => {
    // this will only emit if the state changes
});

So say if I dispatch 2 actions in sequence and both of them set the state to true, only the first one will emit a success value the 2nd one won't because the value is not changed. Is there a way to emit regardless?

Upvotes: 0

Views: 1248

Answers (2)

Hamza Zaidi
Hamza Zaidi

Reputation: 672

It is important to understand why selectors is not returning the value. Selectors are Memoized and does not execute if the value does not change. There is a way to skip the memoization

myStateStore.select(success).subscribe(success => {
    // this will only emit if the state changes
});

success.release(); // This will force the selector to not memoized.

Upvotes: 0

Rafi Henig
Rafi Henig

Reputation: 6422

You might want to add changeCount property to your state (which would get incremented on each change (or success)

export interface MyState {
  success?: boolean;
  changeCount: number;
}

Then change your code as following:

this.myStateStore
.pipe(
  select(changeCount),
  mergeMapTo(this.myStateStore.select(success))
)
.subscribe(success => {
 
});

Upvotes: 1

Related Questions