Skip to content

feat: HMR bootstrap and livesync options #1531

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 5 commits into from
Oct 9, 2018
Merged
Changes from 1 commit
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
28 changes: 24 additions & 4 deletions nativescript-angular/platform-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ if ((<any>global).___TS_UNUSED) {
return InjectionToken;
})();
}

export interface HmrOptions {
moduleTypeFactory?: () => Type<any> | NgModuleFactory<any>
livesyncCallback: (bootstrapPlatfrom: () => void) => void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bootstrapPlatfrom -> bootstrapPlatform

}

export interface AppOptions {
bootInExistingPage?: boolean;
cssFile?: string;
startPageActionBarHidden?: boolean;
createFrameOnBootstrap?: boolean;
hmr?: HmrOptions;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

hmr -> hmrOptions

I thought hmr was a boolean at first.

}

export type PlatformFactory = (extraProviders?: StaticProvider[]) => PlatformRef;
Expand Down Expand Up @@ -91,7 +98,9 @@ export class NativeScriptPlatformRef extends PlatformRef {

@profile
bootstrapModuleFactory<M>(moduleFactory: NgModuleFactory<M>): Promise<NgModuleRef<M>> {
this._bootstrapper = () => this.platform.bootstrapModuleFactory(moduleFactory);
this._bootstrapper = () => this.platform.bootstrapModuleFactory(
this.appOptions.hmr ? <NgModuleFactory<M>>this.appOptions.hmr.moduleTypeFactory() : moduleFactory
);

this.bootstrapApp();

Expand All @@ -103,7 +112,9 @@ export class NativeScriptPlatformRef extends PlatformRef {
moduleType: Type<M>,
compilerOptions: CompilerOptions | CompilerOptions[] = []
): Promise<NgModuleRef<M>> {
this._bootstrapper = () => this.platform.bootstrapModule(moduleType, compilerOptions);
this._bootstrapper = () => this.platform.bootstrapModule(
this.appOptions.hmr ? <Type<M>>this.appOptions.hmr.moduleTypeFactory() : moduleType,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That:

this.appOptions.hmr ? <Type ...

can be moved to a variable for readability.

compilerOptions);

this.bootstrapApp();

Expand All @@ -113,7 +124,11 @@ export class NativeScriptPlatformRef extends PlatformRef {
@profile
private bootstrapApp() {
(<any>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") {
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing semicolon

onBeforeLivesync.next(lastModuleRef);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to emit if there's no lastBootstrappedModule?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will be no lastBootstrappedModule if there was an error in the initial bootstrap and lastBootstrappedModule.get() will return null if the angular app was manually killed and collected (don't think it is a common practice in NS apps, but still ...). In either case I think the onBeforeLivesync should emit to be in balance with the onAfterLivesync.

if (lastModuleRef) {
lastModuleRef.destroy();
}

const autoCreateFrame = !!this.appOptions.createFrameOnBootstrap;
let tempAppHostView: AppHostView;
Expand Down