@@ -8,7 +8,7 @@ import './polyfills/console';
88import { rendererLog , rendererError } from "./trace" ;
99import { SanitizationService } from '@angular/core/src/security' ;
1010import { isPresent , Type , print } from '@angular/core/src/facade/lang' ;
11- import { ReflectiveInjector , coreLoadAndBootstrap , createPlatform ,
11+ import { ReflectiveInjector , coreLoadAndBootstrap , createPlatform , EventEmitter ,
1212 getPlatform , assertPlatform , ComponentRef , PlatformRef , PLATFORM_DIRECTIVES , PLATFORM_PIPES } from '@angular/core' ;
1313import { bind , provide , Provider } from '@angular/core/src/di' ;
1414
@@ -30,6 +30,8 @@ import {NS_DIRECTIVES} from './directives';
3030import { Page } from 'ui/page' ;
3131import { TextView } from 'ui/text-view' ;
3232import application = require( 'application' ) ;
33+ import { topmost , NavigationEntry } from "ui/frame" ;
34+ import { Observable } from "rxjs" ;
3335
3436export type ProviderArray = Array < Type | Provider | any [ ] > ;
3537
@@ -50,9 +52,20 @@ class ConsoleLogger {
5052 logGroupEnd ( ) { }
5153}
5254
55+ interface BootstrapParams {
56+ appComponentType : Type ,
57+ customProviders ?: ProviderArray ,
58+ appOptions ?: AppOptions
59+ }
60+ var bootstrapCache : BootstrapParams
61+
62+ var lastBootstrappedApp : WeakRef < ComponentRef < any > > ;
63+ export const onBeforeLivesync = new EventEmitter < ComponentRef < any > > ( ) ;
64+ export const onAfterLivesync = new EventEmitter < ComponentRef < any > > ( ) ;
65+
5366// See: https://github.com/angular/angular/commit/1745366530266d298306b995ecd23dabd8569e28
5467export const NS_COMPILER_PROVIDERS : ProviderArray = [
55- COMPILER_PROVIDERS ,
68+ COMPILER_PROVIDERS ,
5669 provide ( CompilerConfig , {
5770 useFactory : ( platformDirectives : any [ ] , platformPipes : any [ ] ) => {
5871 return new CompilerConfig ( { platformDirectives, platformPipes } ) ;
@@ -103,7 +116,7 @@ export function bootstrap(appComponentType: any,
103116 // Http Setup
104117 // Since HTTP_PROVIDERS can be added with customProviders above, this must come after
105118 appProviders . push ( [
106- provide ( XSRFStrategy , { useValue : new NSXSRFStrategy ( ) } ) ,
119+ provide ( XSRFStrategy , { useValue : new NSXSRFStrategy ( ) } ) ,
107120 NSFileSystem ,
108121 provide ( Http , {
109122 useFactory : ( backend , options , nsFileSystem ) => {
@@ -112,7 +125,7 @@ export function bootstrap(appComponentType: any,
112125 } )
113126 ] ) ;
114127
115- var platform = getPlatform ( ) ;
128+ var platform = getPlatform ( ) ;
116129 if ( ! isPresent ( platform ) ) {
117130 platform = createPlatform ( ReflectiveInjector . resolveAndCreate ( platformProviders ) ) ;
118131 }
@@ -122,46 +135,89 @@ export function bootstrap(appComponentType: any,
122135 return coreLoadAndBootstrap ( appComponentType , appInjector ) ;
123136}
124137
138+ function createNavigationEntry ( params : BootstrapParams , resolve : ( comp : ComponentRef < any > ) => void , reject : ( e : Error ) => void , isReboot : boolean ) {
139+ const navEntry : NavigationEntry = {
140+ create : ( ) : Page => {
141+ let page = new Page ( ) ;
142+ if ( params . appOptions ) {
143+ page . actionBarHidden = params . appOptions . startPageActionBarHidden ;
144+ }
145+
146+ let onLoadedHandler = function ( args ) {
147+ page . off ( 'loaded' , onLoadedHandler ) ;
148+ //profiling.stop('application-start');
149+ rendererLog ( 'Page loaded' ) ;
150+
151+ //profiling.start('ng-bootstrap');
152+ rendererLog ( 'BOOTSTRAPPING...' ) ;
153+ bootstrap ( params . appComponentType , params . customProviders ) . then ( ( compRef ) => {
154+ //profiling.stop('ng-bootstrap');
155+ rendererLog ( 'ANGULAR BOOTSTRAP DONE.' ) ;
156+ lastBootstrappedApp = new WeakRef ( compRef ) ;
157+ resolve ( compRef ) ;
158+ } , ( err ) => {
159+ rendererError ( 'ERROR BOOTSTRAPPING ANGULAR' ) ;
160+ let errorMessage = err . message + "\n\n" + err . stack ;
161+ rendererError ( errorMessage ) ;
162+
163+ let view = new TextView ( ) ;
164+ view . text = errorMessage ;
165+ page . content = view ;
166+ reject ( err ) ;
167+ } ) ;
168+ }
169+
170+ page . on ( 'loaded' , onLoadedHandler ) ;
171+
172+ return page ;
173+ }
174+ } ;
175+
176+ if ( isReboot ) {
177+ navEntry . animated = false ;
178+ navEntry . clearHistory = true ;
179+ }
180+
181+ return navEntry ;
182+ }
183+
125184export function nativeScriptBootstrap ( appComponentType : any , customProviders ?: ProviderArray , appOptions ?: AppOptions ) : Promise < ComponentRef < any > > {
185+ bootstrapCache = { appComponentType, customProviders, appOptions } ;
186+
126187 if ( appOptions && appOptions . cssFile ) {
127188 application . cssFile = appOptions . cssFile ;
128189 }
129190
130191 return new Promise ( ( resolve , reject ) => {
131- application . start ( {
132- create : ( ) : Page => {
133- let page = new Page ( ) ;
134- if ( appOptions ) {
135- page . actionBarHidden = appOptions . startPageActionBarHidden ;
136- }
137-
138- let onLoadedHandler = function ( args ) {
139- page . off ( 'loaded' , onLoadedHandler ) ;
140- //profiling.stop('application-start');
141- rendererLog ( 'Page loaded' ) ;
142-
143- //profiling.start('ng-bootstrap');
144- rendererLog ( 'BOOTSTRAPPING...' ) ;
145- bootstrap ( appComponentType , customProviders ) . then ( ( appRef ) => {
146- //profiling.stop('ng-bootstrap');
147- rendererLog ( 'ANGULAR BOOTSTRAP DONE.' ) ;
148- resolve ( appRef ) ;
149- } , ( err ) => {
150- rendererError ( 'ERROR BOOTSTRAPPING ANGULAR' ) ;
151- let errorMessage = err . message + "\n\n" + err . stack ;
152- rendererError ( errorMessage ) ;
153-
154- let view = new TextView ( ) ;
155- view . text = errorMessage ;
156- page . content = view ;
157- reject ( err ) ;
158- } ) ;
159- }
160-
161- page . on ( 'loaded' , onLoadedHandler ) ;
162-
163- return page ;
164- }
165- } ) ;
192+ const navEntry = createNavigationEntry ( bootstrapCache , resolve , reject , false ) ;
193+ application . start ( navEntry ) ;
166194 } )
167195}
196+
197+ // Patch livesync
198+ const _baseLiveSync = global . __onLiveSync ;
199+ global . __onLiveSync = function ( ) {
200+ rendererLog ( "LiveSync Started" )
201+ if ( bootstrapCache ) {
202+ onBeforeLivesync . next ( lastBootstrappedApp ? lastBootstrappedApp . get ( ) : null ) ;
203+
204+ const frame = topmost ( ) ;
205+ const newEntry = createNavigationEntry (
206+ bootstrapCache ,
207+ compRef => onAfterLivesync . next ( compRef ) ,
208+ error => onAfterLivesync . error ( error ) ,
209+ true ) ;
210+
211+ if ( frame ) {
212+ if ( frame . currentPage && frame . currentPage . modal ) {
213+ frame . currentPage . modal . closeModal ( ) ;
214+ }
215+ frame . navigate ( newEntry ) ;
216+ }
217+ }
218+ else {
219+ _baseLiveSync ( ) ;
220+ }
221+ }
222+
223+
0 commit comments