Skip to content
This repository has been archived by the owner on Jan 10, 2018. It is now read-only.

initialState is not being invoked #273

Closed
alan-agius4 opened this issue Nov 17, 2016 · 11 comments
Closed

initialState is not being invoked #273

alan-agius4 opened this issue Nov 17, 2016 · 11 comments

Comments

@alan-agius4
Copy link

alan-agius4 commented Nov 17, 2016

initialState is not being invoked when it's a function

StoreModule.provideStore(_provideRootReducer, _initialState),


export function _provideRootReducer(state: any, action: any) {
	return startupContext.appContext.isDebug
		? compose(storeLogger(), combineReducers)(appReducer)(state, action)
		: combineReducers(appReducer)(state, action);
}

export function _initialState() {
	return {
		appContext: startupContext.appContext,
		market: {
			currentMarket: startupContext.market,
			markets: startupContext.marketList
		}
	};
}
@metanav
Copy link

metanav commented Nov 17, 2016

Does invoking it in provideStore not work?

StoreModule.provideStore(_provideRootReducer, _initialState())

@alan-agius4
Copy link
Author

Not wtih angular2 AOT, you cannot invoke functions

On Thu, 17 Nov 2016 at 16:38, Naveen [email protected] wrote:

Does invoking it in provideStore not work?

StoreModule.provideStore(_provideRootReducer, _initialState())


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#273 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AQv-WgSO5rE0j4gVoGcDi5czaZPJj8B9ks5q_HSOgaJpZM4K1cJS
.

@metanav
Copy link

metanav commented Nov 18, 2016

After #269 do we need to initialize ourselves?

@alan-agius4
Copy link
Author

@metanav, i think you referenced a wrong PR.

What the function invocation does is to return the object aka values for the initial state. There shouldn't be any changes from you jor any users which are not using AOT. If they arr using AOT, they now have an clean option how to provide a dynamic initial state

@bortexz
Copy link

bortexz commented Jan 14, 2017

Is there any workaround towards this? I use angular-cli and I have reducers as a function like:

export const authReducer: ActionReducer<AuthenticationState> =
(state: AuthenticationState = initialState, action: Action) => {
  console.log(action);
  switch (action.type) {
    case AuthenticationActions.LOGIN: {
      return null;
    }

    case AuthenticationActions.LOGIN_SUCCESS: {
      return action.payload;
    }

    case AuthenticationActions.LOGIN_ERROR: {
      return null;
    }

    case AuthenticationActions.LOGOUT: {
      return null;
    }

    default: {
      return state;
    }
  }
};

...

export function reducers () {
  return compose(localStorageSync(['authenticated'], true), combineReducers)({
    authenticated: authReducer
  });
};

...

imports: [
  StoreModule.provideStore(reducers)
]

I have effects setted up, and they work, but the reducer is never called, neither with initialState or after when I dispatch functions. Any idea? It may be related to this?

@florent1933
Copy link

@bertofer : I have exactly the same problem if you find a workaround it would be great 👍

@alan-agius4
Copy link
Author

alan-agius4 commented Jan 14, 2017 via email

@florent1933
Copy link

@alan-agius4 : I am going to to test your PR. Thanks a lot :)

@bortexz
Copy link

bortexz commented Jan 14, 2017

Hi @florent1933 , I could make it work by calling the reducer with state and action in the exported reducers function, like this:

export function reducers (state, action) {
  // return composed(state, action);
  return compose(localStorageSync(['authenticated'], true), combineReducers)({
    authenticated: authReducer
  })(state, action);
};

Being this the function provided to StoreModule.provideStore(reducers). Although I am not sure it belongs to this issue, for me the reducer was never being called, so I will probably open a new issue on this when I investigate more

@florent1933
Copy link

@alan-agius4 : I think our issue is not related to yours so your PR didn't fix it.

@bertofer : Great, it's working 👍 Congrat's!

@bfricka
Copy link

bfricka commented Jun 28, 2017

@bertofer This works if you're aware ahead of time of the keys you want to populate. However, this makes it static. In order be dynamic, what you really want is to be able to do something like:

export const initialState = {router: initialRouterState, ...someExternalInitialStateObject};

Amusingly, AoT will compile like this, but what it compiles to is a train wreck. Here is the compiled result of this expression in AoT:

this.__INITIAL_STATE_65 = { router: { path: 0 } };

You see, because AoT is requiring 100% static symbols, the only way to approach this is with functions, which this project does not support. Without this, I have no choice but to fork the project and implement it myself until upstream catches up.

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

No branches or pull requests

6 participants