store: Life-cycle events not triggered in root state

I’m submitting a…


[ x ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => https://github.com/ngxs/store/blob/master/CONTRIBUTING.md
[ ] Other... Please describe:

Current behavior

  1. Implement NgxsOnInit and NgxsAfterBootstrap in a state that is imported with root. In our case the state was AuthState:
 NgxsModule.forRoot(
      [
        AuthState,
        // follwed by three other states
      ],
      {
        developmentMode: !environment.production,
      }
    ),
// importing other plugins
// importing module with a NgxsModule.forFeature
  1. Load the app

  2. Neither life-cycle events are triggered.

Expected behavior

Expecting life-cycle events to be triggered when loading the app at the specified timings described in the documentation.

This is still working in versions prior to 3.4.0 such as 3.3.4.

Minimal reproduction of the problem with instructions

Was unable to reproduce in a minimal reproduction. So assuming it must be some edge-case to do with import order of NGXS modules similar to other issues: #539 and #375. The difference with this bug is that the life-cycle is called in a state initialized by forRoot.

Here is a more advanced version of the minimal demo that still emits life-cycle events: https://stackblitz.com/edit/ngxs-simple-k8i8y6

What is the motivation / use case for changing the behavior?

Want to use life-cycle hooks to set authentication state.

Environment


Libs:
- @angular/core version: 7.2.7
- @ngxs/store version: 3.4.0/3.4.2
Also these plugins in-use:
```
    "@ngxs/devtools-plugin": "3.4.2",
    "@ngxs/logger-plugin": "3.4.2",
    "@ngxs/router-plugin": "3.4.2",
    "@ngxs/storage-plugin": "3.4.2",
```


Browser:
- [ x ] Chrome (desktop) version 73
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: 8.11.3  
- Platform:  Windows and Mac 

Others:

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 32 (18 by maintainers)

Most upvoted comments

@paullryan @Veetaha @paulstelzer @Koslun @P-de-Jong @sroettering @markwhitfeld The bug has been fixed and is awaiting approval. We plan to release version 3.5.0 as soon as possible.

@splincode I could create a example app to reproduce -> https://github.com/paulstelzer/ngxs-issue

Inside app.module.ts you find integratin of Ngxs and LanguageModule. LanguageModule is packed with ngpackagr (source code -> https://github.com/paulstelzer/innomobile-library/tree/master/libs/%40innomobile/language). That’s the language state -> https://github.com/paulstelzer/innomobile-library/blob/master/libs/%40innomobile/language/src/lib/state/language.state.ts

As you can see inside ngxsoninit normally the default language will be set, If you serve the page (ng serve) for the first time instead of “HOME.TITLE” you will see the correct translated string. The second time you will see “HOME.TITLE”. The reason: ngxsOnInit is not called this time

If you remove NgxsStoragePluginModule.forRoot() in app.module.ts everything is working again

The code works earlier without problems, but since 3.4.0 it’s not working anymore

2019-04-07_09-45-28

@splincode I made a stackblitz at https://stackblitz.com/edit/ngxs-issue-917?file=app%2Fapp.module.ts to help with troubleshooting. Just comment out the storage plugin in app module to it work as intended.

Waiting for fix ☕☕☕

This issue is preventing us from upgrading 😢

@paullryan @Veetaha @paulstelzer @Koslun @P-de-Jong @sroettering Guys, please check out the latest changes before release!

$ npm install @ngxs/store@dev

@splincode Great! When it’s released, I will upgrade and check if everything works as expected 👍

While I can not fix it, but I still hope to fix it soon