| | | 1 | | using Bpmn.Semantics; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Bpmn.Hosting; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// A BPMN scope's record of the work it has started and not finished — the handle-to-context map, and the source of |
| | | 7 | | /// <see cref="BpmnHostSnapshot.LiveWork"/>. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// <para> |
| | | 11 | | /// What belongs in here depends on which callback the scope is about to make, and the difference is load-bearing: |
| | | 12 | | /// completing and faulting work must already have been removed, because leaving it lets a re-armed non-interrupting |
| | | 13 | | /// listener key onto the same <c>(binding ref, iteration id)</c> slot and a teardown then targets the work that just |
| | | 14 | | /// finished; signalling work must still be present, because an escalating activity keeps running and removing it |
| | | 15 | | /// makes the interpreter believe it has already gone. |
| | | 16 | | /// </para> |
| | | 17 | | /// <para> |
| | | 18 | | /// The interpreter re-finds a parked token from <c>(binding ref, iteration id)</c> alone, so a scope must never hold |
| | | 19 | | /// two live units of work under one such pair. Multi-instance instances share a binding ref and are told apart by |
| | | 20 | | /// their iteration id, which is what it is for. |
| | | 21 | | /// </para> |
| | | 22 | | /// </remarks> |
| | | 23 | | internal sealed class BpmnWorkLedger |
| | | 24 | | { |
| | | 25 | | /// <summary>The handle counter. Handles are scope-local so a scope's trace reads independently of Elsa's id generat |
| | 627 | 26 | | public int Sequence { get; set; } |
| | | 27 | | |
| | | 28 | | /// <summary>The live work, in start order.</summary> |
| | 1239 | 29 | | public List<BpmnWorkRecord> Records { get; set; } = []; |
| | | 30 | | |
| | | 31 | | /// <summary>Mints the next scope-local handle.</summary> |
| | 102 | 32 | | public string NextHandle() => $"work-{++Sequence}"; |
| | | 33 | | |
| | | 34 | | /// <summary>The live work running in the given child activity execution, or <c>null</c>.</summary> |
| | | 35 | | public BpmnWorkRecord? FindByChildContextId(string childContextId) => |
| | 209 | 36 | | Records.FirstOrDefault(x => string.Equals(x.ChildContextId, childContextId, StringComparison.Ordinal)); |
| | | 37 | | |
| | | 38 | | /// <summary>The live work with the given handle, or <c>null</c>.</summary> |
| | | 39 | | public BpmnWorkRecord? FindByHandle(string handle) => |
| | 16 | 40 | | Records.FirstOrDefault(x => string.Equals(x.Handle, handle, StringComparison.Ordinal)); |
| | | 41 | | |
| | | 42 | | /// <summary>Drops a record, so the work is no longer reported as live.</summary> |
| | 99 | 43 | | public void Remove(BpmnWorkRecord record) => Records.Remove(record); |
| | | 44 | | |
| | | 45 | | /// <summary>The interpreter's view of this scope's live work.</summary> |
| | | 46 | | public IReadOnlyCollection<BpmnLiveWork> ToLiveWork() => |
| | 156 | 47 | | Records.Select(x => new BpmnLiveWork(x.BindingRef, x.IterationId, x.Handle)).ToArray(); |
| | | 48 | | } |