Missing important recipe to use store for "library" modules #3091
Replies: 8 comments
-
For services (effects) it works because the modules are lazy loaded. Because of this, angular creates a new injector context, and all services which are provided in your generic module will get instantiated again. If it works with StoreModule.forFeature like this with always the same name I don't know. There also is a stackoverflow post related to your question: https://stackoverflow.com/questions/49537571/independent-instances-of-the-same-ngrx-feature-module |
Beta Was this translation helpful? Give feedback.
-
Yeah, i know IT works And why it works (checked reducer Merge logic in forFeature method)..my issues Is about missing important recipe in documentation. I Will create MR if NGRX team Is interested. |
Beta Was this translation helpful? Give feedback.
-
@montella1507 you should look into the work that @jrista is doing with NgRx auto entity. He’s using tokens to provide and over specific library level effects and reducers. |
Beta Was this translation helpful? Give feedback.
-
@montella1507 As @wesleygrimes mentioned, I have used NgRx in a library I've created called NgRx Auto-Entity. In this library, I automatically register reducers (specifically, meta-reducers, which are a more viable approach in a library), as well as effects (although the functions I am using are not really documented, so we should either get them documented properly and make them official, or create another/better official way to register library effects in NgRx.) In any case, I was able to fully encapsulate both reducers and effects within my library. My library's NgModules have |
Beta Was this translation helpful? Give feedback.
-
Hi everyone, I also want to use the store in my libraries without them having dependencies on main app. Could you please guide me through? |
Beta Was this translation helpful? Give feedback.
-
Mainly commenting to watch this issue. Is this related to #1734? I’ve recently been doing a deeper dive into what NgRx Data is doing under the hood and it’s not easier to see how it could form a quick recipe...facade pattern that dispatches actions and provides selectors, factory pattern for data and collection services, services that keep registry of classes, binding reducer methods to actions, effects class to persist data calling services. Just graphing the internal imports of the library reveals the difficulty of which order to write these in if you wanted to replicate this library as a “pattern”. The data and collection classes are easy to extend. Extending the effects or actions is much much harder. |
Beta Was this translation helpful? Give feedback.
-
Hey, just like @pedy711 I'm have lots of trouble doing this. Especially seeing
Due to Ivy is not ready recommended yet, so not sure what to do. |
Beta Was this translation helpful? Give feedback.
-
Even though we have not released a bigger version in a while, we have actually had NgRx Auto-Entity under continued development for most of the year. We are working on a 0.5.0 release which adds a lot of improvements to auto-entity. We will be following up with a 0.6.0 release which improves alignment with how NgRx 8+ work, and 0.7.0 with additional features and improved ng/ngrx support in the fall. We think we've taken Auto-Entity beyond any other entity option so far, an with 0.6.0 will provide a means of customizing and integrating support into your own ngrx code that we don't believe has been achieved with any other ngrx addon library yet. Might be worth taking a look. You can install 0.5.0-beta.1 to get a look at the most recent changes. 0.6.0-alphas should be dropping soon. |
Beta Was this translation helpful? Give feedback.
-
We can find in documentation nice info about common usage of NGRX/Store. However, it is scoped to "root" and "feature" modules - it is subconsciously connected to AppModule (or CoreModule) and LazyLoaded modules.
I think one of the biggest power of Store is, it can be used similar to NGRX/Data for custom libraries.
To explain my proposal, lets have following parts in an application:
So usage will be like..
Lets say, we would like to have 3 Lazy-loaded feature modules like:
with their own grids connected to NGRX/Store and our "grid-data library" (backend meant as logic - generic reducers, actions, effects, crud services.. - similar to NGRX/Data)
What is the best way to do so? At the moment i think it is possible in this way:
StoreModule.forFeature('grids', genericGridReducer),
EffectsModule.forFeature([genericGridEffects]),
]
It works... more.. it works like a charm, but is it correct way? Why does it work?
Why is it possible to import multiple times (in lazy loaded modules) a module with the same forFeature() definition? Why it isnt dropped and recreated?
Why it is possible to register multiple times the same effects, do we have multiple instances then?
I think this way (aka SomethingDataModule) is so powerfull concept of NGRX/xxxx it is worth to write about the correct way in the docs.
I would be willing to submit a PR for the docs ❤️
[x] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No
Beta Was this translation helpful? Give feedback.
All reactions