PatrickJS / angular-hmr

:fire: Angular Hot Module Replacement for Hot Module Reloading
https://patrickjs.com
Apache License 2.0
505 stars 46 forks source link

ngrx tutorial is not full #77

Open EugeneSnihovsky opened 5 years ago

EugeneSnihovsky commented 5 years ago

Hello. I spend about half of day and still can't get positive result. State data is not saved after reload. Please provide normal example of usage.

I already tried:

  1. Your repo tutorial
  2. https://medium.com/@beeman/tutorial-enable-hmr-in-angular-cli-apps-1b0d13b80130
  3. https://stackoverflow.com/a/48221333/4357871
  4. https://github.com/gdi2290/angular-hmr/pull/66#issuecomment-384275692
benjamin-wilson commented 5 years ago

Came up with this, seems to work

export function stateSetter(reducer: ActionReducer<any>): ActionReducer<any> {
  return function (state: any, action: any) {
    const NGRX_STATE = '___NGRX_STATE___';
    const newState = reducer(state, action);
    if (action.type !== '@ngrx/store/init') {
      return window[NGRX_STATE] = newState;
    } else {
      if (window[NGRX_STATE]) {
        return window[NGRX_STATE];
      }
      return newState;
    }
  };
}

export const metaReducers: MetaReducer<State>[] = !environment.production ? [storeFreeze, stateSetter] : [];