forked from ZeraGmbH/Blockly.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.cs
More file actions
54 lines (45 loc) · 1.92 KB
/
Script.cs
File metadata and controls
54 lines (45 loc) · 1.92 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
using BlocklyNet.Scripting.Engine;
using BlocklyNet.Scripting.Logging;
using Microsoft.Extensions.DependencyInjection;
namespace BlocklyNet.Scripting;
/// <summary>
/// Official base class for implementing scripts.
/// </summary>
/// <typeparam name="TRequest">Type of the configuration.</typeparam>
/// <typeparam name="TResult">Result type of the script execution.</typeparam>
/// <typeparam name="TOption">Option class to use.</typeparam>
/// <typeparam name="TLogType">Type of each log entry.</typeparam>
/// <typeparam name="TModifierType">Implementation class of log modifiers.</typeparam>
public abstract class Script<TRequest, TResult, TOption, TLogType, TModifierType>(TRequest request, IScriptSite engine, TOption? options) : Script<TOption, TLogType, TModifierType>(options)
where TRequest : StartScript
where TOption : StartScriptOptions
where TLogType : ScriptLoggingResult, new()
where TModifierType : ScriptLogModifier
{
/// <summary>
/// Configuration of the script.
/// </summary>
public TRequest Request { get; } = request;
/// <inheritdoc/>
public void SetResult(TResult result) => Result = result;
/// <inheritdoc/>
public override StartScript GetRequest() => Request;
/// <inheritdoc/>
public override IScriptSite Engine { get; } = engine;
/// <summary>
/// Test for cancel.
/// </summary>
protected void CheckCancel() => Engine.Cancellation.ThrowIfCancellationRequested();
/// <summary>
/// Access a runtime service.
/// </summary>
/// <typeparam name="T">Type of the service.</typeparam>
/// <returns>Service as requested.</returns>
protected T GetService<T>() where T : notnull => Engine.ServiceProvider.GetRequiredService<T>();
/// <summary>
/// Execute the script.
/// </summary>
protected abstract Task OnExecuteAsync();
/// <inheritdoc/>
public sealed override Task ExecuteAsync() => OnExecuteAsync();
}