akita
akita copied to clipboard
Query select() – returns incorrect state value, once store update is called synchronisly from query subscription
Description
A store with two counters:
export interface CounterStoreState extends State {
counter1: number;
counter2: number;
}
@Injectable()
@StoreConfig({ name: 'counter' })
export class CounterStore extends Store<CounterStoreState> {
constructor() {
super({
counter1: 0,
counter2: 0,
});
}
}
AppComponent
export class AppComponent {
constructor(readonly counterStore: CounterStore) {
//Once counter1 increases, update counter2, synchronously
counterStore._select((state) => {
if (state.counter1 !== state.counter2) {
counterStore.update({
counter2: state.counter1
})
}
}).subscribe();
//Print counter1, counter2
counterStore._select((state) => {
return `counter1: ${state.counter1} counter2: ${state.counter2}`;
}).subscribe(console.log);
counterStore.update({ counter1: 1 });
}
}
Print output:
counter1: 0 counter2: 0
counter1: 1 counter2: 1
counter1: 1 counter2: 0
Conclusion The store will emit an old value in the end, because of the nested update() call