@@ -91,8 +91,8 @@ function _getUniversalData(doc: Document): UniversalData {
9191 } ;
9292}
9393
94- export function ngAspnetCoreEngine ( options : Readonly < IEngineOptions > )
95- : Promise < IEngineRenderResult > {
94+ export async function ngAspnetCoreEngine ( options : Readonly < IEngineOptions > )
95+ : Promise < IEngineRenderResult > {
9696 if ( ! options . appSelector ) {
9797 const selector = `" appSelector: '<${ appSelector } ></${ appSelector } >' "` ;
9898 throw new Error ( `appSelector is required! Pass in ${ selector } ,
@@ -111,52 +111,37 @@ export function ngAspnetCoreEngine(options: Readonly<IEngineOptions>)
111111 }
112112 ] ) ;
113113
114- return new Promise ( ( resolve , reject ) => {
115-
116- try {
117- const moduleOrFactory = options . ngModule ;
118- if ( ! moduleOrFactory ) {
119- throw new Error ( 'You must pass in a NgModule or NgModuleFactory to be bootstrapped' ) ;
120- }
121-
122- let extraProviders = options . providers || [ ] ;
123-
124- extraProviders = extraProviders . concat ( getReqResProviders ( options . request . origin ,
125- options . request . data . request ) ) ;
126-
127- getFactory ( moduleOrFactory , compiler )
128- . then ( factory => {
129- return renderModuleFactory ( factory , {
130- document : options . document || options . appSelector ,
131- url : options . url || options . request . absoluteUrl ,
132- extraProviders : extraProviders
133- } ) ;
134- } )
135- . then ( result => {
136- const doc = result . moduleRef . injector . get ( DOCUMENT ) ;
137- const universalData = _getUniversalData ( doc ) ;
138-
139- resolve ( {
140- html : universalData . appNode ,
141- moduleRef : result . moduleRef ,
142- globals : {
143- styles : universalData . styles ,
144- title : universalData . title ,
145- scripts : universalData . scripts ,
146- meta : universalData . meta ,
147- links : universalData . links
148- }
149- } ) ;
150- } , ( err ) => {
151- reject ( err ) ;
152- } ) ;
153-
154- } catch ( ex ) {
155- reject ( ex ) ;
156- }
114+ const moduleOrFactory = options . ngModule ;
115+ if ( ! moduleOrFactory ) {
116+ throw new Error ( 'You must pass in a NgModule or NgModuleFactory to be bootstrapped' ) ;
117+ }
118+
119+ const extraProviders = [
120+ ...( options . providers || [ ] ) ,
121+ getReqResProviders ( options . request . origin , options . request . data . request ) ,
122+ ] ;
157123
124+ const factory = await getFactory ( moduleOrFactory , compiler ) ;
125+ const result = await renderModuleFactory ( factory , {
126+ document : options . document || options . appSelector ,
127+ url : options . url || options . request . absoluteUrl ,
128+ extraProviders,
158129 } ) ;
159130
131+ const doc = result . moduleRef . injector . get ( DOCUMENT ) ;
132+ const universalData = _getUniversalData ( doc ) ;
133+
134+ return {
135+ html : universalData . appNode ,
136+ moduleRef : result . moduleRef ,
137+ globals : {
138+ styles : universalData . styles ,
139+ title : universalData . title ,
140+ scripts : universalData . scripts ,
141+ meta : universalData . meta ,
142+ links : universalData . links
143+ }
144+ } ;
160145}
161146
162147/**
@@ -178,32 +163,22 @@ function getReqResProviders(origin: string, request: string): StaticProvider[] {
178163
179164/* @internal */
180165const factoryCacheMap = new Map < Type < { } > , NgModuleFactory < { } > > ( ) ;
181- function getFactory (
166+ async function getFactory (
182167 moduleOrFactory : Type < { } > | NgModuleFactory < { } > , compiler : Compiler
183168) : Promise < NgModuleFactory < { } > > {
184-
185- return new Promise < NgModuleFactory < { } > > ( ( resolve , reject ) => {
186- // If module has been compiled AoT
187- if ( moduleOrFactory instanceof NgModuleFactory ) {
188- resolve ( moduleOrFactory ) ;
189- return ;
190- } else {
191- let moduleFactory = factoryCacheMap . get ( moduleOrFactory ) ;
192-
193- // If module factory is cached
194- if ( moduleFactory ) {
195- resolve ( moduleFactory ) ;
196- return ;
197- }
198-
199- // Compile the module and cache it
200- compiler . compileModuleAsync ( moduleOrFactory )
201- . then ( ( factory ) => {
202- factoryCacheMap . set ( moduleOrFactory , factory ) ;
203- resolve ( factory ) ;
204- } , ( err => {
205- reject ( err ) ;
206- } ) ) ;
169+ // If module has been compiled AoT
170+ if ( moduleOrFactory instanceof NgModuleFactory ) {
171+ return moduleOrFactory ;
172+ } else {
173+ let moduleFactory = factoryCacheMap . get ( moduleOrFactory ) ;
174+ // If module factory is cached
175+ if ( moduleFactory ) {
176+ return moduleFactory ;
207177 }
208- } ) ;
178+
179+ // Compile the module and cache it
180+ const factory = await compiler . compileModuleAsync ( moduleOrFactory ) ;
181+ factoryCacheMap . set ( moduleOrFactory , factory ) ;
182+ return factory ;
183+ }
209184}
0 commit comments