< Summary

Information
Class: Chronicler.ChronicleContext
Assembly: Chronicler
File(s): /home/runner/work/Chronicler/Chronicler/src/Chronicler/Recording/ChronicleContext.cs
Line coverage
100%
Covered lines: 44
Uncovered lines: 0
Coverable lines: 44
Total lines: 141
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
QueueDeferredLink(...)100%11100%
ResolveDeferredLinks()100%44100%
ResolveQueuedLinks()100%44100%
ResolveQueuedLinkPass()100%44100%
ThrowUnresolvedDeferredLinks()100%22100%
.ctor(...)100%22100%
TryResolve(...)100%22100%
Describe()100%22100%

File(s)

/home/runner/work/Chronicler/Chronicler/src/Chronicler/Recording/ChronicleContext.cs

#LineLine coverage
 1//=======================================================================
 2// ChronicleContext.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024-present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using System;
 9using System.Collections.Generic;
 10using System.Text;
 11
 12namespace Chronicler;
 13
 14/// <summary>
 15/// Holds link registrations and pending link assignments for a recording session.
 16/// </summary>
 17public sealed class ChronicleContext
 18{
 61019    private readonly List<IDeferredRecordLink> _deferredLinks = new();
 20
 21    /// <summary>
 22    /// Creates a new context with an empty link registry.
 23    /// </summary>
 61024    public ChronicleContext()
 25    {
 61026        Links = new ChronicleLinkRegistry();
 61027    }
 28
 29    /// <summary>
 30    /// Gets the link registry for this session.
 31    /// </summary>
 32    /// <value>The same mutable registry throughout the context's lifetime.</value>
 33    public ChronicleLinkRegistry Links { get; }
 34
 35    internal void QueueDeferredLink<T>(string name, string id, string? slot, Action<T> assignLoadedValue)
 36    {
 737        _deferredLinks.Add(new DeferredRecordLink<T>(name, id, slot, assignLoadedValue));
 638    }
 39
 40    /// <summary>
 41    /// Resolves pending links and invokes their assignment callbacks.
 42    /// </summary>
 43    /// <remarks>
 44    /// The built-in serializers call this after the root <see cref="IRecordable.RecordData"/> returns.
 45    /// Each pass checks pending links from newest to oldest. Passes continue while links resolve.
 46    /// Callbacks run synchronously and may register targets for other pending links.
 47    /// Completed assignments remain applied on failure, and pending callbacks stay queued in this context.
 48    /// </remarks>
 49    /// <exception cref="InvalidOperationException">A pass makes no progress and links remain unresolved.</exception>
 50    public void ResolveDeferredLinks()
 51    {
 10852        if (_deferredLinks.Count == 0)
 10253            return;
 54
 655        ResolveQueuedLinks();
 56
 657        if (_deferredLinks.Count > 0)
 458            ThrowUnresolvedDeferredLinks();
 259    }
 60
 61    private void ResolveQueuedLinks()
 62    {
 863        while (_deferredLinks.Count > 0)
 64        {
 665            if (ResolveQueuedLinkPass() == 0)
 466                return;
 67        }
 268    }
 69
 70    private int ResolveQueuedLinkPass()
 71    {
 672        int resolvedCount = 0;
 2473        for (int i = _deferredLinks.Count - 1; i >= 0; i--)
 74        {
 675            if (!_deferredLinks[i].TryResolve(Links))
 76                continue;
 77
 278            _deferredLinks.RemoveAt(i);
 279            resolvedCount++;
 80        }
 81
 682        return resolvedCount;
 83    }
 84
 85    private void ThrowUnresolvedDeferredLinks()
 86    {
 487        var builder = new StringBuilder();
 488        builder.Append("Unable to resolve deferred links:");
 1689        foreach (IDeferredRecordLink deferredLink in _deferredLinks)
 90        {
 491            builder.Append(' ');
 492            builder.Append('[');
 493            builder.Append(deferredLink.Describe());
 494            builder.Append(']');
 95        }
 96
 497        throw new InvalidOperationException(builder.ToString());
 98    }
 99
 100    private interface IDeferredRecordLink
 101    {
 102        bool TryResolve(ChronicleLinkRegistry registry);
 103
 104        string Describe();
 105    }
 106
 107    private sealed class DeferredRecordLink<T> : IDeferredRecordLink
 108    {
 109        private readonly string _name;
 110        private readonly string _id;
 111        private readonly string? _slot;
 112        private readonly Action<T> _assignLoadedValue;
 113
 7114        public DeferredRecordLink(string name, string id, string? slot, Action<T> assignLoadedValue)
 115        {
 7116            _name = name;
 7117            _id = id;
 7118            _slot = slot;
 7119            _assignLoadedValue = assignLoadedValue ?? throw new ArgumentNullException(nameof(assignLoadedValue));
 6120        }
 121
 122        public bool TryResolve(ChronicleLinkRegistry registry)
 123        {
 124            T? value;
 6125            if (!registry.TryResolve(_id, out value, _slot))
 4126                return false;
 127
 2128            _assignLoadedValue(value!);
 2129            return true;
 130        }
 131
 132        public string Describe()
 133        {
 4134            string typeName = typeof(T).Name;
 4135            if (string.IsNullOrEmpty(_slot))
 2136                return $"{_name}:{typeName}:{_id}";
 137
 2138            return $"{_name}:{typeName}:{_id}@{_slot}";
 139        }
 140    }
 141}