-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.ts
143 lines (125 loc) · 4.52 KB
/
action.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { IResolvedOperation } from '../configuration/types';
import { Diagnostics } from '../diagnostics';
import { IEventbus } from '../eventbus/types';
import { endForEachSystemName, forEachSystemName } from '../operation/for-each';
import { deepCopy } from '../operation/helper/deep-copy';
import { IOperationContext, TOperationData } from '../operation/types';
import { endWhenSystemName, whenSystemName } from '../operation/when';
import { isPromise } from '../util/guards/is-promise';
import { IAction } from './types';
export class Action implements IAction {
public id = '';
protected _contextStack: IOperationContext[] = [];
constructor(public name: string, public startOperations: IResolvedOperation[], protected eventbus: IEventbus) {}
start(initOperationData?: TOperationData): Promise<TOperationData> {
Diagnostics.active &&
Diagnostics.send('eligius-diagnostics-action', `${this.name ?? 'Action'} begins executing start operations`);
this._initializeContextStack();
const result = new Promise<TOperationData>((resolve, reject) => {
this.executeOperation(this.startOperations, 0, resolve, reject, initOperationData);
}).catch((e) => {
Diagnostics.active &&
Diagnostics.send('eligius-diagnostics-action-error', {
name: this.name,
error: e,
});
console.error(`Error in action start '${this.name}'`);
console.error(e);
throw e;
});
return !Diagnostics.active
? result
: result.then((operationsResult) => {
Diagnostics.send(
'eligius-diagnostics-action',
`${this.name ?? 'Action'} finished executing start operations`
);
return operationsResult;
});
}
protected _initializeContextStack() {
this._contextStack = [
{
currentIndex: -1,
eventbus: this.eventbus,
operations: this.startOperations,
},
];
}
private _pushContext(parentContext: IOperationContext): IOperationContext {
const newContext = {
get currentIndex() {
return parentContext.currentIndex;
},
set currentIndex(value: number) {
parentContext.currentIndex = value;
},
get eventbus() {
return parentContext.eventbus;
},
get operations() {
return parentContext.operations;
},
parent: parentContext,
};
this._contextStack.push(newContext);
return newContext;
}
private _popContext() {
this._contextStack.pop();
}
executeOperation(
operations: IResolvedOperation[],
operationIndex: number,
resolve: (value?: any | PromiseLike<any>) => void,
reject: (reason?: any) => void,
previousOperationData: TOperationData | undefined = {}
): void {
let context = this._contextStack[this._contextStack.length - 1];
if (context.newIndex !== undefined) {
operationIndex = context.newIndex;
delete context.newIndex;
}
context.currentIndex = operationIndex;
if (operationIndex < operations.length) {
const operationInfo = operations[operationIndex];
const copy = deepCopy(operationInfo.operationData ?? {});
const mergedOperationData = Object.assign(previousOperationData, copy);
if (operationInfo.systemName === whenSystemName) {
context = this._pushContext(context);
} else if (operationInfo.systemName === forEachSystemName && context.owner !== operationInfo) {
context = this._pushContext(context);
context.owner = operationInfo;
}
Diagnostics.active &&
Diagnostics.send('eligius-diagnostics-operation', {
systemName: operationInfo.systemName,
operationData: mergedOperationData,
context: {
...context,
eventbus: undefined,
},
});
const operationResult = operationInfo.instance.call(context, mergedOperationData);
if (
operationInfo.systemName === endWhenSystemName ||
(operationInfo.systemName === endForEachSystemName && context.newIndex === undefined)
) {
this._popContext();
}
if (isPromise(operationResult)) {
operationResult
.then((promisedOperationResult) =>
this.executeOperation(operations, ++operationIndex, resolve, reject, promisedOperationResult)
)
.catch((error: any) => {
reject(error);
});
} else {
this.executeOperation(operations, ++operationIndex, resolve, reject, operationResult);
}
} else {
resolve(previousOperationData);
}
}
}