| | | 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 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Text; |
| | | 11 | | |
| | | 12 | | namespace Chronicler; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Holds link registrations and pending link assignments for a recording session. |
| | | 16 | | /// </summary> |
| | | 17 | | public sealed class ChronicleContext |
| | | 18 | | { |
| | 610 | 19 | | private readonly List<IDeferredRecordLink> _deferredLinks = new(); |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Creates a new context with an empty link registry. |
| | | 23 | | /// </summary> |
| | 610 | 24 | | public ChronicleContext() |
| | | 25 | | { |
| | 610 | 26 | | Links = new ChronicleLinkRegistry(); |
| | 610 | 27 | | } |
| | | 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 | | { |
| | 7 | 37 | | _deferredLinks.Add(new DeferredRecordLink<T>(name, id, slot, assignLoadedValue)); |
| | 6 | 38 | | } |
| | | 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 | | { |
| | 108 | 52 | | if (_deferredLinks.Count == 0) |
| | 102 | 53 | | return; |
| | | 54 | | |
| | 6 | 55 | | ResolveQueuedLinks(); |
| | | 56 | | |
| | 6 | 57 | | if (_deferredLinks.Count > 0) |
| | 4 | 58 | | ThrowUnresolvedDeferredLinks(); |
| | 2 | 59 | | } |
| | | 60 | | |
| | | 61 | | private void ResolveQueuedLinks() |
| | | 62 | | { |
| | 8 | 63 | | while (_deferredLinks.Count > 0) |
| | | 64 | | { |
| | 6 | 65 | | if (ResolveQueuedLinkPass() == 0) |
| | 4 | 66 | | return; |
| | | 67 | | } |
| | 2 | 68 | | } |
| | | 69 | | |
| | | 70 | | private int ResolveQueuedLinkPass() |
| | | 71 | | { |
| | 6 | 72 | | int resolvedCount = 0; |
| | 24 | 73 | | for (int i = _deferredLinks.Count - 1; i >= 0; i--) |
| | | 74 | | { |
| | 6 | 75 | | if (!_deferredLinks[i].TryResolve(Links)) |
| | | 76 | | continue; |
| | | 77 | | |
| | 2 | 78 | | _deferredLinks.RemoveAt(i); |
| | 2 | 79 | | resolvedCount++; |
| | | 80 | | } |
| | | 81 | | |
| | 6 | 82 | | return resolvedCount; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | private void ThrowUnresolvedDeferredLinks() |
| | | 86 | | { |
| | 4 | 87 | | var builder = new StringBuilder(); |
| | 4 | 88 | | builder.Append("Unable to resolve deferred links:"); |
| | 16 | 89 | | foreach (IDeferredRecordLink deferredLink in _deferredLinks) |
| | | 90 | | { |
| | 4 | 91 | | builder.Append(' '); |
| | 4 | 92 | | builder.Append('['); |
| | 4 | 93 | | builder.Append(deferredLink.Describe()); |
| | 4 | 94 | | builder.Append(']'); |
| | | 95 | | } |
| | | 96 | | |
| | 4 | 97 | | 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 | | |
| | 7 | 114 | | public DeferredRecordLink(string name, string id, string? slot, Action<T> assignLoadedValue) |
| | | 115 | | { |
| | 7 | 116 | | _name = name; |
| | 7 | 117 | | _id = id; |
| | 7 | 118 | | _slot = slot; |
| | 7 | 119 | | _assignLoadedValue = assignLoadedValue ?? throw new ArgumentNullException(nameof(assignLoadedValue)); |
| | 6 | 120 | | } |
| | | 121 | | |
| | | 122 | | public bool TryResolve(ChronicleLinkRegistry registry) |
| | | 123 | | { |
| | | 124 | | T? value; |
| | 6 | 125 | | if (!registry.TryResolve(_id, out value, _slot)) |
| | 4 | 126 | | return false; |
| | | 127 | | |
| | 2 | 128 | | _assignLoadedValue(value!); |
| | 2 | 129 | | return true; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | public string Describe() |
| | | 133 | | { |
| | 4 | 134 | | string typeName = typeof(T).Name; |
| | 4 | 135 | | if (string.IsNullOrEmpty(_slot)) |
| | 2 | 136 | | return $"{_name}:{typeName}:{_id}"; |
| | | 137 | | |
| | 2 | 138 | | return $"{_name}:{typeName}:{_id}@{_slot}"; |
| | | 139 | | } |
| | | 140 | | } |
| | | 141 | | } |