Skip to content

Commit fc9ac0a

Browse files
committed
feat(script_executor): allow to return a Promise for async scripts
Instead of: setAsync(); setTimeout(function(){ set('x', 2); done(); }, 500); You can write: return Promise.resolve() .then(function(){ set('x', 3); });
1 parent ca52d73 commit fc9ac0a

File tree

4 files changed

+355
-3
lines changed

4 files changed

+355
-3
lines changed

src/modules/types/client_interaction/code_executor/controller.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,10 @@ define(['modules/types/client_interaction/code_editor/controller', 'src/util/api
359359
this._done = Promise.resolve();
360360

361361
try {
362-
this.theFunction.apply(this.context, this.libs);
362+
var result = Promise.resolve(this.theFunction.apply(this.context, this.libs));
363+
if (!this._async) {
364+
this._done = result;
365+
}
363366
} catch (e) {
364367
reportError(this.title, e);
365368
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 347 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,347 @@
1+
{
2+
"version": "2.28.5-0",
3+
"grid": {
4+
"layers": {
5+
"Default layer": {
6+
"name": "Default layer"
7+
}
8+
},
9+
"xWidth": 10,
10+
"yHeight": 10
11+
},
12+
"modules": [
13+
{
14+
"url": "modules/types/client_interaction/code_executor/",
15+
"configuration": {
16+
"sections": {},
17+
"groups": {
18+
"group": [
19+
{
20+
"display": [
21+
[
22+
"editor",
23+
"buttons"
24+
]
25+
],
26+
"execOnLoad": [
27+
[]
28+
],
29+
"script": [
30+
"set('x', 1);"
31+
]
32+
}
33+
],
34+
"libs": [
35+
[
36+
{}
37+
]
38+
],
39+
"buttons": [
40+
[
41+
{
42+
"name": "button1",
43+
"label": "Execute"
44+
}
45+
]
46+
]
47+
}
48+
},
49+
"layers": {
50+
"Default layer": {
51+
"position": {
52+
"left": 0,
53+
"top": 0
54+
},
55+
"size": {
56+
"width": 32,
57+
"height": 10
58+
},
59+
"zIndex": 0,
60+
"display": true,
61+
"title": "Sync script",
62+
"bgColor": [
63+
255,
64+
255,
65+
255,
66+
0
67+
],
68+
"wrapper": true,
69+
"created": true,
70+
"name": "Default layer"
71+
}
72+
},
73+
"id": 2,
74+
"vars_in": [
75+
{}
76+
],
77+
"actions_in": [
78+
{}
79+
],
80+
"title": "Sync script",
81+
"vars_out": [
82+
{
83+
"event": "onScriptEnded",
84+
"rel": "outputValue",
85+
"jpath": [
86+
"x"
87+
],
88+
"name": "x"
89+
}
90+
],
91+
"actions_out": []
92+
},
93+
{
94+
"url": "modules/types/display/single_value/",
95+
"configuration": {
96+
"sections": {},
97+
"groups": {
98+
"group": [
99+
{
100+
"defaultvalue": [
101+
null
102+
],
103+
"font": [
104+
null
105+
],
106+
"fontcolor": [
107+
[
108+
0,
109+
0,
110+
0,
111+
1
112+
]
113+
],
114+
"fontsize": [
115+
null
116+
],
117+
"align": [
118+
null
119+
],
120+
"valign": [
121+
null
122+
],
123+
"sprintf": [
124+
null
125+
],
126+
"sprintfOrder": [
127+
null
128+
],
129+
"preformatted": [
130+
[]
131+
],
132+
"append": [
133+
[]
134+
],
135+
"maxEntries": [
136+
1
137+
]
138+
}
139+
]
140+
}
141+
},
142+
"layers": {
143+
"Default layer": {
144+
"position": {
145+
"left": 34,
146+
"top": 18
147+
},
148+
"size": {
149+
"width": 15,
150+
"height": 8
151+
},
152+
"zIndex": 0,
153+
"display": true,
154+
"title": "",
155+
"bgColor": [
156+
255,
157+
255,
158+
255,
159+
0
160+
],
161+
"wrapper": true,
162+
"created": true,
163+
"name": "Default layer"
164+
}
165+
},
166+
"id": 3,
167+
"vars_in": [
168+
{
169+
"rel": "value",
170+
"name": "x"
171+
}
172+
],
173+
"actions_in": [
174+
{}
175+
],
176+
"title": "",
177+
"vars_out": [],
178+
"actions_out": []
179+
},
180+
{
181+
"url": "modules/types/client_interaction/code_executor/",
182+
"configuration": {
183+
"sections": {},
184+
"groups": {
185+
"group": [
186+
{
187+
"display": [
188+
[
189+
"editor",
190+
"buttons"
191+
]
192+
],
193+
"execOnLoad": [
194+
[]
195+
],
196+
"script": [
197+
"setAsync();\n\nsetTimeout(function(){\n set('x', 2);\n done();\n}, 500);"
198+
]
199+
}
200+
],
201+
"libs": [
202+
[
203+
{}
204+
]
205+
],
206+
"buttons": [
207+
[
208+
{
209+
"name": "button1",
210+
"label": "Execute"
211+
}
212+
]
213+
]
214+
}
215+
},
216+
"layers": {
217+
"Default layer": {
218+
"position": {
219+
"left": 0,
220+
"top": 11
221+
},
222+
"size": {
223+
"width": 32,
224+
"height": 22
225+
},
226+
"zIndex": 0,
227+
"display": true,
228+
"title": "Async script",
229+
"bgColor": [
230+
255,
231+
255,
232+
255,
233+
0
234+
],
235+
"wrapper": true,
236+
"created": true,
237+
"name": "Default layer"
238+
}
239+
},
240+
"id": 4,
241+
"vars_in": [
242+
{}
243+
],
244+
"actions_in": [
245+
{}
246+
],
247+
"title": "Async script",
248+
"vars_out": [
249+
{
250+
"event": "onScriptEnded",
251+
"rel": "outputValue",
252+
"jpath": [
253+
"x"
254+
],
255+
"name": "x"
256+
}
257+
],
258+
"actions_out": []
259+
},
260+
{
261+
"url": "modules/types/client_interaction/code_executor/",
262+
"configuration": {
263+
"sections": {},
264+
"groups": {
265+
"group": [
266+
{
267+
"display": [
268+
[
269+
"editor",
270+
"buttons"
271+
]
272+
],
273+
"execOnLoad": [
274+
[]
275+
],
276+
"script": [
277+
"return Promise.resolve()\n .then(function(){\n set('x', 3);\n });"
278+
]
279+
}
280+
],
281+
"libs": [
282+
[
283+
{}
284+
]
285+
],
286+
"buttons": [
287+
[
288+
{
289+
"name": "button1",
290+
"label": "Execute"
291+
}
292+
]
293+
]
294+
}
295+
},
296+
"layers": {
297+
"Default layer": {
298+
"position": {
299+
"left": 0,
300+
"top": 34
301+
},
302+
"size": {
303+
"width": 32,
304+
"height": 22
305+
},
306+
"zIndex": 0,
307+
"display": true,
308+
"title": "Async script with a Promise",
309+
"bgColor": [
310+
255,
311+
255,
312+
255,
313+
0
314+
],
315+
"wrapper": true,
316+
"created": true,
317+
"name": "Default layer"
318+
}
319+
},
320+
"id": 5,
321+
"vars_in": [
322+
{}
323+
],
324+
"actions_in": [
325+
{}
326+
],
327+
"title": "Async script with a Promise",
328+
"vars_out": [
329+
{
330+
"event": "onScriptEnded",
331+
"rel": "outputValue",
332+
"jpath": [
333+
"x"
334+
],
335+
"name": "x"
336+
}
337+
],
338+
"actions_out": []
339+
}
340+
],
341+
"variables": [],
342+
"aliases": [],
343+
"pouchvariables": [],
344+
"configuration": {
345+
"title": "No title"
346+
}
347+
}

testcase/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
],
6262
'Client interaction': [
6363
{description: 'Drag and drop / paste', folder: 'dragdrop'},
64-
{description: 'Drop image for histogram', folder: 'histogram'}
64+
{description: 'Drop image for histogram', folder: 'histogram'},
65+
{description: 'Script executor', folder: 'scriptExecutor'},
66+
{description: 'Script executor 2', folder: 'scriptExecutor2'}
6567
],
6668
'Spectra displayer': [
6769
{description: 'Chart', folder: 'chart'},
@@ -87,7 +89,6 @@
8789
{description: 'House monitoring', folder: 'houseMonitoring'},
8890
{description: 'Hierarchical structure explorer', folder: 'fancytree'},
8991
{description: 'Panzoom', folder: 'panzoom'},
90-
{description: 'Script executor', folder: 'scriptExecutor'},
9192
{description: 'Loading screen API', folder: 'loading'},
9293
{description: 'Progress bar', folder: 'progress'}
9394
]

0 commit comments

Comments
 (0)