@@ -6,7 +6,7 @@ import './polyfills/array';
66import { rendererLog , rendererError } from "./trace" ;
77import { SanitizationService } from '@angular/core/src/security' ;
88import { isPresent , Type , print } from '@angular/core/src/facade/lang' ;
9- import { ReflectiveInjector , coreLoadAndBootstrap , createPlatform ,
9+ import { ReflectiveInjector , coreLoadAndBootstrap , createPlatform , EventEmitter ,
1010 getPlatform , assertPlatform , ComponentRef , PlatformRef , PLATFORM_DIRECTIVES , PLATFORM_PIPES } from '@angular/core' ;
1111import { bind , provide , Provider } from '@angular/core/src/di' ;
1212
@@ -28,6 +28,8 @@ import {NS_DIRECTIVES} from './directives';
2828import { Page } from 'ui/page' ;
2929import { TextView } from 'ui/text-view' ;
3030import application = require( 'application' ) ;
31+ import { topmost , NavigationEntry } from "ui/frame" ;
32+ import { Observable } from "rxjs" ;
3133
3234export type ProviderArray = Array < Type | Provider | any [ ] > ;
3335
@@ -48,9 +50,20 @@ class ConsoleLogger {
4850 logGroupEnd ( ) { }
4951}
5052
53+ interface BootstrapParams {
54+ appComponentType : Type ,
55+ customProviders ?: ProviderArray ,
56+ appOptions ?: AppOptions
57+ }
58+ var bootstrapCache : BootstrapParams
59+
60+ var lastBootstrappedApp : WeakRef < ComponentRef < any > > ;
61+ export const onBeforeLivesync = new EventEmitter < ComponentRef < any > > ( ) ;
62+ export const onAfterLivesync = new EventEmitter < ComponentRef < any > > ( ) ;
63+
5164// See: https://github.com/angular/angular/commit/1745366530266d298306b995ecd23dabd8569e28
5265export const NS_COMPILER_PROVIDERS : ProviderArray = [
53- COMPILER_PROVIDERS ,
66+ COMPILER_PROVIDERS ,
5467 provide ( CompilerConfig , {
5568 useFactory : ( platformDirectives : any [ ] , platformPipes : any [ ] ) => {
5669 return new CompilerConfig ( { platformDirectives, platformPipes } ) ;
@@ -101,7 +114,7 @@ export function bootstrap(appComponentType: any,
101114 // Http Setup
102115 // Since HTTP_PROVIDERS can be added with customProviders above, this must come after
103116 appProviders . push ( [
104- provide ( XSRFStrategy , { useValue : new NSXSRFStrategy ( ) } ) ,
117+ provide ( XSRFStrategy , { useValue : new NSXSRFStrategy ( ) } ) ,
105118 NSFileSystem ,
106119 provide ( Http , {
107120 useFactory : ( backend , options , nsFileSystem ) => {
@@ -110,7 +123,7 @@ export function bootstrap(appComponentType: any,
110123 } )
111124 ] ) ;
112125
113- var platform = getPlatform ( ) ;
126+ var platform = getPlatform ( ) ;
114127 if ( ! isPresent ( platform ) ) {
115128 platform = createPlatform ( ReflectiveInjector . resolveAndCreate ( platformProviders ) ) ;
116129 }
@@ -120,46 +133,89 @@ export function bootstrap(appComponentType: any,
120133 return coreLoadAndBootstrap ( appComponentType , appInjector ) ;
121134}
122135
136+ function createNavigationEntry ( params : BootstrapParams , resolve : ( comp : ComponentRef < any > ) => void , reject : ( e : Error ) => void , isReboot : boolean ) {
137+ const navEntry : NavigationEntry = {
138+ create : ( ) : Page => {
139+ let page = new Page ( ) ;
140+ if ( params . appOptions ) {
141+ page . actionBarHidden = params . appOptions . startPageActionBarHidden ;
142+ }
143+
144+ let onLoadedHandler = function ( args ) {
145+ page . off ( 'loaded' , onLoadedHandler ) ;
146+ //profiling.stop('application-start');
147+ rendererLog ( 'Page loaded' ) ;
148+
149+ //profiling.start('ng-bootstrap');
150+ rendererLog ( 'BOOTSTRAPPING...' ) ;
151+ bootstrap ( params . appComponentType , params . customProviders ) . then ( ( compRef ) => {
152+ //profiling.stop('ng-bootstrap');
153+ rendererLog ( 'ANGULAR BOOTSTRAP DONE.' ) ;
154+ lastBootstrappedApp = new WeakRef ( compRef ) ;
155+ resolve ( compRef ) ;
156+ } , ( err ) => {
157+ rendererError ( 'ERROR BOOTSTRAPPING ANGULAR' ) ;
158+ let errorMessage = err . message + "\n\n" + err . stack ;
159+ rendererError ( errorMessage ) ;
160+
161+ let view = new TextView ( ) ;
162+ view . text = errorMessage ;
163+ page . content = view ;
164+ reject ( err ) ;
165+ } ) ;
166+ }
167+
168+ page . on ( 'loaded' , onLoadedHandler ) ;
169+
170+ return page ;
171+ }
172+ } ;
173+
174+ if ( isReboot ) {
175+ navEntry . animated = false ;
176+ navEntry . clearHistory = true ;
177+ }
178+
179+ return navEntry ;
180+ }
181+
123182export function nativeScriptBootstrap ( appComponentType : any , customProviders ?: ProviderArray , appOptions ?: AppOptions ) : Promise < ComponentRef < any > > {
183+ bootstrapCache = { appComponentType, customProviders, appOptions } ;
184+
124185 if ( appOptions && appOptions . cssFile ) {
125186 application . cssFile = appOptions . cssFile ;
126187 }
127188
128189 return new Promise ( ( resolve , reject ) => {
129- application . start ( {
130- create : ( ) : Page => {
131- let page = new Page ( ) ;
132- if ( appOptions ) {
133- page . actionBarHidden = appOptions . startPageActionBarHidden ;
134- }
135-
136- let onLoadedHandler = function ( args ) {
137- page . off ( 'loaded' , onLoadedHandler ) ;
138- //profiling.stop('application-start');
139- rendererLog ( 'Page loaded' ) ;
140-
141- //profiling.start('ng-bootstrap');
142- rendererLog ( 'BOOTSTRAPPING...' ) ;
143- bootstrap ( appComponentType , customProviders ) . then ( ( appRef ) => {
144- //profiling.stop('ng-bootstrap');
145- rendererLog ( 'ANGULAR BOOTSTRAP DONE.' ) ;
146- resolve ( appRef ) ;
147- } , ( err ) => {
148- rendererError ( 'ERROR BOOTSTRAPPING ANGULAR' ) ;
149- let errorMessage = err . message + "\n\n" + err . stack ;
150- rendererError ( errorMessage ) ;
151-
152- let view = new TextView ( ) ;
153- view . text = errorMessage ;
154- page . content = view ;
155- reject ( err ) ;
156- } ) ;
157- }
158-
159- page . on ( 'loaded' , onLoadedHandler ) ;
160-
161- return page ;
162- }
163- } ) ;
190+ const navEntry = createNavigationEntry ( bootstrapCache , resolve , reject , false ) ;
191+ application . start ( navEntry ) ;
164192 } )
165193}
194+
195+ // Patch livesync
196+ const _baseLiveSync = global . __onLiveSync ;
197+ global . __onLiveSync = function ( ) {
198+ rendererLog ( "LiveSync Started" )
199+ if ( bootstrapCache ) {
200+ onBeforeLivesync . next ( lastBootstrappedApp ? lastBootstrappedApp . get ( ) : null ) ;
201+
202+ const frame = topmost ( ) ;
203+ const newEntry = createNavigationEntry (
204+ bootstrapCache ,
205+ compRef => onAfterLivesync . next ( compRef ) ,
206+ error => onAfterLivesync . error ( error ) ,
207+ true ) ;
208+
209+ if ( frame ) {
210+ if ( frame . currentPage && frame . currentPage . modal ) {
211+ frame . currentPage . modal . closeModal ( ) ;
212+ }
213+ frame . navigate ( newEntry ) ;
214+ }
215+ }
216+ else {
217+ _baseLiveSync ( ) ;
218+ }
219+ }
220+
221+
0 commit comments