ng-turkey / ngxs-reset-plugin

Reset plugin for NGXS: Effortlessly clears, resets, or overwrites NGXS states respecting the state tree.
https://stackblitz.com/edit/ngxs-pizza
MIT License
108 stars 8 forks source link

StateResetAll remove all state keys except the one to keep #30

Closed yd021976 closed 4 years ago

yd021976 commented 4 years ago

Describe the bug When I want to reset my state to default initial state except one I use "StateResetAll", exemple this.store.dispatch(new StateResetAll(SiteZonesState))

My store has several states, like this

NgxsModule.forRoot([
            ApplicationState,
            UserState,
            UsersState,
            TemplatesState,
            AclUIState,
            AclEntitiesState,
            ApplicationLocksState,
            ServicesState,
            AppNotificationsState,
            SiteZonesState,
            SiteZonesUIState
        ])

When I want to reset my state to default state (init) except one, all of the keys of my state disappear except the one in my exemple. So after StateResetAll action dispatch, my state looks like :[SiteZonesState] instead of [ApplicationState, UserState,UsersState,TemplatesState,AclUIState,AclEntitiesState,ApplicationLocksState,ServicesState,AppNotificationsState,SiteZonesState,SiteZonesUIState]

Possible bug In debug, I see that ResetHandler constructor seem to never "catch" InitState action, and then never set initialState property of ResetService. As the resetService.initialState in your plugin is always "undefined", when I dispatch a ResetStateAll, the plugin set my state with the "undefined" state value stored in ResetService.

NGXS version : 3.6.2

yd021976 commented 4 years ago

After some debug of the plugin, I can confirm that the "InitState" action is never "received" by the actions$ Observable in the reset handler. I don't know why.

Note that in any of my state classes, I can receive "InitState" action from NGXS.

Here the plugin code that is never executed and cause the ResetPlugin.InitialState to never be initialized with the initial store state at startup.

Source path : src/lib/reset.handler.ts The code below is never executed (lines 13 to 18)

this.actions$
      .pipe(
        ofActionSuccessful(InitState),
        take(1),
      )
      .subscribe(() => (this.resetService.initialState = this.store.snapshot()));
armanozak commented 4 years ago

Hi @yd021976,

I think you are not calling the static forRoot method of NgxsResetPluginModule which hooks into the action stream via APP_INITIALIZER. Please call the forRoot method as described here.

Let us know if this works.

yd021976 commented 4 years ago

Hi, thanks for your reply. Unfortunably i confirm i use forRoot static method of the plugin module and it still doesn’t work.

armanozak commented 4 years ago

Is it called in the root module of your application? Could you please share a minimum reproduction of the issue? You can use a playground such as StackBlitz.

By the way, here is a quick playground that shows it is working.

yd021976 commented 4 years ago

My bad, it was very tricky to reproduce the error. I start from a scratch Angular 9 project, and finally find what was going wrong.

Simply, we MUST import the BrowserModule before importing NgxsModule.forRoot()

I just don't know why I have to do this, but now I've turn into a new problem because my app_initializer (using NGXS store) doesn't work with BrowserModule imported before NgxsModule.forRoot().

So as I've found MY error, I think you can close this issue.

Thank you for your help and quick answer

armanozak commented 4 years ago

You're welcome. Have a nice day. 🙂

heavybeard commented 3 years ago

Hey! One year later I landed in this issue too.

I can confirm that if you move the BrowserModule import right after the NgxsModule the NgxsResetPluginModule will erase all state keys except the ones to keep.

I will be back soon with an example.

heavybeard commented 3 years ago

Here is the example

You can move BrowserModule above the NgxsModule import to let the NgxsResetPluginModule works properly