refactor(@angular-devkit/core): cleanup ajv initialization

This commit is contained in:
Charles Lyding 2018-08-08 11:39:38 -04:00 committed by vikerman
parent 7eb080362f
commit 2dcf0b9901

View File

@ -7,7 +7,7 @@
*/ */
import * as ajv from 'ajv'; import * as ajv from 'ajv';
import * as http from 'http'; import * as http from 'http';
import { Observable, from, of as observableOf } from 'rxjs'; import { Observable, from, of as observableOf, throwError } from 'rxjs';
import { concatMap, map, switchMap, tap } from 'rxjs/operators'; import { concatMap, map, switchMap, tap } from 'rxjs/operators';
import { BaseException } from '../../exception/exception'; import { BaseException } from '../../exception/exception';
import { PartiallyOrderedSet, isObservable } from '../../utils'; import { PartiallyOrderedSet, isObservable } from '../../utils';
@ -176,7 +176,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
validate = (validate.refVal as any)[(validate.refs as any)['#' + refHash]]; validate = (validate.refVal as any)[(validate.refs as any)['#' + refHash]];
} }
return { context: validate, schema: validate && validate.schema as JsonObject }; return { context: validate, schema: validate.schema as JsonObject };
} }
compile(schema: JsonObject): Observable<SchemaValidator> { compile(schema: JsonObject): Observable<SchemaValidator> {
@ -186,23 +186,18 @@ export class CoreSchemaRegistry implements SchemaRegistry {
// in synchronous (if available). // in synchronous (if available).
let validator: Observable<ajv.ValidateFunction>; let validator: Observable<ajv.ValidateFunction>;
try { try {
const maybeFnValidate = this._ajv.compile(schema); validator = observableOf(this._ajv.compile(schema));
validator = observableOf(maybeFnValidate);
} catch (e) { } catch (e) {
// Propagate the error. // Propagate the error.
if (!(e instanceof (ajv.MissingRefError as {} as Function))) { if (!(e instanceof (ajv.MissingRefError as {} as Function))) {
throw e; return throwError(e);
} }
validator = new Observable(obs => { try {
this._ajv.compileAsync(schema) validator = from(this._ajv.compileAsync(schema));
.then(validate => { } catch (e) {
obs.next(validate); return throwError(e);
obs.complete(); }
}, err => {
obs.error(err);
});
});
} }
return validator return validator