refactor(compiler): allow sync AOT compilation (#16832).
AOT compilation can be executed synchronously now, if the `ReosurceLoader` returns a string directly (and no `Promise`).
This commit is contained in:

committed by
Chuck Jazdzewski

parent
255d7226d1
commit
5af143e8e4
@ -6,6 +6,8 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ɵisPromise as isPromise} from '@angular/core';
|
||||
|
||||
import * as o from './output/output_ast';
|
||||
|
||||
export const MODULE_SUFFIX = '';
|
||||
@ -80,19 +82,28 @@ export class ValueTransformer implements ValueVisitor {
|
||||
visitOther(value: any, context: any): any { return value; }
|
||||
}
|
||||
|
||||
export class SyncAsyncResult<T> {
|
||||
constructor(public syncResult: T|null, public asyncResult: Promise<T>|null = null) {
|
||||
if (!asyncResult) {
|
||||
this.asyncResult = Promise.resolve(syncResult);
|
||||
export type SyncAsync<T> = T | Promise<T>;
|
||||
|
||||
export const SyncAsync = {
|
||||
assertSync: <T>(value: SyncAsync<T>): T => {
|
||||
if (isPromise(value)) {
|
||||
throw new Error(`Illegal state: value cannot be a promise`);
|
||||
}
|
||||
return value;
|
||||
},
|
||||
then: <T, R>(value: SyncAsync<T>, cb: (value: T) => R | Promise<R>| SyncAsync<R>):
|
||||
SyncAsync<R> => { return isPromise(value) ? value.then(cb) : cb(value);},
|
||||
all: <T>(syncAsyncValues: SyncAsync<T>[]): SyncAsync<T[]> => {
|
||||
return syncAsyncValues.some(isPromise) ? Promise.all(syncAsyncValues) : syncAsyncValues as T[];
|
||||
}
|
||||
}
|
||||
|
||||
export function syntaxError(msg: string): Error {
|
||||
const error = Error(msg);
|
||||
(error as any)[ERROR_SYNTAX_ERROR] = true;
|
||||
return error;
|
||||
}
|
||||
export function syntaxError(msg: string):
|
||||
Error {
|
||||
const error = Error(msg);
|
||||
(error as any)[ERROR_SYNTAX_ERROR] = true;
|
||||
return error;
|
||||
}
|
||||
|
||||
const ERROR_SYNTAX_ERROR = 'ngSyntaxError';
|
||||
|
||||
|
Reference in New Issue
Block a user