-
Notifications
You must be signed in to change notification settings - Fork 3
/
parallel-environment-definition.ts
54 lines (48 loc) · 2.65 KB
/
parallel-environment-definition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import {IParallelEnvironment} from "./parallel-environment";
import {FunctionCall} from "../function/function-call";
import {ISerializedFunctionCall} from "../function/serialized-function-call";
import {FunctionCallSerializer} from "../function/function-call-serializer";
/**
* Defines the environment for a parallel chain. The environment can consist of one or multiple static environments or
* environments created by using an environment provider. The environments are merged in the order they have been added
* to the parallel chain, meaning that later environments override the properties of earlier environments.
* The implementation tries to minimize the number of environment variables to transfer by merging the environments as much
* as possible while still maintaining the sequential order of the environments.
*/
export class ParallelEnvironmentDefinition {
public static of(environment?: FunctionCall | IParallelEnvironment) {
const definition = ParallelEnvironmentDefinition.EMPTY;
if (environment) {
return definition.add(environment);
}
return definition;
}
private static EMPTY = new ParallelEnvironmentDefinition();
private constructor (public environments: Array<IParallelEnvironment | FunctionCall> = []) { }
/**
* Adds the given environment
*
* The operation tries to keep the operation chain to a minimal in length. If two subsequent environments are
* not providers, then the definitions are merged.
* @param environment the new environment to add
* @returns {ParallelEnvironmentDefinition} the new parallel environment resulting from adding the passed in environment
* to the existing one
*/
public add(environment: FunctionCall | IParallelEnvironment): ParallelEnvironmentDefinition {
const newEnvironments = this.environments.slice();
if (!(environment instanceof FunctionCall) && this.environments.length > 0 && !(this.environments[this.environments.length - 1] instanceof FunctionCall)) {
newEnvironments[newEnvironments.length - 1] = Object.assign({}, newEnvironments[newEnvironments.length - 1], environment);
} else {
newEnvironments.push(environment);
}
return new ParallelEnvironmentDefinition(newEnvironments);
}
public toJSON(functionCallSerializer: FunctionCallSerializer): Array<ISerializedFunctionCall | IParallelEnvironment> {
return this.environments.map(environment => {
if (environment instanceof FunctionCall) {
return functionCallSerializer.serializeFunctionCall(environment);
}
return environment;
});
}
}