@@ -8,7 +8,7 @@ import './polyfills/console';
8
8
import { rendererLog , rendererError } from "./trace" ;
9
9
import { SanitizationService } from '@angular/core/src/security' ;
10
10
import { isPresent , Type , print } from '@angular/core/src/facade/lang' ;
11
- import { ReflectiveInjector , coreLoadAndBootstrap , createPlatform ,
11
+ import { ReflectiveInjector , coreLoadAndBootstrap , createPlatform , EventEmitter ,
12
12
getPlatform , assertPlatform , ComponentRef , PlatformRef , PLATFORM_DIRECTIVES , PLATFORM_PIPES } from '@angular/core' ;
13
13
import { bind , provide , Provider } from '@angular/core/src/di' ;
14
14
@@ -30,6 +30,8 @@ import {NS_DIRECTIVES} from './directives';
30
30
import { Page } from 'ui/page' ;
31
31
import { TextView } from 'ui/text-view' ;
32
32
import application = require( 'application' ) ;
33
+ import { topmost , NavigationEntry } from "ui/frame" ;
34
+ import { Observable } from "rxjs" ;
33
35
34
36
export type ProviderArray = Array < Type | Provider | any [ ] > ;
35
37
@@ -50,9 +52,20 @@ class ConsoleLogger {
50
52
logGroupEnd ( ) { }
51
53
}
52
54
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
+
53
66
// See: https://github.com/angular/angular/commit/1745366530266d298306b995ecd23dabd8569e28
54
67
export const NS_COMPILER_PROVIDERS : ProviderArray = [
55
- COMPILER_PROVIDERS ,
68
+ COMPILER_PROVIDERS ,
56
69
provide ( CompilerConfig , {
57
70
useFactory : ( platformDirectives : any [ ] , platformPipes : any [ ] ) => {
58
71
return new CompilerConfig ( { platformDirectives, platformPipes } ) ;
@@ -103,7 +116,7 @@ export function bootstrap(appComponentType: any,
103
116
// Http Setup
104
117
// Since HTTP_PROVIDERS can be added with customProviders above, this must come after
105
118
appProviders . push ( [
106
- provide ( XSRFStrategy , { useValue : new NSXSRFStrategy ( ) } ) ,
119
+ provide ( XSRFStrategy , { useValue : new NSXSRFStrategy ( ) } ) ,
107
120
NSFileSystem ,
108
121
provide ( Http , {
109
122
useFactory : ( backend , options , nsFileSystem ) => {
@@ -112,7 +125,7 @@ export function bootstrap(appComponentType: any,
112
125
} )
113
126
] ) ;
114
127
115
- var platform = getPlatform ( ) ;
128
+ var platform = getPlatform ( ) ;
116
129
if ( ! isPresent ( platform ) ) {
117
130
platform = createPlatform ( ReflectiveInjector . resolveAndCreate ( platformProviders ) ) ;
118
131
}
@@ -122,46 +135,89 @@ export function bootstrap(appComponentType: any,
122
135
return coreLoadAndBootstrap ( appComponentType , appInjector ) ;
123
136
}
124
137
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
+
125
184
export function nativeScriptBootstrap ( appComponentType : any , customProviders ?: ProviderArray , appOptions ?: AppOptions ) : Promise < ComponentRef < any > > {
185
+ bootstrapCache = { appComponentType, customProviders, appOptions } ;
186
+
126
187
if ( appOptions && appOptions . cssFile ) {
127
188
application . cssFile = appOptions . cssFile ;
128
189
}
129
190
130
191
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 ) ;
166
194
} )
167
195
}
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