How to prevent simultaneous effect

I have an expensive calculation that is triggered by an effect. Now I want to make sure that this calculation is never called at the same time, i.e. If it is called a second time while the first call is still running, the second call should be ignored.

My approach to this problem was to create 2 actions: calculate and setLoading.

@Effect()
calculate$ = this.updates$
  .whenAction(CALCULATE)
  .flatMap(data => {
    console.debug('LOADING', data.state.loading);
    if (!data.state.loading) {
      this.store.dispatch(Actions.setLoading(true));
      await DO_THE_EXPENSIVE_CALCULATION();
      this.store.dispatch(Actions.setLoading(false));
    }
  });

with Actions.setLoading, explicitly setting state.loading. However, if I start to calculate 2 times in a row:

store.dispatch(Actions.calculate());
store.dispatch(Actions.calculate());

conclusion

LOADING false
LOADING false

and therefore, costly billing is done twice. How can I prevent this?

+4
source share
1 answer

LOADING false , Action.setLoading . / . .

, , , , , rxjs v4/v5:

: Batching using RxJS?

, , - :

@Effect()
calculate$ = this.updates$
  .whenAction(CALCULATE)
  .exhaustMap(data => DO_THE_EXPENSIVE_CALCULATION())
  ;

, DO_THE_EXPENSIVE_CALCULATION() ( ).

+6

Source: https://habr.com/ru/post/1655451/


All Articles