live demo https://stackblitz.com/edit/angular-vw78jf
There is ToppingsStateModel in my ngxs state
export interface ToppingsStateModel {
entities: { [ id: number ]: Topping };
selectedToppings: number[];
}
One action change my entities list, another action change selectedToppings. In products.component I get list of toppings from selector
export class ToppingsState {
constructor(private toppingsService: ToppingsService) {
}
@Selector()
static entities(state: ToppingsStateModel) {
console.log('getEntities', state.entities);
return state.entities;
}
@Selector([ToppingsState.entities])
static toppings(state: ToppingsStateModel, entities: {[id: number]: Topping}): Topping[] {
return Object.keys(entities).map(id => entities[parseInt(id, 10)]);
}
...
}
and it's product.component
export class ProductsComponent implements OnInit {
@Select(ToppingsState.toppings) toppings$: Observable<Topping[]>;
constructor(private store: Store, private actions$: Actions) {}
ngOnInit() {
const state = this.store.dispatch(new LoadToppings());
setTimeout(() => this.store.dispatch(new VisualizeToppings([1])), 2000);
this.toppings$.subscribe((toppings) => {console.log('UUUU NEW TOPPINGS!!!')});
}
}
when I dispatch VisualizeToppings action I get new toppings value. In my console I have
action [Products] Load Toppings @ 10:57:59.735
getEntities {}
UUUU NEW TOPPINGS!!!
getEntities {1: {…}, 2: {…}}
UUUU NEW TOPPINGS!!!
action [Products] Visualize Toppings @ 10:58:01.744
getEntities {1: {…}, 2: {…}}
UUUU NEW TOPPINGS!!!
I changed another part of the state. Why did selectors execute again when I dispatched VisualizeToppings action? What do I do wrong?