1
- import { NgModuleRef , NgZone , PlatformRef } from '@angular/core' ;
1
+ import { ApplicationRef , EnvironmentProviders , NgModuleRef , NgZone , PlatformRef , Provider , Type , ɵinternalCreateApplication as internalCreateApplication } from '@angular/core' ;
2
2
import { filter , map , take } from 'rxjs/operators' ;
3
3
import { Application , ApplicationEventData , Color , LaunchEventData , LayoutBase , profile , removeTaggedAdditionalCSS , StackLayout , TextView , View , Utils , Trace } from '@nativescript/core' ;
4
4
import { AppHostView } from './app-host-view' ;
5
5
import { NativeScriptLoadingService } from './loading.service' ;
6
6
import { APP_ROOT_VIEW , DISABLE_ROOT_VIEW_HANDLING , NATIVESCRIPT_ROOT_MODULE_ID } from './tokens' ;
7
7
import { Observable , Subject } from 'rxjs' ;
8
8
import { NativeScriptDebug } from './trace' ;
9
+ import { NATIVESCRIPT_MODULE_PROVIDERS , NATIVESCRIPT_MODULE_STATIC_PROVIDERS } from './nativescript' ;
9
10
10
11
export interface AppLaunchView extends LayoutBase {
11
12
// called when the animation is to begin
@@ -27,7 +28,7 @@ export type NgModuleReason = 'hotreload' | 'applaunch' | 'appexit';
27
28
export type NgModuleEvent =
28
29
| {
29
30
moduleType : 'main' | 'loading' | string ;
30
- reference : NgModuleRef < unknown > ;
31
+ reference : NgModuleRef < unknown > | ApplicationRef ;
31
32
reason : NgModuleReason | string ;
32
33
}
33
34
| {
@@ -57,9 +58,29 @@ export const onAfterLivesync: Observable<{
57
58
map ( ( v ) => ( { moduleRef : v . reference as NgModuleRef < any > } ) )
58
59
) ;
59
60
export interface AppRunOptions < T , K > {
60
- appModuleBootstrap : ( reason : NgModuleReason ) => Promise < NgModuleRef < T > > ;
61
- loadingModule ?: ( reason : NgModuleReason ) => Promise < NgModuleRef < K > > ;
61
+ /**
62
+ * Runs when the app is launched or during HMR.
63
+ * May not run immediately if the app was started in background (e.g. push notification)
64
+ * @param reason reason for bootstrap. @see {NgModuleReason}
65
+ * @returns Promise to the bootstrapped NgModuleRef
66
+ */
67
+ appModuleBootstrap : ( reason : NgModuleReason ) => Promise < NgModuleRef < T > | ApplicationRef > ;
68
+ /**
69
+ * Loads a custom NgModule for the loading screen.
70
+ * This loads only if appModuleBootstrap doesn't resolve synchronously (e.g. async APP_INITIALIZER).
71
+ * @param reason reason for bootstrap. @see {NgModuleReason}
72
+ * @returns Promise to the bootstrapped NgModuleRef. Must resolve immediately (no async initialization)
73
+ */
74
+ loadingModule ?: ( reason : NgModuleReason ) => Promise < NgModuleRef < K > | ApplicationRef > ;
75
+ /**
76
+ * Simpler than loadingModule, this will show a view while the app is bootstrapping asynchronously.
77
+ * @param reason reason for bootstrap. @see {NgModuleReason}
78
+ * @returns View that will be shown while app boots
79
+ */
62
80
launchView ?: ( reason : NgModuleReason ) => AppLaunchView ;
81
+ /**
82
+ * Wether we are running in an embedded context (e.g. embedding NativeScript in an existing app)
83
+ */
63
84
embedded ?: boolean ;
64
85
}
65
86
@@ -71,17 +92,17 @@ if (import.meta['webpackHot']) {
71
92
} ;
72
93
}
73
94
74
- function emitModuleBootstrapEvent < T > ( ref : NgModuleRef < T > , name : 'main' | 'loading' , reason : NgModuleReason ) {
95
+ function emitModuleBootstrapEvent < T > ( ref : NgModuleRef < T > | ApplicationRef , name : 'main' | 'loading' , reason : NgModuleReason ) {
75
96
postAngularBootstrap$ . next ( {
76
97
moduleType : name ,
77
98
reference : ref ,
78
99
reason,
79
100
} ) ;
80
101
}
81
102
82
- function destroyRef < T > ( ref : NgModuleRef < T > , name : 'main' | 'loading' , reason : NgModuleReason ) : void ;
103
+ function destroyRef < T > ( ref : NgModuleRef < T > | ApplicationRef , name : 'main' | 'loading' , reason : NgModuleReason ) : void ;
83
104
function destroyRef ( ref : PlatformRef , reason : NgModuleReason ) : void ;
84
- function destroyRef < T > ( ref : PlatformRef | NgModuleRef < T > , name ?: string , reason ?: string ) : void {
105
+ function destroyRef < T > ( ref : PlatformRef | ApplicationRef | NgModuleRef < T > , name ?: string , reason ?: string ) : void {
85
106
if ( ref ) {
86
107
if ( ref instanceof PlatformRef ) {
87
108
preAngularDisposal$ . next ( {
@@ -90,7 +111,7 @@ function destroyRef<T>(ref: PlatformRef | NgModuleRef<T>, name?: string, reason?
90
111
reason : name ,
91
112
} ) ;
92
113
}
93
- if ( ref instanceof NgModuleRef ) {
114
+ if ( ref instanceof NgModuleRef || ref instanceof ApplicationRef ) {
94
115
preAngularDisposal$ . next ( {
95
116
moduleType : name ,
96
117
reference : ref ,
@@ -177,12 +198,29 @@ function runSynchronously(fn: () => void, done?: () => void): void {
177
198
}
178
199
}
179
200
201
+ function createProvidersConfig ( options ?: ApplicationConfig ) {
202
+ return {
203
+ appProviders : [ ...NATIVESCRIPT_MODULE_PROVIDERS , ...NATIVESCRIPT_MODULE_STATIC_PROVIDERS , ...( options ?. providers ?? [ ] ) ] ,
204
+ // platformProviders: INTERNAL_BROWSER_PLATFORM_PROVIDERS
205
+ } ;
206
+ }
207
+
208
+ export interface ApplicationConfig {
209
+ /**
210
+ * List of providers that should be available to the root component and all its children.
211
+ */
212
+ providers : Array < Provider | EnvironmentProviders > ;
213
+ }
214
+ export function bootstrapApplication ( rootComponent : Type < unknown > , options ?: ApplicationConfig ) : Promise < ApplicationRef > {
215
+ return internalCreateApplication ( { rootComponent, ...createProvidersConfig ( options ) } ) ;
216
+ }
217
+
180
218
export function runNativeScriptAngularApp < T , K > ( options : AppRunOptions < T , K > ) {
181
- let mainModuleRef : NgModuleRef < T > = null ;
182
- let loadingModuleRef : NgModuleRef < K > ;
219
+ let mainModuleRef : NgModuleRef < T > | ApplicationRef = null ;
220
+ let loadingModuleRef : NgModuleRef < K > | ApplicationRef ;
183
221
let platformRef : PlatformRef = null ;
184
222
let bootstrapId = - 1 ;
185
- const updatePlatformRef = ( moduleRef : NgModuleRef < T | K > , reason : NgModuleReason ) => {
223
+ const updatePlatformRef = ( moduleRef : NgModuleRef < T | K > | ApplicationRef , reason : NgModuleReason ) => {
186
224
const newPlatformRef = moduleRef . injector . get ( PlatformRef ) ;
187
225
if ( newPlatformRef === platformRef ) {
188
226
return ;
@@ -193,12 +231,12 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
193
231
} ;
194
232
let launchEventDone = true ;
195
233
let targetRootView : View = null ;
196
- const setRootView = ( ref : NgModuleRef < T | K > | View ) => {
234
+ const setRootView = ( ref : NgModuleRef < T | K > | ApplicationRef | View ) => {
197
235
if ( bootstrapId === - 1 ) {
198
236
// treat edge cases
199
237
return ;
200
238
}
201
- if ( ref instanceof NgModuleRef ) {
239
+ if ( ref instanceof NgModuleRef || ref instanceof ApplicationRef ) {
202
240
if ( ref . injector . get ( DISABLE_ROOT_VIEW_HANDLING , false ) ) {
203
241
return ;
204
242
}
@@ -262,10 +300,11 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
262
300
return ;
263
301
}
264
302
mainModuleRef = ref ;
265
- ref . onDestroy ( ( ) => ( mainModuleRef = mainModuleRef === ref ? null : mainModuleRef ) ) ;
303
+
304
+ ( ref instanceof ApplicationRef ? ref . components [ 0 ] : ref ) . onDestroy ( ( ) => ( mainModuleRef = mainModuleRef === ref ? null : mainModuleRef ) ) ;
266
305
updatePlatformRef ( ref , reason ) ;
267
306
const styleTag = ref . injector . get ( NATIVESCRIPT_ROOT_MODULE_ID ) ;
268
- ref . onDestroy ( ( ) => {
307
+ ( ref instanceof ApplicationRef ? ref . components [ 0 ] : ref ) . onDestroy ( ( ) => {
269
308
removeTaggedAdditionalCSS ( styleTag ) ;
270
309
} ) ;
271
310
bootstrapped = true ;
@@ -296,10 +335,10 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
296
335
return ;
297
336
}
298
337
loadingModuleRef = loadingRef ;
299
- loadingModuleRef . onDestroy ( ( ) => ( loadingModuleRef = loadingModuleRef === loadingRef ? null : loadingModuleRef ) ) ;
338
+ ( loadingModuleRef instanceof ApplicationRef ? loadingModuleRef . components [ 0 ] : loadingModuleRef ) . onDestroy ( ( ) => ( loadingModuleRef = loadingModuleRef === loadingRef ? null : loadingModuleRef ) ) ;
300
339
updatePlatformRef ( loadingRef , reason ) ;
301
340
const styleTag = loadingModuleRef . injector . get ( NATIVESCRIPT_ROOT_MODULE_ID ) ;
302
- loadingRef . onDestroy ( ( ) => {
341
+ ( loadingModuleRef instanceof ApplicationRef ? loadingModuleRef . components [ 0 ] : loadingModuleRef ) . onDestroy ( ( ) => {
303
342
removeTaggedAdditionalCSS ( styleTag ) ;
304
343
} ) ;
305
344
setRootView ( loadingRef ) ;
0 commit comments