UNPKG

1.57 kBPlain TextView Raw
1import {Injector} from '@angular/core';
2
3import {ComposedTransition, Variant, VariantsMap} from '../application/contracts';
4
5import {RuntimeException} from '../exception';
6
7import {typeToInjectorFunction} from '../transformation';
8
9export 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 // If there is no value provided for this variant, and if null or undefined is not
22 // explicitly listed in the variant values, then we will just skip this transition
23 // altogether. If you don't want this behaviour, then either add null or undefined
24 // to the set of permissible variant values, or always provide values for each variant
25 // when you call renderUri and so forth.
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};