salesforce / akita

🚀 State Management Tailored-Made for JS Applications
https://salesforce.github.io/akita/
Apache License 2.0
3.7k stars 342 forks source link

Query select() – returns incorrect state value, once store update is called synchronisly from query subscription #1065

Open rohovtsov1 opened 1 year ago

rohovtsov1 commented 1 year ago

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