forked from ZeraGmbH/Blockly.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Values.cs
61 lines (53 loc) · 2.21 KB
/
Values.cs
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
namespace BlocklyNet.Core.Model;
/// <summary>
/// All values in a block.
/// </summary>
public class Values : Entities<Value>
{
/// <summary>
/// Retrieve the key to use for value identification.
/// </summary>
/// <param name="value">Some value.</param>
/// <returns>The name of the value.</returns>
protected override string GetKey(Value value) => value.Name;
/// <summary>
/// Evaluate a single value.
/// </summary>
/// <param name="name">Name of the value.</param>
/// <param name="context">Execution context.</param>
/// <param name="required">If set the indicated value must exist.</param>
/// <returns>Current result of the value.</returns>
/// <exception cref="ArgumentException">Value does not exist.</exception>
public Task<object?> EvaluateAsync(string name, Context context, bool required = true)
{
/* See if the value is known */
var value = TryGet(name);
if (value == null)
{
/* The value is optional so just indicate a null result. */
if (!required) return Task.FromResult((object?)null);
/* Value must exist so throw an exception. */
throw new ArgumentException($"value {name} not found");
}
/* Before executing see if the script should be cancelled. */
context.Cancellation.ThrowIfCancellationRequested();
/* Try to evaluate the value. */
return value.EvaluateAsync(context);
}
/// <summary>
/// Evaluate a single value.
/// </summary>
/// <param name="name">Name of the value.</param>
/// <param name="context">Execution context.</param>
/// <param name="required">If set the indicated value must exist.</param>
/// <typeparam name="T">Expected result type.</typeparam>
/// <returns>Current result of the value.</returns>
/// <exception cref="ArgumentException">Value does not exist.</exception>
public async Task<T> EvaluateAsync<T>(string name, Context context, bool required = true)
{
/* Execute the script. */
var result = await EvaluateAsync(name, context, required);
/* Try to change type of result if possible. */
return result == null ? default! : (T)result;
}
}