ngxs / store

πŸš€ NGXS - State Management for Angular
http://ngxs.io
MIT License
3.54k stars 402 forks source link

After reload state form local storage, state$.subscribe() give back an empty state #122

Closed labs-scnm closed 6 years ago

labs-scnm commented 6 years ago

I use the NgxsLocalStoragePluginModule. After reload the app, the state will load from local storage correctly (state show correctly on dev tools), but the subscribe give me an null value.

@Select(TmpState) tmpState$: Observable<any>;

constructor() {
  this.tmpState$.subscribe((state) => {
    console.log(state); // null
  });
}

Any idea what I'm doing wrong?

deebloo commented 6 years ago

@labs-scnm what version are you using?

labs-scnm commented 6 years ago

I tested with 2.0.0-rc.10 and 2.0.0-rc.12.

deebloo commented 6 years ago

@labs-scnm could you try not in the constructor? (different lifecycle hook or in the template) and see what that result is?

labs-scnm commented 6 years ago

I tested it before on ngOnInit(), same result.

deebloo commented 6 years ago

@labs-scnm what about spitting it out in the template? I am only asking because our integration app isn't having this problem

deebloo commented 6 years ago

@labs-scnm can you provide a repo or stackblitz that reproduces the issue?

labs-scnm commented 6 years ago

On the template is the same result, it's empty.

It seems, the store init will dispatch before restore the state.

Ok, I will try to provide an example.

labs-scnm commented 6 years ago

Ok, I found the problem. The restore dosen't work with lazy loading!

Here are an working example: https://stackblitz.com/edit/ngxs-localstorage

deebloo commented 6 years ago

@labs-scnm excellent thank you!

labs-scnm commented 6 years ago

Welcome! Hope you find a solution. ☺️

deebloo commented 6 years ago

ha me too :)

deebloo commented 6 years ago

@labs-scnm we just pushed an update to lazy loading and feature modules. could you give it a shot?

labs-scnm commented 6 years ago

Sry, but still same result. Empty result after reload. πŸ˜”

screenshot

I updated also the example app, so you can verify the result: https://stackblitz.com/edit/ngxs-localstorage

labs-scnm commented 6 years ago

I tested some possible scenarios, it seams the lazy state is loading to early from the local storage, also if I'm not running the lazy module.

Some Details If I have an lazy module with own (lazy) state, normally the state will be loaded first with the module. I can see an empty state on the DevTools, before I load the lazy module. But if the state restored from the local storage, the state is still exist, also before the the lazy module loaded.

I update my example app, to run and test this scenario.

Hope this will help to find the issue! πŸ™

deebloo commented 6 years ago

@labs-scnm thanks for the info (and patience) I still haven't figured this one out and im burned out for the weekend. Ill try to get back to it something this week. could also use some help if you have any ideas