Skip to content

Optimize platfroms for AOT #529

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 1 commit into from
Nov 14, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 0 additions & 6 deletions nativescript-angular/dom-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ export class NativeScriptElementSchemaRegistry extends ElementSchemaRegistry {
}
}

export class NativeScriptSanitizer extends Sanitizer {
sanitize(context: SecurityContext, value: string): string {
return value;
}
}

export class NativeScriptDomAdapter extends Parse5DomAdapter {
static makeCurrent() {
rendererLog("Setting DOM");
Expand Down
1 change: 1 addition & 0 deletions nativescript-angular/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./platform";
export * from "./platform-static";
export * from "./router";
export * from "./forms";
export * from "./http";
Expand Down
5 changes: 1 addition & 4 deletions nativescript-angular/nativescript.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ import {
ErrorHandler,
Renderer,
RootRenderer,
Sanitizer,
NgModule, NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA
NgModule, NO_ERRORS_SCHEMA,
} from '@angular/core';
import {
defaultPageProvider, defaultFrameProvider, defaultDeviceProvider
} from "./platform-providers";
import { NativeScriptSanitizer } from './dom-adapter';
import { NS_DIRECTIVES } from './directives';

import * as nativescriptIntl from "nativescript-intl";
Expand All @@ -45,7 +43,6 @@ export function errorHandlerFactory() {
{ provide: RootRenderer, useClass: NativeScriptRootRenderer },
NativeScriptRenderer,
{ provide: Renderer, useClass: NativeScriptRenderer },
{ provide: Sanitizer, useClass: NativeScriptSanitizer },
ModalDialogService
],
entryComponents: [
Expand Down
194 changes: 194 additions & 0 deletions nativescript-angular/platform-common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// Initial imports and polyfills
import 'globals';
import './zone.js/dist/zone-nativescript';
import 'reflect-metadata';
import './polyfills/array';
import './polyfills/console';

import {
Type,
Injector,
CompilerOptions,
PlatformRef,
NgModuleFactory,
NgModuleRef,
EventEmitter,
Provider,
Sanitizer,
OpaqueToken,
} from '@angular/core';

import { rendererLog, rendererError } from "./trace";
import { PAGE_FACTORY, PageFactory, defaultPageFactoryProvider } from './platform-providers';

import * as application from "application";
import { topmost, NavigationEntry } from "ui/frame";
import { Page } from 'ui/page';
import { TextView } from 'ui/text-view';

import * as nativescriptIntl from "nativescript-intl";
global.Intl = nativescriptIntl;

export const onBeforeLivesync = new EventEmitter<NgModuleRef<any>>();
export const onAfterLivesync = new EventEmitter<NgModuleRef<any>>();
let lastBootstrappedModule: WeakRef<NgModuleRef<any>>;
type BootstrapperAction = () => Promise<NgModuleRef<any>>;

interface BootstrapParams {
appModuleType: Type<any>;
appOptions?: AppOptions;
}

export interface AppOptions {
bootInExistingPage: boolean;
cssFile?: string;
startPageActionBarHidden?: boolean;
}

export type PlatformFactory = (extraProviders?: Provider[]) => PlatformRef;

export class NativeScriptSanitizer extends Sanitizer {
sanitize(context: any, value: string): string {
return value;
}
}

export const COMMON_PROVIDERS = [
defaultPageFactoryProvider,
{ provide: Sanitizer, useClass: NativeScriptSanitizer },
];

export class NativeScriptPlatformRef extends PlatformRef {
private _bootstrapper: BootstrapperAction;

constructor(private platform: PlatformRef, private appOptions?: AppOptions) {
super();
}

bootstrapModuleFactory<M>(moduleFactory: NgModuleFactory<M>): Promise<NgModuleRef<M>> {
this._bootstrapper = () => this.platform.bootstrapModuleFactory(moduleFactory);

this.bootstrapApp();

return null; //Make the compiler happy
}

bootstrapModule<M>(moduleType: Type<M>, compilerOptions: CompilerOptions | CompilerOptions[] = []): Promise<NgModuleRef<M>> {
this._bootstrapper = () => this.platform.bootstrapModule(moduleType, compilerOptions);

this.bootstrapApp();

return null; //Make the compiler happy
}

private bootstrapApp() {
global.__onLiveSyncCore = () => this.livesyncModule();

const mainPageEntry = this.createNavigationEntry(this._bootstrapper);

application.start(mainPageEntry);
}

livesyncModule(): void {
rendererLog("ANGULAR LiveSync Started");

onBeforeLivesync.next(lastBootstrappedModule ? lastBootstrappedModule.get() : null);

const mainPageEntry = this.createNavigationEntry(
this._bootstrapper,
compRef => onAfterLivesync.next(compRef),
error => onAfterLivesync.error(error),
true
);
mainPageEntry.animated = false;
mainPageEntry.clearHistory = true;

const frame = topmost();
if (frame) {
if (frame.currentPage && frame.currentPage.modal) {
frame.currentPage.modal.closeModal();
}
frame.navigate(mainPageEntry);
}
}

onDestroy(callback: () => void): void {
this.platform.onDestroy(callback);
}

get injector(): Injector {
return this.platform.injector;
};

destroy(): void {
this.platform.destroy();
}

get destroyed(): boolean {
return this.platform.destroyed;
}

private createNavigationEntry(
bootstrapAction: BootstrapperAction,
resolve?: (comp: NgModuleRef<any>) => void,
reject?: (e: Error) => void,
isLivesync: boolean = false,
isReboot: boolean = false): NavigationEntry {

const pageFactory: PageFactory = this.platform.injector.get(PAGE_FACTORY);

const navEntry: NavigationEntry = {
create: (): Page => {
let page = pageFactory({ isBootstrap: true, isLivesync });
if (this.appOptions) {
page.actionBarHidden = this.appOptions.startPageActionBarHidden;
}

let onLoadedHandler = function (args) {
page.off('loaded', onLoadedHandler);
//profiling.stop('application-start');
rendererLog('Page loaded');

//profiling.start('ng-bootstrap');
rendererLog('BOOTSTRAPPING...');
bootstrapAction().then((moduleRef) => {
//profiling.stop('ng-bootstrap');
rendererLog('ANGULAR BOOTSTRAP DONE.');
lastBootstrappedModule = new WeakRef(moduleRef);

if (resolve) {
resolve(moduleRef);
}
return moduleRef;
}, (err) => {
rendererError('ERROR BOOTSTRAPPING ANGULAR');
let errorMessage = err.message + "\n\n" + err.stack;
rendererError(errorMessage);

let view = new TextView();
view.text = errorMessage;
page.content = view;

if (reject) {
reject(err);
}
});
};

page.on('loaded', onLoadedHandler);

return page;
}
};

if (isReboot) {
navEntry.animated = false;
navEntry.clearHistory = true;
}

return navEntry;
}

liveSyncApp() {
}
}
17 changes: 17 additions & 0 deletions nativescript-angular/platform-static.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Always import platform-common first - because polyfills
import { NativeScriptPlatformRef, AppOptions, COMMON_PROVIDERS, PlatformFactory } from "./platform-common";

import { platformCore, PlatformRef, createPlatformFactory } from '@angular/core';

// "Static" platform
const _platformNativeScript: PlatformFactory = createPlatformFactory(
platformCore, 'nativeScript', [...COMMON_PROVIDERS]);

export function platformNativeScript(options?: AppOptions, extraProviders?: any[]): PlatformRef {
//Return raw platform to advanced users only if explicitly requested
if (options && options.bootInExistingPage === true) {
return _platformNativeScript(extraProviders);
} else {
return new NativeScriptPlatformRef(_platformNativeScript(extraProviders), options);
}
}
Loading