-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRequestUserInput.cs
More file actions
108 lines (97 loc) · 2.9 KB
/
Copy pathRequestUserInput.cs
File metadata and controls
108 lines (97 loc) · 2.9 KB
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
using BlocklyNet.Core.Model;
using BlocklyNet.Extensions.Builder;
namespace BlocklyNet.Extensions;
/// <summary>
/// Request data from the user.
/// </summary>
[CustomBlock(
"request_user_input",
"Scripts",
@"{
""message0"": ""AwaitUserInteraction %1 Key %2 Type %3 Required %4 Auto close after (s) %5 Exception on auto close %6"",
""args0"": [
{
""type"": ""input_dummy""
},
{
""type"": ""input_value"",
""name"": ""KEY"",
""check"": ""String""
},
{
""type"": ""input_value"",
""name"": ""TYPE""
},
{
""type"": ""input_value"",
""name"": ""REQUIRED"",
""check"": ""Boolean""
},
{
""type"": ""input_value"",
""name"": ""DELAY"",
""check"": ""Number""
},
{
""type"": ""input_value"",
""name"": ""THROWMESSAGE"",
""check"": ""String""
}
],
""output"": null,
""colour"": 230,
""tooltip"": ""Request interaction from the user"",
""helpUrl"": """"
}",
@"{
""inputs"": {
""KEY"": {
""shadow"": {
""type"": ""text"",
""fields"": {
""TEXT"": """"
}
}
},
""TYPE"": {
""shadow"": {
""type"": ""text"",
""fields"": {
""TEXT"": """"
}
}
}
}
}"
)]
public class RequestUserInput : Block
{
/// <inheritdoc/>
protected override async Task<object?> EvaluateAsync(Context context)
{
var delay = await Values.EvaluateOptionalDoubleAsync("DELAY", context);
var key = await Values.EvaluateAsync<string>("KEY", context);
var required = await Values.EvaluateAsync<bool?>("REQUIRED", context, false);
var secs = delay.GetValueOrDefault(0);
var type = await Values.EvaluateAsync<string>("TYPE", context, false);
/* No delay necessary - just wait for the reply to be available. */
if (secs <= 0) return await context.Engine.GetUserInputAsync<object>(key, type, required: required);
var cancel = new CancellationTokenSource();
var delayTask = Task.Delay(TimeSpan.FromSeconds(secs), cancel.Token);
var inputTask = context.Engine.GetUserInputAsync<object>(key, type, delay, required);
/* See which task terminates first. */
if (inputTask == await Task.WhenAny(inputTask, delayTask))
{
/* Cancel timer. */
await cancel.CancelAsync();
/* Report result. */
return await inputTask;
}
/* Simulate user input. */
await context.Engine.Engine.SetUserInputAsync(null);
/* May want to throw an exception. */
var message = await Values.EvaluateAsync<string?>("THROWMESSAGE", context, false);
if (message != null) throw new TimeoutException(message);
return null;
}
}