1- import { NgModuleRef , NgZone , PlatformRef } from '@angular/core' ;
1+ import { ApplicationRef , EnvironmentProviders , NgModuleRef , NgZone , PlatformRef , Provider , Type , ɵinternalCreateApplication as internalCreateApplication } from '@angular/core' ;
22import { filter , map , take } from 'rxjs/operators' ;
33import { Application , ApplicationEventData , Color , LaunchEventData , LayoutBase , profile , removeTaggedAdditionalCSS , StackLayout , TextView , View , Utils , Trace } from '@nativescript/core' ;
44import { AppHostView } from './app-host-view' ;
55import { NativeScriptLoadingService } from './loading.service' ;
66import { APP_ROOT_VIEW , DISABLE_ROOT_VIEW_HANDLING , NATIVESCRIPT_ROOT_MODULE_ID } from './tokens' ;
77import { Observable , Subject } from 'rxjs' ;
88import { NativeScriptDebug } from './trace' ;
9+ import { NATIVESCRIPT_MODULE_PROVIDERS , NATIVESCRIPT_MODULE_STATIC_PROVIDERS } from './nativescript' ;
910
1011export interface AppLaunchView extends LayoutBase {
1112 // called when the animation is to begin
@@ -27,7 +28,7 @@ export type NgModuleReason = 'hotreload' | 'applaunch' | 'appexit';
2728export type NgModuleEvent =
2829 | {
2930 moduleType : 'main' | 'loading' | string ;
30- reference : NgModuleRef < unknown > ;
31+ reference : NgModuleRef < unknown > | ApplicationRef ;
3132 reason : NgModuleReason | string ;
3233 }
3334 | {
@@ -57,9 +58,29 @@ export const onAfterLivesync: Observable<{
5758 map ( ( v ) => ( { moduleRef : v . reference as NgModuleRef < any > } ) )
5859) ;
5960export 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+ */
6280 launchView ?: ( reason : NgModuleReason ) => AppLaunchView ;
81+ /**
82+ * Wether we are running in an embedded context (e.g. embedding NativeScript in an existing app)
83+ */
6384 embedded ?: boolean ;
6485}
6586
@@ -71,17 +92,17 @@ if (import.meta['webpackHot']) {
7192 } ;
7293}
7394
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 ) {
7596 postAngularBootstrap$ . next ( {
7697 moduleType : name ,
7798 reference : ref ,
7899 reason,
79100 } ) ;
80101}
81102
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 ;
83104function 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 {
85106 if ( ref ) {
86107 if ( ref instanceof PlatformRef ) {
87108 preAngularDisposal$ . next ( {
@@ -90,7 +111,7 @@ function destroyRef<T>(ref: PlatformRef | NgModuleRef<T>, name?: string, reason?
90111 reason : name ,
91112 } ) ;
92113 }
93- if ( ref instanceof NgModuleRef ) {
114+ if ( ref instanceof NgModuleRef || ref instanceof ApplicationRef ) {
94115 preAngularDisposal$ . next ( {
95116 moduleType : name ,
96117 reference : ref ,
@@ -177,12 +198,29 @@ function runSynchronously(fn: () => void, done?: () => void): void {
177198 }
178199}
179200
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+
180218export 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 ;
183221 let platformRef : PlatformRef = null ;
184222 let bootstrapId = - 1 ;
185- const updatePlatformRef = ( moduleRef : NgModuleRef < T | K > , reason : NgModuleReason ) => {
223+ const updatePlatformRef = ( moduleRef : NgModuleRef < T | K > | ApplicationRef , reason : NgModuleReason ) => {
186224 const newPlatformRef = moduleRef . injector . get ( PlatformRef ) ;
187225 if ( newPlatformRef === platformRef ) {
188226 return ;
@@ -193,12 +231,12 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
193231 } ;
194232 let launchEventDone = true ;
195233 let targetRootView : View = null ;
196- const setRootView = ( ref : NgModuleRef < T | K > | View ) => {
234+ const setRootView = ( ref : NgModuleRef < T | K > | ApplicationRef | View ) => {
197235 if ( bootstrapId === - 1 ) {
198236 // treat edge cases
199237 return ;
200238 }
201- if ( ref instanceof NgModuleRef ) {
239+ if ( ref instanceof NgModuleRef || ref instanceof ApplicationRef ) {
202240 if ( ref . injector . get ( DISABLE_ROOT_VIEW_HANDLING , false ) ) {
203241 return ;
204242 }
@@ -262,10 +300,11 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
262300 return ;
263301 }
264302 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 ) ) ;
266305 updatePlatformRef ( ref , reason ) ;
267306 const styleTag = ref . injector . get ( NATIVESCRIPT_ROOT_MODULE_ID ) ;
268- ref . onDestroy ( ( ) => {
307+ ( ref instanceof ApplicationRef ? ref . components [ 0 ] : ref ) . onDestroy ( ( ) => {
269308 removeTaggedAdditionalCSS ( styleTag ) ;
270309 } ) ;
271310 bootstrapped = true ;
@@ -296,10 +335,10 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
296335 return ;
297336 }
298337 loadingModuleRef = loadingRef ;
299- loadingModuleRef . onDestroy ( ( ) => ( loadingModuleRef = loadingModuleRef === loadingRef ? null : loadingModuleRef ) ) ;
338+ ( loadingModuleRef instanceof ApplicationRef ? loadingModuleRef . components [ 0 ] : loadingModuleRef ) . onDestroy ( ( ) => ( loadingModuleRef = loadingModuleRef === loadingRef ? null : loadingModuleRef ) ) ;
300339 updatePlatformRef ( loadingRef , reason ) ;
301340 const styleTag = loadingModuleRef . injector . get ( NATIVESCRIPT_ROOT_MODULE_ID ) ;
302- loadingRef . onDestroy ( ( ) => {
341+ ( loadingModuleRef instanceof ApplicationRef ? loadingModuleRef . components [ 0 ] : loadingModuleRef ) . onDestroy ( ( ) => {
303342 removeTaggedAdditionalCSS ( styleTag ) ;
304343 } ) ;
305344 setRootView ( loadingRef ) ;
0 commit comments