-
Notifications
You must be signed in to change notification settings - Fork 0
/
action-creator-factory.ts
169 lines (145 loc) · 4.36 KB
/
action-creator-factory.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { v4 as uuidv4 } from 'uuid';
import * as operations from '../../operation';
import { deepCopy } from '../../operation/helper/deep-copy';
import { TOperation, TOperationData } from '../../operation/types';
import {
ExtractDataType,
IActionConfiguration,
IEndableActionConfiguration,
ITimelineActionConfiguration,
} from '../types';
import { ConfigurationFactory } from './configuration-factory';
export class ActionCreatorFactory {
constructor(private readonly configurationfactory: ConfigurationFactory) {}
createAction(name: string): EndableActionCreator {
const creator = new EndableActionCreator(name, this);
this.configurationfactory.addAction(creator.actionConfig);
return creator;
}
createInitAction(name: string): EndableActionCreator {
const creator = new EndableActionCreator(name, this);
this.configurationfactory.addInitAction(creator.actionConfig);
return creator;
}
createEventAction(name: string): ActionCreator {
const creator = new ActionCreator(name, this);
this.configurationfactory.addEventAction(creator.actionConfig);
return creator;
}
createTimelineAction(uri: string, name: string): TimelineActionCreator {
const creator = new TimelineActionCreator(name, this);
this.configurationfactory.addTimelineAction(uri, creator.actionConfig);
return creator;
}
end() {
return this.configurationfactory;
}
}
export class ActionCreator<
T extends IActionConfiguration = IActionConfiguration
> {
actionConfig: T;
constructor(
name: string | undefined,
private readonly factory: ActionCreatorFactory
) {
this.actionConfig = {
name: '',
id: uuidv4(),
startOperations: [],
} as any;
if (name?.length) {
this.actionConfig.name = name;
}
}
getId() {
return this.actionConfig.id;
}
setName(name: string) {
this.actionConfig.name = name;
return this;
}
getConfiguration(callBack: (config: T) => T) {
const copy = deepCopy(this.actionConfig);
const newConfig = callBack.call(this, copy);
if (newConfig) {
this.actionConfig = newConfig;
}
return this;
}
addStartOperationByType<T extends TOperation<any>>(
operationClass: T,
operationData: Partial<ExtractDataType<T>>
) {
const entries = Object.entries(operations).find(
([, value]) => value === operationClass
);
if (entries) {
return this.addStartOperation(entries[0], operationData);
}
throw new Error(`Operation class not found: ${operationClass.toString()}`);
}
addStartOperation(systemName: string, operationData: TOperationData) {
if (!(operations as Record<string, any>)[systemName]) {
throw Error(`Unknown operation: ${systemName}`);
}
const { startOperations } = this.actionConfig;
startOperations.push({
id: uuidv4(),
systemName: systemName,
operationData: operationData,
});
return this;
}
next() {
return this.factory;
}
}
export class EndableActionCreator<
T extends IEndableActionConfiguration = IEndableActionConfiguration
> extends ActionCreator<T> {
constructor(name: string | undefined, factory: ActionCreatorFactory) {
super(name, factory);
this.actionConfig.endOperations = [];
}
addEndOperationByType<T extends TOperation<any>>(
operationClass: T,
operationData: Partial<ExtractDataType<T>>
) {
const entries = Object.entries(operations).find(
([, value]) => value === operationClass
);
if (entries) {
return this.addEndOperation(entries[0], operationData);
}
throw new Error(`Operation class not found: ${operationClass.toString()}`);
}
addEndOperation(systemName: string, operationData: TOperationData) {
if (!(operations as Record<string, any>)[systemName]) {
throw Error(`Unknown operation: ${systemName}`);
}
let { endOperations } = this.actionConfig;
if (!endOperations) {
endOperations = this.actionConfig.endOperations = [];
}
endOperations.push({
id: uuidv4(),
systemName: systemName,
operationData: operationData,
});
return this;
}
}
export class TimelineActionCreator extends EndableActionCreator<
ITimelineActionConfiguration
> {
addDuration(start: number, end?: number) {
this.actionConfig.duration = {
start: start,
};
if (end) {
this.actionConfig.duration.end = end;
}
return this;
}
}