Skip to content

feat: prevent core modules from getting loaded multiple times #1196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions e2e/router/app/lazy/lazy.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { Route } from "@angular/router";

import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { NativeScriptCommonModule } from "nativescript-angular/common";
import { NativeScriptRouterModule } from "nativescript-angular/router";

import { LazyComponent } from "./lazy.component";
Expand All @@ -26,7 +26,7 @@ const routes: Route[] = [
@NgModule({
schemas: [NO_ERRORS_SCHEMA],
imports: [
NativeScriptModule,
NativeScriptCommonModule,
NativeScriptRouterModule,
NativeScriptRouterModule.forChild(routes)
],
Expand Down
7 changes: 6 additions & 1 deletion nativescript-angular/animations/animations.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NgModule, Injectable, NgZone, Provider, RendererFactory2 } from "@angular/core";
import { NgModule, Injectable, NgZone, Provider, RendererFactory2, Optional, SkipSelf } from "@angular/core";

import { AnimationBuilder } from "@angular/animations";

Expand All @@ -17,6 +17,7 @@ import {
import { NativeScriptModule } from "../nativescript.module";
import { NativeScriptRendererFactory } from "../renderer";
import { NativeScriptAnimationDriver } from "./animation-driver";
import { throwIfAlreadyLoaded } from "../common/utils";

@Injectable()
export class InjectableAnimationEngine extends AnimationEngine {
Expand Down Expand Up @@ -55,4 +56,8 @@ export const NATIVESCRIPT_ANIMATIONS_PROVIDERS: Provider[] = [
providers: NATIVESCRIPT_ANIMATIONS_PROVIDERS,
})
export class NativeScriptAnimationsModule {
constructor(@Optional() @SkipSelf() parentModule: NativeScriptAnimationsModule) {
// Prevents NativeScriptAnimationsModule from getting imported multiple times
throwIfAlreadyLoaded(parentModule, "NativeScriptAnimationsModule");
}
}
8 changes: 8 additions & 0 deletions nativescript-angular/common/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function throwIfAlreadyLoaded(
parentModule: any,
moduleName: string,
) {
if ( parentModule ) {
throw new Error(`${moduleName} has already been loaded. Import ${moduleName} in the AppModule only.`);
}
}
7 changes: 7 additions & 0 deletions nativescript-angular/nativescript.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ import {
NgModule,
RendererFactory2,
SystemJsNgModuleLoader,
Optional,
SkipSelf,
} from "@angular/core";

import { NativeScriptCommonModule } from "./common";
import { NativeScriptRendererFactory } from "./renderer";
import { DetachedLoader } from "./common/detached-loader";
import { throwIfAlreadyLoaded } from "./common/utils";

export function errorHandlerFactory() {
return new ErrorHandler();
Expand Down Expand Up @@ -49,4 +52,8 @@ export function errorHandlerFactory() {
schemas: [NO_ERRORS_SCHEMA]
})
export class NativeScriptModule {
constructor(@Optional() @SkipSelf() parentModule: NativeScriptModule) {
// Prevents NativeScriptModule from getting imported multiple times
throwIfAlreadyLoaded(parentModule, "NativeScriptModule");
}
}
4 changes: 2 additions & 2 deletions tests/app/lazy-loaded.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, NgModule } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { NativeScriptCommonModule } from "nativescript-angular/common";
import { NativeScriptRouterModule } from "nativescript-angular/router";
import { ModalDialogParams } from "nativescript-angular/directives/dialogs";
import { Page } from "ui/page";
Expand Down Expand Up @@ -31,7 +31,7 @@ export class ModalLazyComponent {
],
entryComponents: [ModalLazyComponent], // when lazily loaded and opened via modal on demand
imports: [
NativeScriptModule,
NativeScriptCommonModule,
NativeScriptRouterModule,
NativeScriptRouterModule.forChild(routes)
],
Expand Down