-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathScriptEngine.Input.cs
More file actions
167 lines (138 loc) · 6.23 KB
/
ScriptEngine.Input.cs
File metadata and controls
167 lines (138 loc) · 6.23 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
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
using System.Text.Json;
using BlocklyNet.Extensions.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace BlocklyNet.Scripting.Engine;
partial class ScriptEngine<TLogType>
{
/// <summary>
/// Current active user reply.
/// </summary>
private UserInputRequest? _inputRequest;
/// <summary>
/// Response trigger for user replies.
/// </summary>
private TaskCompletionSource<UserInputResponse>? _inputResponse;
/// <summary>
/// Exact time the input was requested.
/// </summary>
private DateTime _inputStarted = DateTime.MinValue;
/// <summary>
/// Optional planned seconds to auto-close the input request.
/// </summary>
private double? _inputDelay = null;
/// <inheritdoc/>
public void SetUserInput(UserInputResponse? response) => SetUserInput(response, true);
private void SetUserInput(UserInputResponse? response, bool mustLock)
{
TaskCompletionSource<UserInputResponse>? inputResponse;
using (mustLock ? Lock.Wait() : null)
{
/* The script requesting the input must still be the active one. */
if (_active == null || (response != null && _active.JobId != response.JobId))
throw new ArgumentException("jobId");
/* See if there is anyone wating on the response. */
inputResponse = _inputResponse;
if (inputResponse == null)
return;
/* Copy from request. */
response ??= new UserInputResponse
{
JobId = _active.JobId,
Key = _inputRequest?.Key ?? string.Empty,
ValueType = _inputRequest?.ValueType
};
/* Clear the pending request. */
_inputRequest = null;
_inputResponse = null;
}
/* Set result outside of lock - just to reduce the minimal chance of a deadlock even further. */
Logger.LogTrace("Script {JobId} is processing {Key}={Value}.", response.JobId, response.Key, response.Value);
inputResponse.SetResult(response);
}
/// <summary>
/// Decode user input from raw value and value type.
/// </summary>
/// <param name="value">Some value - may be null.</param>
/// <param name="valueType">Type of the value - can be null.</param>
/// <param name="services">Dependency injection to use.</param>
/// <typeparam name="T">Expected type of the result.</typeparam>
/// <returns>Decoded value accoring to type or value itself if
/// decoding is not possible.</returns>
public static T? DecodeUserInput<T>(object? value, string? valueType, IServiceProvider services)
{
/* Check for array of values - each with a dedicated type. */
if (value is JsonElement array && array.ValueKind == JsonValueKind.Array && valueType?.StartsWith("[]") == true)
{
/* Number of elements must match. */
var typeNames = valueType[2..].Split("⊕");
if (typeNames.Length == array.GetArrayLength())
{
/* Convert each value. */
var values = new List<object?>();
for (var i = 0; i < typeNames.Length; i++)
values.Add(UserInputValueFromJson(array[i], typeNames[i], services));
/* Blind convert - hopefully caller knows what he is doing. */
return (T?)(object?)values.ToArray();
}
}
/* Fallback mode. */
value = UserInputValueFromJson(value, valueType, services);
return value == null ? default : (T?)value;
}
/// <inheritdoc/>
public Task<T?> GetUserInputAsync<T>(string key, string? type = null, double? delay = null, bool? required = null)
{
using (Lock.Wait())
{
/* We have no active script. */
if (_active == null)
throw new InvalidOperationException("no active script.");
/* If in the normal case there is no existing request just send the request to all clients. */
if (_inputResponse == null)
{
/* Create a new response handler. */
_inputResponse = new TaskCompletionSource<UserInputResponse>();
_inputDelay = delay;
_inputStarted = DateTime.UtcNow;
/* Tell our clients that we would like to get some input. */
var inputRequest = new UserInputRequest
{
JobId = _active.JobId,
Key = key,
SecondsToAutoClose = _inputDelay,
StartedAt = _inputStarted,
ValueType = type,
};
_context?
.SendAsync(ScriptEngineNotifyMethods.InputRequest, _inputRequest = inputRequest)
.ContinueWith(
t => Logger.LogError("Failed to request user input for script {JobId}: {Exception}", inputRequest.JobId, t.Exception?.Message),
CancellationToken.None,
TaskContinuationOptions.NotOnRanToCompletion,
TaskScheduler.Current)
.Touch();
}
/* Report a promise on the result. */
Logger.LogTrace("Script {JobId} is requesting input for {Key}.", _active.JobId, key);
return _inputResponse
.Task
.ContinueWith(
t => DecodeUserInput<T>(t.Result.Value, t.Result.ValueType, ServiceProvider),
CancellationToken.None,
TaskContinuationOptions.OnlyOnRanToCompletion,
TaskScheduler.Current
);
}
}
private static object? UserInputValueFromJson(object? value, string? type, IServiceProvider services)
{
/* Not JSON. */
if (value is not JsonElement json) return value;
/* Reconstruct to known model. */
if (!string.IsNullOrEmpty(type) && services.GetRequiredService<IScriptModels>().Models.TryGetValue(type, out var model))
return JsonSerializer.Deserialize(value?.ToString() ?? "null", model.Type, JsonUtils.JsonSettings);
/* Just check for scalar. */
return json.ToJsonScalar();
}
}