< Summary

Information
Class: Elsa.Expressions.JavaScript.Handlers.ConfigureEngineWithVariablesAndInputOutputAccessors
Assembly: Elsa.Expressions.JavaScript
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/Handlers/ConfigureEngineWithVariablesAndInputOutputAccessors.cs
Line coverage
97%
Covered lines: 33
Uncovered lines: 1
Coverable lines: 34
Total lines: 80
Line coverage: 97%
Branch coverage
87%
Covered branches: 14
Total branches: 16
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
HandleAsync()50%2287.5%
CreateVariableAccessors(...)100%22100%
CreateWorkflowInputAccessors(...)87.5%88100%
CreateActivityOutputAccessorsAsync()100%44100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/Handlers/ConfigureEngineWithVariablesAndInputOutputAccessors.cs

#LineLine coverage
 1using Elsa.Expressions.Models;
 2using Elsa.Extensions;
 3using Elsa.Expressions.JavaScript.Extensions;
 4using Elsa.Expressions.JavaScript.Notifications;
 5using Elsa.Expressions.JavaScript.Options;
 6using Elsa.Mediator.Contracts;
 7using Humanizer;
 8using JetBrains.Annotations;
 9using Jint;
 10using Microsoft.Extensions.Options;
 11
 12namespace Elsa.Expressions.JavaScript.Handlers;
 13
 14/// <summary>
 15/// A handler that configures the Jint engine with workflow input and output accessors.
 16/// </summary>
 17[UsedImplicitly]
 52018public class ConfigureEngineWithVariablesAndInputOutputAccessors(IOptions<JintOptions> options) : INotificationHandler<E
 19{
 20    /// <inheritdoc />
 21    public async Task HandleAsync(EvaluatingJavaScript notification, CancellationToken cancellationToken)
 22    {
 18823        if (options.Value.DisableWrappers)
 024            return;
 25
 18826        var engine = notification.Engine;
 18827        var context = notification.Context;
 28
 29        // The order of the next 3 lines is important.
 18830        CreateVariableAccessors(engine, context);
 18831        CreateWorkflowInputAccessors(engine, context);
 18832        await CreateActivityOutputAccessorsAsync(engine, context);
 18833    }
 34
 35    private void CreateVariableAccessors(Engine engine, ExpressionExecutionContext context)
 36    {
 18837        var variableNames = context.GetVariableNamesInScope().FilterInvalidVariableNames().ToList();
 38
 64239        foreach (var variableName in variableNames)
 40        {
 13341            var pascalName = variableName.Pascalize();
 16542            engine.SetValue($"get{pascalName}", (Func<object?>)(() => context.GetVariableInScope(variableName)));
 13343            engine.SetValue($"set{pascalName}", (Action<object?>)(value =>
 13344            {
 345                engine.SyncVariablesContainer(options, variableName, value);
 346                context.SetVariableInScope(variableName, value);
 13647            }));
 48        }
 18849    }
 50
 51    private void CreateWorkflowInputAccessors(Engine engine, ExpressionExecutionContext context)
 52    {
 53        // Create workflow input accessors - only if the current activity is not part of a composite activity definition
 54        // Otherwise, the workflow input accessors will hide the composite activity input accessors which rely on variab
 18855        if (context.IsContainedWithinCompositeActivity())
 856            return;
 57
 24258        var inputs = context.GetWorkflowInputs().Where(x => x.Name.IsValidVariableName()).ToDictionary(x => x.Name, Stri
 59
 18060        if (!context.TryGetWorkflowExecutionContext(out var workflowExecutionContext))
 6961            return;
 62
 11163        var inputDefinitions = workflowExecutionContext.Workflow.Inputs;
 64
 23265        foreach (var inputDefinition in inputDefinitions)
 66        {
 567            var input = inputs.GetValueOrDefault(inputDefinition.Name);
 968            engine.SetValue($"get{inputDefinition.Name}", (Func<object?>)(() => input?.Value));
 69        }
 11170    }
 71
 72    private static async Task CreateActivityOutputAccessorsAsync(Engine engine, ExpressionExecutionContext context)
 73    {
 18874        var activityOutputs = context.GetActivityOutputs();
 75
 47676        await foreach (var activityOutput in activityOutputs)
 20077        foreach (var outputName in activityOutput.OutputNames.FilterInvalidVariableNames())
 5178            engine.SetValue($"get{outputName}From{activityOutput.ActivityName.Pascalize()}", (Func<object?>)(() => conte
 18879    }
 80}