store icon indicating copy to clipboard operation
store copied to clipboard

state is being overwritten

Open tsawan opened this issue 8 years ago • 6 comments

State is being overwritten when multiple actions are dispatched together (as an array). This is happening as I tried to use the new way of calling async service and updating the state simultaneously.

Please see the demo app. check how first string "redux" disappears" when second string "ngxs" is set. https://stackblitz.com/edit/ngxs-simple-ghp1mx

tsawan avatar Mar 18 '18 10:03 tsawan

I did some investigation and I think that problem is with parameter deconstructing.

@Action(GetIt2)
getIt2({ state, setState }) {
  return Observable.of('ngxs').delay(2500)
  .pipe(tap(data => {
    setState({
      ...state,
      name2: data
    })

  }));
}

This piece of code gets transpiled to:

AppState.prototype.getIt2 = function (_a) {
    var state = _a.state, setState = _a.setState;
    return rxjs_1.Observable.of('ngxs').delay(2500)
        .pipe(operators_1.tap(function (data) {
        setState(__assign({}, state, { name2: data }));
    }));
};

As you can see, there's an assigment state = _a.state. This will trigger lazy state getter in state-factory.ts and therefore it will "cache" old state:

invokeActions(getState, setState, dispatch, action): any[] {
  const results = [];

  for (const metadata of this.states) {
    const name = getTypeFromInstance(action);
    const actionMeta = metadata.actions[name];

    if (actionMeta) {
      const stateContext = {
        get state(): any {      // <------------------------------ this getter
          const state = getState();
          return getValue(state, metadata.depth);
        },
        setState(val: any): void {
          let state = { ...getState() };
          state = setValue(state, metadata.depth, val);
          setState(state);
          return state;
        },
        dispatch(actions: any | any[]): Observable<any> {
          return dispatch(actions);
        }
      };

      const result = metadata.instance[actionMeta.fn](stateContext, action);
      if (result) {
        results.push(result);
      }
    }
  }

  return results;
}

If you change action definition to this:

@Action(GetIt2)
getIt2(context) {
  return Observable.of('ngxs').delay(2500)
  .pipe(tap(data => {
    context.setState({
      ...context.state,
      name2: data
    })

  }));
}

it will get's transpiled to:

AppState.prototype.getIt2 = function (context) {
    return rxjs_1.Observable.of('ngxs').delay(2500)
        .pipe(operators_1.tap(function (data) {
        context.setState(__assign({}, context.state, { name2: data }));
    }));
};

and it will work as expected.


@amcdnl This behaviour may be too confusing and provide unexpected results, so I would suggest to rather expose getState function in context instead of state getter.

bossqone avatar Mar 18 '18 11:03 bossqone

@bossqone awesome debugging. we should be able to at least write a test around this and see if the explicit function works compared to the getter (which I think it will)

deebloo avatar Mar 18 '18 13:03 deebloo

Here are examples:

  • using get state getter: https://codepen.io/bossqone/pen/OvbMMg
  • using getState function: https://codepen.io/bossqone/pen/MVbKKP

bossqone avatar Mar 18 '18 13:03 bossqone

@amcdnl what do you think?

deebloo avatar Mar 18 '18 14:03 deebloo

@bossqone - Your my hero for debugging this!

Sadly, I agree, we are gonna have to make it getState unless we tell people to not destructure it but i don't think thats a solution.

amcdnl avatar Mar 18 '18 16:03 amcdnl

Switched to getState in 2.0.0-rc.1

amcdnl avatar Mar 18 '18 16:03 amcdnl