< Summary

Information
Class: Elsa.Bpmn.Hosting.BpmnScopeMemory
Assembly: Elsa.Bpmn
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Bpmn/Hosting/BpmnScopeMemory.cs
Line coverage
100%
Covered lines: 17
Uncovered lines: 0
Coverable lines: 17
Total lines: 65
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
get_State()100%11100%
get_Work()100%11100%
Load(...)100%22100%
Read(...)100%66100%
Write(...)100%11100%
SaveState()100%22100%
SaveWork()100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Bpmn/Hosting/BpmnScopeMemory.cs

#LineLine coverage
 1using System.Text.Json;
 2using Bpmn.Model.State;
 3using Elsa.Workflows;
 4
 5namespace Elsa.Bpmn.Hosting;
 6
 7/// <summary>
 8/// A BPMN scope's own memory: the interpreter's execution state, and the ledger of work this scope started.
 9/// </summary>
 10/// <remarks>
 11/// Both live in the scope's <see cref="ActivityExecutionContext.Properties"/>, because that is where per-execution
 12/// state belongs and because the handle-to-context map must survive anything that rewrites
 13/// <see cref="ActivityExecutionContext.Tag"/>. Both are held as serialized JSON strings rather than as objects:
 14/// the property bag is written through <c>PolymorphicObjectConverter</c>, which silently drops the type of a value
 15/// whose type has no registered alias and hands back a loose <c>ExpandoObject</c> on the way in.
 16/// </remarks>
 17internal sealed class BpmnScopeMemory
 18{
 19    /// <summary>The property key holding the interpreter's execution state.</summary>
 20    public const string ExecutionStatePropertyKey = "Bpmn:ExecutionState";
 21
 22    /// <summary>The property key holding this scope's work ledger.</summary>
 23    public const string WorkLedgerPropertyKey = "Bpmn:WorkLedger";
 24
 225    private static readonly JsonSerializerOptions SerializerOptions = new(JsonSerializerDefaults.General);
 26
 27    private readonly ActivityExecutionContext _context;
 28
 14929    private BpmnScopeMemory(ActivityExecutionContext context, BpmnExecutionState? state, BpmnWorkLedger work)
 30    {
 14931        _context = context;
 14932        State = state;
 14933        Work = work;
 14934    }
 35
 36    /// <summary>The state the interpreter last returned, or <c>null</c> before the scope has been started.</summary>
 68937    public BpmnExecutionState? State { get; set; }
 38
 39    /// <summary>The work this scope has started and not finished.</summary>
 76140    public BpmnWorkLedger Work { get; }
 41
 42    /// <summary>Reads a scope's memory from its property bag.</summary>
 43    public static BpmnScopeMemory Load(ActivityExecutionContext context) =>
 14944        new(context, Read<BpmnExecutionState>(context, ExecutionStatePropertyKey), Read<BpmnWorkLedger>(context, WorkLed
 45
 46    /// <summary>Reads a JSON string property, or returns <c>null</c> when it is absent.</summary>
 47    public static T? Read<T>(ActivityExecutionContext context, string key) where T : class =>
 52848        context.Properties.TryGetValue(key, out var value) && value is string json && !string.IsNullOrWhiteSpace(json)
 52849            ? JsonSerializer.Deserialize<T>(json, SerializerOptions)
 52850            : null;
 51
 52    /// <summary>Writes a value as a JSON string property.</summary>
 53    public static void Write<T>(ActivityExecutionContext context, string key, T value) =>
 44654        context.Properties[key] = JsonSerializer.Serialize(value, SerializerOptions);
 55
 56    /// <summary>Persists the execution state.</summary>
 57    public void SaveState()
 58    {
 13559        if (State is not null)
 13560            Write(_context, ExecutionStatePropertyKey, State);
 13561    }
 62
 63    /// <summary>Persists the work ledger.</summary>
 20964    public void SaveWork() => Write(_context, WorkLedgerPropertyKey, Work);
 65}