Skip to content

Commit 4571197

Browse files
hanslalexeagle
authored andcommitted
feat(@angular-devkit/core): add a fallback registry
Its get method goes through an array of registries and return the first one found.
1 parent c461ed5 commit 4571197

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

packages/angular_devkit/core/src/experimental/jobs/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export * from './api';
99
export * from './create-job-handler';
1010
export * from './exception';
1111
export * from './dispatcher';
12+
export * from './fallback-registry';
1213
export * from './simple-registry';
1314
export * from './simple-scheduler';
1415
export * from './strategy';

0 commit comments

Comments
 (0)