Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rohovtsov1 opened this issue Apr 28, 2023 · 0 comments

Comments

@rohovtsov1
Copy link

rohovtsov1 commented Apr 28, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant