ngxs / store

🚀 NGXS - State Management for Angular
http://ngxs.io
MIT License
3.54k stars 405 forks source link

🐞[BUG]: Adding plugins with `provideStates` does not work #2227

Closed Andreas-Hjortland closed 1 month ago

Andreas-Hjortland commented 1 month ago

Affected Package

The issue is caused by package @ngxs/store

Is this a regression?

No, although I have a project that works with @NgModule.

Description

If I add a plugin that i lazily load through the router, the plugin will not be invoked when we dispatch an action.

🔬 Minimal Reproduction

https://stackblitz.com/~/github.com/Andreas-Hjortland/ngxs-standalone-plugin-repro

We add the plugin in src/app/foo/routes.ts along with the state for that component, but the plugin is never invoked.

If you remove the comment for the ENVIRONMENT_INITIALIZER, it works. This hack just iterates over all the services registered in the dependency scope and finds the PluginManager based on the prototype and invokes it.

Proposed solution

I think the easiest way to fix this will be to add an ENVIRONMENT_INITIALIZER in the provideStates function which loads the PluginManager for that state so that we can ensure that the initialization logic which hydrates the root manager runs.

If you want to, I can create a pull request with this solution, or another solution if you guys have a better idea =)

🔥 Exception or Error

There is no exception or error, we just don't get any results

Environment


Libs:
- @angular/core version: 18.2.0
- @ngxs/store version: 18.1.1

Browser:
- [x] Chrome (desktop) version 129h
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [x] Firefox version 131
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX

For Tooling issues:
- Node version: 22.9
- Platform: Linux (Arch)

markwhitfeld commented 1 month ago

This will be resolved in the next release of NGXS. We will comment here when the release goes out.

Andreas-Hjortland commented 1 month ago

That’s awesome. Thank you so much for the work that you do =)

arturovt commented 1 month ago

You can use the dev version before the release goes out @ngxs/store@18.1.1-dev.master-a8f62df.