Skip to content
This repository has been archived by the owner on Nov 23, 2020. It is now read-only.

Child state conflict if multiple lazy-loaded states are present #309

Open
cyr-x opened this issue Jan 7, 2019 · 0 comments
Open

Child state conflict if multiple lazy-loaded states are present #309

cyr-x opened this issue Jan 7, 2019 · 0 comments

Comments

@cyr-x
Copy link

cyr-x commented Jan 7, 2019

🐞 bug report

Affected Package

@loona/angular: 1.0.0

Description

If multiple states are "lazy-loaded" via LoonaModule.forChild, only the latest module import will initialize a child state. This is caused because CHILD_STATE is provided with multi: false option, therefore every call of LoonaModule.forChild will override previous values.

This happens if you have for example multiple feature modules which are imported by the app root module and each of the feature module holds a state by using LoonaModule.forChild.

This can be solved by providing CHILD_STATE with multi: true and changing the effect initialization code inside LoonaChildModule to handle multiple state groups.

@NgModule()
export class LoonaModule {
  // ...
  static forChild(states: any[] = []): ModuleWithProviders {
    return {
      ngModule: LoonaChildModule,
      providers: [
        ...states, 
        { provide: CHILD_STATE, useValue: states, multi: true },
      ],
    };
  }
}


@NgModule()
export class LoonaChildModule {
  constructor(
    @Inject(CHILD_STATE) stateGroups: StateClass<Metadata>[][], // <-- changed
    // ...
  ) {
   // ...

   // Changed to handle CHILD_STATE provider array
    stateGroups.forEach(group =>
    {
      group.forEach(state => {
         // ...
      });
    })
    // ...
  }
}

But this will propaply cause some side-effects because states are instanciated multiple times.

🌍 Your Environment

Angular Version:


Angular CLI: 7.1.4
Node: 10.9.0
OS: linux x64
Angular: 7.1.4
... animations, cli, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.11.4
@angular-devkit/build-angular     0.11.4
@angular-devkit/build-optimizer   0.11.4
@angular-devkit/build-webpack     0.11.4
@angular-devkit/core              7.1.4
@angular-devkit/schematics        7.1.4
@ngtools/webpack                  7.1.4
@schematics/angular               7.1.4
@schematics/update                0.11.4
rxjs                              6.3.3
typescript                        3.2.2
webpack                           4.23.1
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant