1 | import {Injector} from '@angular/core';
|
2 |
|
3 | import {ComposedTransition, Variant, VariantsMap} from '../application/contracts';
|
4 |
|
5 | import {RuntimeException} from '../exception';
|
6 |
|
7 | import {typeToInjectorFunction} from '../transformation';
|
8 |
|
9 | export const composeTransitions = <V>(variants: VariantsMap, values: V): ComposedTransition => {
|
10 | return (injector: Injector): Promise<void> => {
|
11 | if (variants == null || Object.keys(variants).length === 0) {
|
12 | return Promise.resolve();
|
13 | }
|
14 |
|
15 | const promises = new Array<Promise<void>>();
|
16 |
|
17 | for (const [, v, value] of Object.keys(variants).map(k => [k, variants[k], values[k]])) {
|
18 | const variant: Variant<V> = v;
|
19 |
|
20 | if (value == null) {
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | if (new Set(variant.values).has(value) === false) {
|
27 | continue;
|
28 | }
|
29 | }
|
30 |
|
31 | const fn = typeToInjectorFunction(variant.transition, t => t.transition(value));
|
32 |
|
33 | promises.push(
|
34 | Promise.resolve(fn(injector, value))
|
35 | .catch(exception => Promise.reject(new RuntimeException(`Transition failed: ${variant.transition.name}: ${value}`, exception))));
|
36 | }
|
37 |
|
38 | return Promise.all(promises) as Promise<any>;
|
39 | };
|
40 | };
|