From 8f0959850dd4e94897f66a3816a02ee6c929df6e Mon Sep 17 00:00:00 2001 From: vakrilov Date: Fri, 14 Sep 2018 16:14:22 +0300 Subject: [PATCH 1/3] feat: HMR bootstrap and livesinc options --- nativescript-angular/platform-common.ts | 28 +++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/nativescript-angular/platform-common.ts b/nativescript-angular/platform-common.ts index 2590fe381..eb1cdb7b4 100644 --- a/nativescript-angular/platform-common.ts +++ b/nativescript-angular/platform-common.ts @@ -50,11 +50,18 @@ if ((global).___TS_UNUSED) { return InjectionToken; })(); } + +export interface HmrOptions { + moduleTypeFactory?: () => Type | NgModuleFactory + livesyncCallback: (bootstrapPlatfrom: () => void) => void; +} + export interface AppOptions { bootInExistingPage?: boolean; cssFile?: string; startPageActionBarHidden?: boolean; createFrameOnBootstrap?: boolean; + hmr?: HmrOptions; } export type PlatformFactory = (extraProviders?: StaticProvider[]) => PlatformRef; @@ -91,7 +98,9 @@ export class NativeScriptPlatformRef extends PlatformRef { @profile bootstrapModuleFactory(moduleFactory: NgModuleFactory): Promise> { - this._bootstrapper = () => this.platform.bootstrapModuleFactory(moduleFactory); + this._bootstrapper = () => this.platform.bootstrapModuleFactory( + this.appOptions.hmr ? >this.appOptions.hmr.moduleTypeFactory() : moduleFactory + ); this.bootstrapApp(); @@ -103,7 +112,9 @@ export class NativeScriptPlatformRef extends PlatformRef { moduleType: Type, compilerOptions: CompilerOptions | CompilerOptions[] = [] ): Promise> { - this._bootstrapper = () => this.platform.bootstrapModule(moduleType, compilerOptions); + this._bootstrapper = () => this.platform.bootstrapModule( + this.appOptions.hmr ? >this.appOptions.hmr.moduleTypeFactory() : moduleType, + compilerOptions); this.bootstrapApp(); @@ -113,7 +124,11 @@ export class NativeScriptPlatformRef extends PlatformRef { @profile private bootstrapApp() { (global).__onLiveSyncCore = () => { - this._livesync(); + if (this.appOptions.hmr) { + this.appOptions.hmr.livesyncCallback(() => this._livesync()); + } else { + this._livesync(); + } }; if (this.appOptions && typeof this.appOptions.cssFile === "string") { @@ -224,7 +239,12 @@ export class NativeScriptPlatformRef extends PlatformRef { if (isLogEnabled()) { bootstrapLog("Angular livesync started."); } - onBeforeLivesync.next(lastBootstrappedModule ? lastBootstrappedModule.get() : null); + + const lastModuleRef = lastBootstrappedModule ? lastBootstrappedModule.get() : null + onBeforeLivesync.next(lastModuleRef); + if (lastModuleRef) { + lastModuleRef.destroy(); + } const autoCreateFrame = !!this.appOptions.createFrameOnBootstrap; let tempAppHostView: AppHostView; From b35fa396926f7da207be7a65997b3ef0eaa291e6 Mon Sep 17 00:00:00 2001 From: Alexander Vakrilov Date: Mon, 1 Oct 2018 23:45:42 +0300 Subject: [PATCH 2/3] style: added missing semi column --- nativescript-angular/platform-common.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nativescript-angular/platform-common.ts b/nativescript-angular/platform-common.ts index eb1cdb7b4..269cb9ab6 100644 --- a/nativescript-angular/platform-common.ts +++ b/nativescript-angular/platform-common.ts @@ -52,7 +52,7 @@ if ((global).___TS_UNUSED) { } export interface HmrOptions { - moduleTypeFactory?: () => Type | NgModuleFactory + moduleTypeFactory?: () => Type | NgModuleFactory; livesyncCallback: (bootstrapPlatfrom: () => void) => void; } @@ -240,7 +240,7 @@ export class NativeScriptPlatformRef extends PlatformRef { bootstrapLog("Angular livesync started."); } - const lastModuleRef = lastBootstrappedModule ? lastBootstrappedModule.get() : null + const lastModuleRef = lastBootstrappedModule ? lastBootstrappedModule.get() : null; onBeforeLivesync.next(lastModuleRef); if (lastModuleRef) { lastModuleRef.destroy(); From fee8fc687239b516e9cddf2d17dbe4f65db9130f Mon Sep 17 00:00:00 2001 From: vakrilov Date: Tue, 9 Oct 2018 02:05:43 +0300 Subject: [PATCH 3/3] style: long-lines, jsdoc, typos --- nativescript-angular/platform-common.ts | 45 +++++++++++++++++++------ 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/nativescript-angular/platform-common.ts b/nativescript-angular/platform-common.ts index 269cb9ab6..505ec91b2 100644 --- a/nativescript-angular/platform-common.ts +++ b/nativescript-angular/platform-common.ts @@ -51,17 +51,33 @@ if ((global).___TS_UNUSED) { })(); } +// tslint:disable:max-line-length +/** + * Options to be passed when HMR is enabled + */ export interface HmrOptions { + /** + * A factory function that returns either Module type or NgModuleFactory type. + * This needs to be a factory function as the types will change when modules are replaced. + */ moduleTypeFactory?: () => Type | NgModuleFactory; - livesyncCallback: (bootstrapPlatfrom: () => void) => void; + + /** + * A livesync callback that will be called instead of the original livesync. + * It gives the HMR a hook to apply the module replacement. + * @param bootstrapPlatform - A bootstrap callback to be called after HMR is done. It will bootstrap a new angular app within the exisiting platform, using the moduleTypeFactory to get the Module or NgModuleFactory to be used. + */ + livesyncCallback: (bootstrapPlatform: () => void) => void; } +// tslint:enable:max-line-length + export interface AppOptions { bootInExistingPage?: boolean; cssFile?: string; startPageActionBarHidden?: boolean; createFrameOnBootstrap?: boolean; - hmr?: HmrOptions; + hmrOptions?: HmrOptions; } export type PlatformFactory = (extraProviders?: StaticProvider[]) => PlatformRef; @@ -98,9 +114,14 @@ export class NativeScriptPlatformRef extends PlatformRef { @profile bootstrapModuleFactory(moduleFactory: NgModuleFactory): Promise> { - this._bootstrapper = () => this.platform.bootstrapModuleFactory( - this.appOptions.hmr ? >this.appOptions.hmr.moduleTypeFactory() : moduleFactory - ); + this._bootstrapper = () => { + let bootstrapFactory = moduleFactory; + if (this.appOptions.hmrOptions) { + bootstrapFactory = >this.appOptions.hmrOptions.moduleTypeFactory(); + } + + return this.platform.bootstrapModuleFactory(bootstrapFactory); + }; this.bootstrapApp(); @@ -112,10 +133,14 @@ export class NativeScriptPlatformRef extends PlatformRef { moduleType: Type, compilerOptions: CompilerOptions | CompilerOptions[] = [] ): Promise> { - this._bootstrapper = () => this.platform.bootstrapModule( - this.appOptions.hmr ? >this.appOptions.hmr.moduleTypeFactory() : moduleType, - compilerOptions); + this._bootstrapper = () => { + let bootstrapType = moduleType; + if (this.appOptions.hmrOptions) { + bootstrapType = >this.appOptions.hmrOptions.moduleTypeFactory(); + } + return this.platform.bootstrapModule(bootstrapType, compilerOptions); + }; this.bootstrapApp(); return null; // Make the compiler happy @@ -124,8 +149,8 @@ export class NativeScriptPlatformRef extends PlatformRef { @profile private bootstrapApp() { (global).__onLiveSyncCore = () => { - if (this.appOptions.hmr) { - this.appOptions.hmr.livesyncCallback(() => this._livesync()); + if (this.appOptions.hmrOptions) { + this.appOptions.hmrOptions.livesyncCallback(() => this._livesync()); } else { this._livesync(); }