Lazy load feature modules, ngrx action trigged before reducer is initialize











up vote
0
down vote

favorite












I'm facing following problem. My action is fired before my reducer is initialised. For this reason my reducer don't pick up the action. How can I affect it that my reducer picks up actions when fired before my reducer has been initialised?



This image shows you the redux flow.



enter image description here



My action is dispatched from a ConfigurationEffect. And should be captured by different XFeature-reducers these reducers are feature based reducers



Code example:



app module



@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
CommonModule,
AppRoutingModule,
HttpClientModule,
StoreModule.forFeature('config', fromConfig.reducer),
EffectsModule.forFeature([ConfigEffects]),
EffectsModule.forRoot([AppEffects]),
StoreModule.forRoot(reducers, { metaReducers }),
StoreDevtoolsModule.instrument({
maxAge: 10,
name: 'Debug DevTools',
logOnly: true,
actionSanitizer,
stateSanitizer,
}),
],
bootstrap: [AppComponent],
})
export class AppModule {}


config effects



@Effect()
loadConfigs$ = this.actions$.pipe(
ofType(configActions.ConfigActionTypes.LoadConfigs),
switchMap((action: configActions.LoadConfigs) =>
this.configService.loadConfig().pipe(
map((result: ConfigResponse) => new configActions.SaveConfigs(result)),
catchError((err: HttpErrorResponse) => of(new configActions.FailConfigs(err.message))),
),
),
);


xFeature reducer



export function reducer(state = initialState, action: XFeatureActions | ConfigActions): XFeatureState {
switch (action.type) {
// save configs, this will be called when the device is initially loaded.
case ConfigActionTypes.SaveConfigs:
...
})









share|improve this question
























  • you should provide a bit more details. most important, where is your action being dispatched from?
    – dee zg
    Nov 8 at 8:51










  • Improved with code example
    – Bo Vandersteene
    Nov 8 at 9:03










  • hm...that doesn't look like the whole picture. So, usual setup with lazy loaded store is: you have AppModule in which you register main reducers&effects using .forRoot(...). Then, you have your lazy loaded angular module, e.g. MyLazyModule in which you register your lazy loaded ngrx reducers/effects the same way you do in your AppModule with the difference that now you use .forFeature(...) instead of .forRoot(...). In short, remove .forFeature(...) registrations from your AppModule and put them into your lazy loaded module.
    – dee zg
    Nov 8 at 9:10










  • But then my other reducers are still loaded after the action has been send. The only way to affect a solution for now is to load all reducers on startup time, which I want to avoid. There should be like a replay action or something else
    – Bo Vandersteene
    Nov 8 at 9:15










  • which other reducers? i am sorry, i might be missing something, but i have hard time understanding your overall app structure. in particular, which angular modules you have and which corresponding ngrx module you have and then what actions (from which ngrx module) are fired where and cause problems. It would be beneficial if you could clarify that a bit.
    – dee zg
    Nov 8 at 9:16















up vote
0
down vote

favorite












I'm facing following problem. My action is fired before my reducer is initialised. For this reason my reducer don't pick up the action. How can I affect it that my reducer picks up actions when fired before my reducer has been initialised?



This image shows you the redux flow.



enter image description here



My action is dispatched from a ConfigurationEffect. And should be captured by different XFeature-reducers these reducers are feature based reducers



Code example:



app module



@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
CommonModule,
AppRoutingModule,
HttpClientModule,
StoreModule.forFeature('config', fromConfig.reducer),
EffectsModule.forFeature([ConfigEffects]),
EffectsModule.forRoot([AppEffects]),
StoreModule.forRoot(reducers, { metaReducers }),
StoreDevtoolsModule.instrument({
maxAge: 10,
name: 'Debug DevTools',
logOnly: true,
actionSanitizer,
stateSanitizer,
}),
],
bootstrap: [AppComponent],
})
export class AppModule {}


config effects



@Effect()
loadConfigs$ = this.actions$.pipe(
ofType(configActions.ConfigActionTypes.LoadConfigs),
switchMap((action: configActions.LoadConfigs) =>
this.configService.loadConfig().pipe(
map((result: ConfigResponse) => new configActions.SaveConfigs(result)),
catchError((err: HttpErrorResponse) => of(new configActions.FailConfigs(err.message))),
),
),
);


xFeature reducer



export function reducer(state = initialState, action: XFeatureActions | ConfigActions): XFeatureState {
switch (action.type) {
// save configs, this will be called when the device is initially loaded.
case ConfigActionTypes.SaveConfigs:
...
})









share|improve this question
























  • you should provide a bit more details. most important, where is your action being dispatched from?
    – dee zg
    Nov 8 at 8:51










  • Improved with code example
    – Bo Vandersteene
    Nov 8 at 9:03










  • hm...that doesn't look like the whole picture. So, usual setup with lazy loaded store is: you have AppModule in which you register main reducers&effects using .forRoot(...). Then, you have your lazy loaded angular module, e.g. MyLazyModule in which you register your lazy loaded ngrx reducers/effects the same way you do in your AppModule with the difference that now you use .forFeature(...) instead of .forRoot(...). In short, remove .forFeature(...) registrations from your AppModule and put them into your lazy loaded module.
    – dee zg
    Nov 8 at 9:10










  • But then my other reducers are still loaded after the action has been send. The only way to affect a solution for now is to load all reducers on startup time, which I want to avoid. There should be like a replay action or something else
    – Bo Vandersteene
    Nov 8 at 9:15










  • which other reducers? i am sorry, i might be missing something, but i have hard time understanding your overall app structure. in particular, which angular modules you have and which corresponding ngrx module you have and then what actions (from which ngrx module) are fired where and cause problems. It would be beneficial if you could clarify that a bit.
    – dee zg
    Nov 8 at 9:16













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm facing following problem. My action is fired before my reducer is initialised. For this reason my reducer don't pick up the action. How can I affect it that my reducer picks up actions when fired before my reducer has been initialised?



This image shows you the redux flow.



enter image description here



My action is dispatched from a ConfigurationEffect. And should be captured by different XFeature-reducers these reducers are feature based reducers



Code example:



app module



@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
CommonModule,
AppRoutingModule,
HttpClientModule,
StoreModule.forFeature('config', fromConfig.reducer),
EffectsModule.forFeature([ConfigEffects]),
EffectsModule.forRoot([AppEffects]),
StoreModule.forRoot(reducers, { metaReducers }),
StoreDevtoolsModule.instrument({
maxAge: 10,
name: 'Debug DevTools',
logOnly: true,
actionSanitizer,
stateSanitizer,
}),
],
bootstrap: [AppComponent],
})
export class AppModule {}


config effects



@Effect()
loadConfigs$ = this.actions$.pipe(
ofType(configActions.ConfigActionTypes.LoadConfigs),
switchMap((action: configActions.LoadConfigs) =>
this.configService.loadConfig().pipe(
map((result: ConfigResponse) => new configActions.SaveConfigs(result)),
catchError((err: HttpErrorResponse) => of(new configActions.FailConfigs(err.message))),
),
),
);


xFeature reducer



export function reducer(state = initialState, action: XFeatureActions | ConfigActions): XFeatureState {
switch (action.type) {
// save configs, this will be called when the device is initially loaded.
case ConfigActionTypes.SaveConfigs:
...
})









share|improve this question















I'm facing following problem. My action is fired before my reducer is initialised. For this reason my reducer don't pick up the action. How can I affect it that my reducer picks up actions when fired before my reducer has been initialised?



This image shows you the redux flow.



enter image description here



My action is dispatched from a ConfigurationEffect. And should be captured by different XFeature-reducers these reducers are feature based reducers



Code example:



app module



@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
CommonModule,
AppRoutingModule,
HttpClientModule,
StoreModule.forFeature('config', fromConfig.reducer),
EffectsModule.forFeature([ConfigEffects]),
EffectsModule.forRoot([AppEffects]),
StoreModule.forRoot(reducers, { metaReducers }),
StoreDevtoolsModule.instrument({
maxAge: 10,
name: 'Debug DevTools',
logOnly: true,
actionSanitizer,
stateSanitizer,
}),
],
bootstrap: [AppComponent],
})
export class AppModule {}


config effects



@Effect()
loadConfigs$ = this.actions$.pipe(
ofType(configActions.ConfigActionTypes.LoadConfigs),
switchMap((action: configActions.LoadConfigs) =>
this.configService.loadConfig().pipe(
map((result: ConfigResponse) => new configActions.SaveConfigs(result)),
catchError((err: HttpErrorResponse) => of(new configActions.FailConfigs(err.message))),
),
),
);


xFeature reducer



export function reducer(state = initialState, action: XFeatureActions | ConfigActions): XFeatureState {
switch (action.type) {
// save configs, this will be called when the device is initially loaded.
case ConfigActionTypes.SaveConfigs:
...
})






angular ngrx ngrx-store






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 8:58

























asked Nov 8 at 8:49









Bo Vandersteene

196212




196212












  • you should provide a bit more details. most important, where is your action being dispatched from?
    – dee zg
    Nov 8 at 8:51










  • Improved with code example
    – Bo Vandersteene
    Nov 8 at 9:03










  • hm...that doesn't look like the whole picture. So, usual setup with lazy loaded store is: you have AppModule in which you register main reducers&effects using .forRoot(...). Then, you have your lazy loaded angular module, e.g. MyLazyModule in which you register your lazy loaded ngrx reducers/effects the same way you do in your AppModule with the difference that now you use .forFeature(...) instead of .forRoot(...). In short, remove .forFeature(...) registrations from your AppModule and put them into your lazy loaded module.
    – dee zg
    Nov 8 at 9:10










  • But then my other reducers are still loaded after the action has been send. The only way to affect a solution for now is to load all reducers on startup time, which I want to avoid. There should be like a replay action or something else
    – Bo Vandersteene
    Nov 8 at 9:15










  • which other reducers? i am sorry, i might be missing something, but i have hard time understanding your overall app structure. in particular, which angular modules you have and which corresponding ngrx module you have and then what actions (from which ngrx module) are fired where and cause problems. It would be beneficial if you could clarify that a bit.
    – dee zg
    Nov 8 at 9:16


















  • you should provide a bit more details. most important, where is your action being dispatched from?
    – dee zg
    Nov 8 at 8:51










  • Improved with code example
    – Bo Vandersteene
    Nov 8 at 9:03










  • hm...that doesn't look like the whole picture. So, usual setup with lazy loaded store is: you have AppModule in which you register main reducers&effects using .forRoot(...). Then, you have your lazy loaded angular module, e.g. MyLazyModule in which you register your lazy loaded ngrx reducers/effects the same way you do in your AppModule with the difference that now you use .forFeature(...) instead of .forRoot(...). In short, remove .forFeature(...) registrations from your AppModule and put them into your lazy loaded module.
    – dee zg
    Nov 8 at 9:10










  • But then my other reducers are still loaded after the action has been send. The only way to affect a solution for now is to load all reducers on startup time, which I want to avoid. There should be like a replay action or something else
    – Bo Vandersteene
    Nov 8 at 9:15










  • which other reducers? i am sorry, i might be missing something, but i have hard time understanding your overall app structure. in particular, which angular modules you have and which corresponding ngrx module you have and then what actions (from which ngrx module) are fired where and cause problems. It would be beneficial if you could clarify that a bit.
    – dee zg
    Nov 8 at 9:16
















you should provide a bit more details. most important, where is your action being dispatched from?
– dee zg
Nov 8 at 8:51




you should provide a bit more details. most important, where is your action being dispatched from?
– dee zg
Nov 8 at 8:51












Improved with code example
– Bo Vandersteene
Nov 8 at 9:03




Improved with code example
– Bo Vandersteene
Nov 8 at 9:03












hm...that doesn't look like the whole picture. So, usual setup with lazy loaded store is: you have AppModule in which you register main reducers&effects using .forRoot(...). Then, you have your lazy loaded angular module, e.g. MyLazyModule in which you register your lazy loaded ngrx reducers/effects the same way you do in your AppModule with the difference that now you use .forFeature(...) instead of .forRoot(...). In short, remove .forFeature(...) registrations from your AppModule and put them into your lazy loaded module.
– dee zg
Nov 8 at 9:10




hm...that doesn't look like the whole picture. So, usual setup with lazy loaded store is: you have AppModule in which you register main reducers&effects using .forRoot(...). Then, you have your lazy loaded angular module, e.g. MyLazyModule in which you register your lazy loaded ngrx reducers/effects the same way you do in your AppModule with the difference that now you use .forFeature(...) instead of .forRoot(...). In short, remove .forFeature(...) registrations from your AppModule and put them into your lazy loaded module.
– dee zg
Nov 8 at 9:10












But then my other reducers are still loaded after the action has been send. The only way to affect a solution for now is to load all reducers on startup time, which I want to avoid. There should be like a replay action or something else
– Bo Vandersteene
Nov 8 at 9:15




But then my other reducers are still loaded after the action has been send. The only way to affect a solution for now is to load all reducers on startup time, which I want to avoid. There should be like a replay action or something else
– Bo Vandersteene
Nov 8 at 9:15












which other reducers? i am sorry, i might be missing something, but i have hard time understanding your overall app structure. in particular, which angular modules you have and which corresponding ngrx module you have and then what actions (from which ngrx module) are fired where and cause problems. It would be beneficial if you could clarify that a bit.
– dee zg
Nov 8 at 9:16




which other reducers? i am sorry, i might be missing something, but i have hard time understanding your overall app structure. in particular, which angular modules you have and which corresponding ngrx module you have and then what actions (from which ngrx module) are fired where and cause problems. It would be beneficial if you could clarify that a bit.
– dee zg
Nov 8 at 9:16












1 Answer
1






active

oldest

votes

















up vote
1
down vote













I'm uncertain if this is what you're looking for but when a reducer is registered, NgRx dispatches a update-reducers action. You can listen to these actions inside your effect in order to disaptch the SaveConfig action.



Pre NgRx 7 this looks as:



{type: '@ngrx/store/update-reducers', feature: 'feature1'}
{type: '@ngrx/store/update-reducers', feature: 'feature2'}


Starting from NgRx 7 this looks as:



{type: '@ngrx/store/update-reducers', features: ['feature1',
'feature2']}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204221%2flazy-load-feature-modules-ngrx-action-trigged-before-reducer-is-initialize%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    I'm uncertain if this is what you're looking for but when a reducer is registered, NgRx dispatches a update-reducers action. You can listen to these actions inside your effect in order to disaptch the SaveConfig action.



    Pre NgRx 7 this looks as:



    {type: '@ngrx/store/update-reducers', feature: 'feature1'}
    {type: '@ngrx/store/update-reducers', feature: 'feature2'}


    Starting from NgRx 7 this looks as:



    {type: '@ngrx/store/update-reducers', features: ['feature1',
    'feature2']}





    share|improve this answer

























      up vote
      1
      down vote













      I'm uncertain if this is what you're looking for but when a reducer is registered, NgRx dispatches a update-reducers action. You can listen to these actions inside your effect in order to disaptch the SaveConfig action.



      Pre NgRx 7 this looks as:



      {type: '@ngrx/store/update-reducers', feature: 'feature1'}
      {type: '@ngrx/store/update-reducers', feature: 'feature2'}


      Starting from NgRx 7 this looks as:



      {type: '@ngrx/store/update-reducers', features: ['feature1',
      'feature2']}





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        I'm uncertain if this is what you're looking for but when a reducer is registered, NgRx dispatches a update-reducers action. You can listen to these actions inside your effect in order to disaptch the SaveConfig action.



        Pre NgRx 7 this looks as:



        {type: '@ngrx/store/update-reducers', feature: 'feature1'}
        {type: '@ngrx/store/update-reducers', feature: 'feature2'}


        Starting from NgRx 7 this looks as:



        {type: '@ngrx/store/update-reducers', features: ['feature1',
        'feature2']}





        share|improve this answer












        I'm uncertain if this is what you're looking for but when a reducer is registered, NgRx dispatches a update-reducers action. You can listen to these actions inside your effect in order to disaptch the SaveConfig action.



        Pre NgRx 7 this looks as:



        {type: '@ngrx/store/update-reducers', feature: 'feature1'}
        {type: '@ngrx/store/update-reducers', feature: 'feature2'}


        Starting from NgRx 7 this looks as:



        {type: '@ngrx/store/update-reducers', features: ['feature1',
        'feature2']}






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 16:26









        timdeschryver

        1,889117




        1,889117






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204221%2flazy-load-feature-modules-ngrx-action-trigged-before-reducer-is-initialize%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff