Skip to content

Commit d7b403c

Browse files
committed
refactor(aspnetcore-engine): use async/await function
1 parent 9da7415 commit d7b403c

File tree

3 files changed

+79
-111
lines changed

3 files changed

+79
-111
lines changed

modules/aspnetcore-engine/src/main.ts

+45-70
Original file line numberDiff line numberDiff line change
@@ -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 */
180165
const 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
}

modules/aspnetcore-engine/src/platform-server-utils.ts

+28-33
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
INITIAL_CONFIG,
2929
PlatformState
3030
} from '@angular/platform-server';
31-
import {filter, take} from 'rxjs/operators';
31+
import {first} from 'rxjs/operators';
3232

3333
interface PlatformOptions {
3434
document?: string;
@@ -51,42 +51,37 @@ function _getPlatform(
5151
]);
5252
}
5353

54-
function _render<T>(platform: PlatformRef,
54+
async function _render<T>(platform: PlatformRef,
5555
moduleRefPromise: Promise<NgModuleRef<T>>): Promise<ModuleRenderResult<T>> {
56-
return moduleRefPromise.then(moduleRef => {
57-
const transitionId = moduleRef.injector.get(ɵTRANSITION_ID, null);
58-
if (!transitionId) {
59-
throw new Error(
60-
`renderModule[Factory]() requires the use of BrowserModule.withServerTransition() to ensure
56+
const moduleRef = await moduleRefPromise;
57+
const transitionId = moduleRef.injector.get(ɵTRANSITION_ID, null);
58+
if (!transitionId) {
59+
throw new Error(`renderModule[Factory]() requires the use of BrowserModule.withServerTransition() to ensure
6160
the server-rendered app can be properly bootstrapped into a client app.`);
62-
}
63-
const applicationRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
64-
return applicationRef.isStable
65-
.pipe(
66-
filter((isStable: boolean) => isStable),
67-
take(1)
68-
).toPromise()
69-
.then(() => {
70-
const platformState = platform.injector.get(PlatformState);
61+
}
7162

72-
// Run any BEFORE_APP_SERIALIZED callbacks just before rendering to string.
73-
const callbacks = moduleRef.injector.get(BEFORE_APP_SERIALIZED, null);
74-
if (callbacks) {
75-
for (const callback of callbacks) {
76-
try {
77-
callback();
78-
} catch (e) {
79-
// Ignore exceptions.
80-
console.warn('Ignoring BEFORE_APP_SERIALIZED Exception: ', e);
81-
}
82-
}
83-
}
63+
const applicationRef = moduleRef.injector.get(ApplicationRef);
64+
await applicationRef.isStable
65+
.pipe(
66+
first(isStable => isStable),
67+
).toPromise();
8468

85-
const output = platformState.renderToString();
86-
platform.destroy();
87-
return { html: output, moduleRef };
88-
});
89-
});
69+
const platformState = platform.injector.get(PlatformState);
70+
// Run any BEFORE_APP_SERIALIZED callbacks just before rendering to string.
71+
const callbacks = moduleRef.injector.get(BEFORE_APP_SERIALIZED, null);
72+
if (callbacks) {
73+
for (const callback of callbacks) {
74+
try {
75+
callback();
76+
} catch (e) {
77+
// Ignore exceptions.
78+
console.warn('Ignoring BEFORE_APP_SERIALIZED Exception: ', e);
79+
}
80+
}
81+
}
82+
const output = platformState.renderToString();
83+
platform.destroy();
84+
return { html: output, moduleRef };
9085
}
9186

9287
/**

modules/common/engine/src/engine.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,23 @@ export class CommonEngine {
5959
}
6060

6161
/** Return the factory for a given engine instance */
62-
getFactory(moduleOrFactory: Type<{}> | NgModuleFactory<{}>): Promise<NgModuleFactory<{}>> {
62+
async getFactory(moduleOrFactory: Type<{}> | NgModuleFactory<{}>): Promise<NgModuleFactory<{}>> {
6363
// If module has been compiled AoT
6464
if (moduleOrFactory instanceof NgModuleFactory) {
65-
return Promise.resolve(moduleOrFactory);
65+
return moduleOrFactory;
6666
} else {
6767
// we're in JIT mode
6868
let moduleFactory = this.factoryCacheMap.get(moduleOrFactory);
6969

7070
// If module factory is cached
7171
if (moduleFactory) {
72-
return Promise.resolve(moduleFactory);
72+
return moduleFactory;
7373
}
7474

7575
// Compile the module and cache it
76-
return this.getCompiler().compileModuleAsync(moduleOrFactory)
77-
.then((factory) => {
78-
this.factoryCacheMap.set(moduleOrFactory, factory);
79-
return factory;
80-
});
76+
const factory = await this.getCompiler().compileModuleAsync(moduleOrFactory);
77+
this.factoryCacheMap.set(moduleOrFactory, factory);
78+
return factory;
8179
}
8280
}
8381

0 commit comments

Comments
 (0)