@lowdefy/engine 4.0.0-alpha.29 → 4.0.0-alpha.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,248 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { type } from '@lowdefy/helpers';
16
+ import getActionMethods from './actions/getActionMethods.js';
17
+ let Actions = class Actions {
18
+ async callAsyncAction({ action , arrayIndices , block , event , index , responses }) {
19
+ try {
20
+ const response = await this.callAction({
21
+ action,
22
+ arrayIndices,
23
+ block,
24
+ event,
25
+ index,
26
+ responses
27
+ });
28
+ responses[action.id] = response;
29
+ } catch (error) {
30
+ responses[action.id] = error;
31
+ console.error(error);
32
+ }
33
+ }
34
+ async callActionLoop({ actions , arrayIndices , block , event , progress , responses }) {
35
+ for (const [index, action] of actions.entries()){
36
+ try {
37
+ if (action.async === true) {
38
+ this.callAsyncAction({
39
+ action,
40
+ arrayIndices,
41
+ block,
42
+ event,
43
+ index,
44
+ progress,
45
+ responses
46
+ });
47
+ } else {
48
+ const response = await this.callAction({
49
+ action,
50
+ arrayIndices,
51
+ block,
52
+ event,
53
+ index,
54
+ progress,
55
+ responses
56
+ });
57
+ responses[action.id] = response;
58
+ }
59
+ } catch (error) {
60
+ responses[action.id] = error;
61
+ throw {
62
+ error,
63
+ action
64
+ };
65
+ }
66
+ }
67
+ }
68
+ async callActions({ actions , arrayIndices , block , catchActions , event , eventName , progress }) {
69
+ const startTimestamp = new Date();
70
+ const responses = {};
71
+ try {
72
+ await this.callActionLoop({
73
+ actions,
74
+ arrayIndices,
75
+ block,
76
+ event,
77
+ responses,
78
+ progress
79
+ });
80
+ } catch (error) {
81
+ console.error(error);
82
+ try {
83
+ await this.callActionLoop({
84
+ actions: catchActions,
85
+ arrayIndices,
86
+ block,
87
+ event,
88
+ responses,
89
+ progress
90
+ });
91
+ } catch (errorCatch) {
92
+ console.error(errorCatch);
93
+ return {
94
+ blockId: block.blockId,
95
+ bounced: false,
96
+ endTimestamp: new Date(),
97
+ error,
98
+ errorCatch,
99
+ event,
100
+ eventName,
101
+ responses,
102
+ startTimestamp,
103
+ success: false
104
+ };
105
+ }
106
+ return {
107
+ blockId: block.blockId,
108
+ bounced: false,
109
+ endTimestamp: new Date(),
110
+ error,
111
+ event,
112
+ eventName,
113
+ responses,
114
+ startTimestamp,
115
+ success: false
116
+ };
117
+ }
118
+ return {
119
+ blockId: block.blockId,
120
+ bounced: false,
121
+ endTimestamp: new Date(),
122
+ event,
123
+ eventName,
124
+ responses,
125
+ startTimestamp,
126
+ success: true
127
+ };
128
+ }
129
+ async callAction({ action , arrayIndices , block , event , index , progress , responses }) {
130
+ if (!this.actions[action.type]) {
131
+ throw {
132
+ error: new Error(`Invalid action type "${action.type}" at "${block.blockId}".`),
133
+ type: action.type,
134
+ index
135
+ };
136
+ }
137
+ const { output: parsedAction , errors: parserErrors } = this.context._internal.parser.parse({
138
+ actions: responses,
139
+ event,
140
+ arrayIndices,
141
+ input: action,
142
+ location: block.blockId
143
+ });
144
+ if (parserErrors.length > 0) {
145
+ throw {
146
+ error: parserErrors[0],
147
+ type: action.type,
148
+ index
149
+ };
150
+ }
151
+ if (parsedAction.skip === true) {
152
+ return {
153
+ type: action.type,
154
+ skipped: true,
155
+ index
156
+ };
157
+ }
158
+ const messages = parsedAction.messages || {};
159
+ let response;
160
+ const closeLoading = this.displayMessage({
161
+ defaultMessage: 'Loading',
162
+ duration: 0,
163
+ message: messages.loading,
164
+ status: 'loading'
165
+ });
166
+ try {
167
+ response = await this.actions[action.type]({
168
+ globals: this.context._internal.lowdefy._internal.globals,
169
+ methods: getActionMethods({
170
+ actions: responses,
171
+ arrayIndices,
172
+ blockId: block.blockId,
173
+ context: this.context,
174
+ event
175
+ }),
176
+ params: parsedAction.params
177
+ });
178
+ if (progress) {
179
+ progress();
180
+ }
181
+ } catch (error) {
182
+ responses[action.id] = {
183
+ error,
184
+ index,
185
+ type: action.type
186
+ };
187
+ const { output: parsedMessages , errors: parserErrors } = this.context._internal.parser.parse({
188
+ actions: responses,
189
+ event,
190
+ arrayIndices,
191
+ input: action.messages,
192
+ location: block.blockId
193
+ });
194
+ if (parserErrors.length > 0) {
195
+ // this condition is very unlikely since parser errors usually occur in the first parse.
196
+ throw {
197
+ error: parserErrors[0],
198
+ type: action.type,
199
+ index
200
+ };
201
+ }
202
+ closeLoading();
203
+ this.displayMessage({
204
+ defaultMessage: error.message,
205
+ duration: 6,
206
+ hideExplicitly: true,
207
+ message: (parsedMessages || {}).error,
208
+ status: 'error'
209
+ });
210
+ throw {
211
+ type: action.type,
212
+ error,
213
+ index
214
+ };
215
+ }
216
+ closeLoading();
217
+ this.displayMessage({
218
+ defaultMessage: 'Success',
219
+ message: messages.success,
220
+ status: 'success'
221
+ });
222
+ return {
223
+ type: action.type,
224
+ response,
225
+ index
226
+ };
227
+ }
228
+ displayMessage({ defaultMessage , duration , hideExplicitly , message , status }) {
229
+ let close = ()=>undefined;
230
+ if (hideExplicitly && message !== false || !hideExplicitly && !type.isNone(message)) {
231
+ close = this.context._internal.lowdefy._internal.displayMessage({
232
+ content: type.isString(message) ? message : defaultMessage,
233
+ duration,
234
+ status
235
+ });
236
+ }
237
+ return close;
238
+ }
239
+ constructor(context){
240
+ this.context = context;
241
+ this.callAction = this.callAction.bind(this);
242
+ this.callActionLoop = this.callActionLoop.bind(this);
243
+ this.callActions = this.callActions.bind(this);
244
+ this.displayMessage = this.displayMessage.bind(this);
245
+ this.actions = context._internal.lowdefy._internal.actions;
246
+ }
247
+ };
248
+ export default Actions;