Skip to content

Commit

Permalink
Started with group concept.
Browse files Browse the repository at this point in the history
  • Loading branch information
JMS-1 committed Sep 19, 2024
1 parent 50e8258 commit 3f53c26
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
1 change: 1 addition & 0 deletions Library/Extensions/Builder/BlocklyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ public static TParser AddCustomBlocks<TParser>(this TParser parser, IScriptModel
/* Add library extensions: blocks, any order. */
builder.AddBlock<CreateRunScriptParameter>();
builder.AddBlock<Delay>();
builder.AddBlock<ExecutionGroup>();
builder.AddBlock<ExtractProperty>();
builder.AddBlock<HttpRequest>();
builder.AddBlock<Now>();
Expand Down
56 changes: 56 additions & 0 deletions Library/Extensions/ExecutionGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
using BlocklyNet.Core.Model;
using BlocklyNet.Extensions.Builder;
using Newtonsoft.Json;

namespace BlocklyNet.Extensions;

/// <summary>
/// Execute a function as a Blockly.NET group.
/// </summary>
[CustomBlock(
"execute_group",
"",
@"{
""message0"": ""ExecutionGroup %1 %2 %3"",
""args0"": [
{
""type"": ""input_dummy""
},
{
""type"": ""field_label_serializable"",
""name"": ""RESULT"",
""text"": ""Result""
},
{
""type"": ""input_value"",
""name"": ""RESULT""
}
],
""previousStatement"": null,
""nextStatement"": null,
""colour"": ""#107159"",
""tooltip"": ""Execute something as a group."",
""helpUrl"": """"
}",
""
)]
public class ExecutionGroup : Block
{
/// <inheritdoc/>
public override async Task<object?> EvaluateAsync(Context context)
{
/* Register the group. */
context.Engine.BeginGroup(Id);

/* Execute the function to get the group result. */
var groupResult = await Values.EvaluateAsync("RESULT", context);

/* Finish the group. */
context.Engine.EndGroup(groupResult);

/* Continue with execution. */
return await base.EvaluateAsync(context);
}
}
11 changes: 11 additions & 0 deletions Library/Scripting/Engine/IScriptSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,15 @@ public interface IScriptSite
/// </summary>
/// <param name="block">The block to execute.</param>
Task SingleStepAsync(Block block);

/// <summary>
/// Start the execution of a new group.
/// </summary>
/// <param name="key">Unique identifier of the group.</param>
void BeginGroup(string key);

/// <summary>
/// End the execution of the current group.
/// </summary>
void EndGroup(object? result);
}
10 changes: 10 additions & 0 deletions Library/Scripting/Engine/ScriptEngine.Nested.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ protected class ScriptSite(ScriptEngine engine, IScript? parent, int depth) : IS
public Task<object?> EvaluateAsync(string scriptAsXml, Dictionary<string, object?> presets) =>
_engine.Parser.Parse(scriptAsXml).EvaluateAsync(presets, this);

/// <inheritdoc/>
public void BeginGroup(string key)
{
}

/// <inheritdoc/>
public void EndGroup(object? result)
{
}

/// <inheritdoc/>
public Task<TResult> RunAsync<TResult>(StartScript request, StartScriptOptions? options = null)
=> _engine.StartChildAsync<TResult>(request, CurrentScript, options, depth);
Expand Down
15 changes: 14 additions & 1 deletion Library/Scripting/Engine/ScriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ namespace BlocklyNet.Scripting.Engine;
/// <param name="_rootProvider">Dependency injection manager.</param>
/// <param name="logger">Logging helper.</param>
/// <param name="parser">Script parser to use.</param>
public partial class ScriptEngine(IServiceProvider _rootProvider, IScriptParser parser, ILogger<ScriptEngine> logger, IScriptEngineNotifySink? context = null) : IScriptEngine, IScriptSite, IDisposable
public partial class ScriptEngine(IServiceProvider _rootProvider, IScriptParser parser, ILogger<ScriptEngine> logger, IScriptEngineNotifySink? context = null) :
IScriptEngine,
IScriptSite,
IDisposable
{
/// <inheritdoc/>
public IScriptEngine Engine => this;
Expand Down Expand Up @@ -429,4 +432,14 @@ protected virtual ScriptFinished CreateFinishNotification(IScriptInstance script

/// <inheritdoc/>
public Task SingleStepAsync(Block block) => Task.CompletedTask;

/// <inheritdoc/>
public void BeginGroup(string key)
{
}

/// <inheritdoc/>
public void EndGroup(object? result)
{
}
}

0 comments on commit 3f53c26

Please sign in to comment.