|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { Observable, from, of } from 'rxjs'; |
| 9 | +import { concatMap, first } from 'rxjs/operators'; |
| 10 | +import { JsonValue, schema } from '../../json'; |
| 11 | +import { JobDescription, JobHandler, JobName, Registry, isJobHandler } from './api'; |
| 12 | +import { JobNameAlreadyRegisteredException } from './exception'; |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * A simple job registry that keep a map of JobName => JobHandler internally. |
| 17 | + */ |
| 18 | +export class FallbackRegistry< |
| 19 | + MinimumArgumentValueT extends JsonValue = JsonValue, |
| 20 | + MinimumInputValueT extends JsonValue = JsonValue, |
| 21 | + MinimumOutputValueT extends JsonValue = JsonValue, |
| 22 | +> implements Registry<MinimumArgumentValueT, MinimumInputValueT, MinimumOutputValueT> { |
| 23 | + constructor(protected _fallbacks: Registry< |
| 24 | + MinimumArgumentValueT, |
| 25 | + MinimumInputValueT, |
| 26 | + MinimumOutputValueT |
| 27 | + >[] = []) {} |
| 28 | + |
| 29 | + addFallback(registry: Registry) { |
| 30 | + this._fallbacks.push(registry); |
| 31 | + } |
| 32 | + |
| 33 | + get< |
| 34 | + A extends MinimumArgumentValueT = MinimumArgumentValueT, |
| 35 | + I extends MinimumInputValueT = MinimumInputValueT, |
| 36 | + O extends MinimumOutputValueT = MinimumOutputValueT, |
| 37 | + >(name: JobName): Observable<JobHandler<A, I, O> | null> { |
| 38 | + return from(this._fallbacks).pipe( |
| 39 | + concatMap(fb => fb.get<A, I, O>(name)), |
| 40 | + first(x => x !== null, null), |
| 41 | + ); |
| 42 | + } |
| 43 | +} |
0 commit comments