| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Text; |
| | | 4 | | |
| | | 5 | | namespace Chronicler; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Carries session-scoped serialization services such as stable link resolution. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class ChronicleContext |
| | | 11 | | { |
| | 169 | 12 | | private readonly List<IDeferredRecordLink> _deferredLinks = new(); |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Creates a new context with an empty link registry. |
| | | 16 | | /// </summary> |
| | 169 | 17 | | public ChronicleContext() |
| | | 18 | | { |
| | 169 | 19 | | Links = new ChronicleLinkRegistry(); |
| | 169 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the registry used to save and load stable links to external or runtime-owned objects. |
| | | 24 | | /// </summary> |
| | | 25 | | public ChronicleLinkRegistry Links { get; } |
| | | 26 | | |
| | | 27 | | internal void QueueDeferredLink<T>(string name, string id, string? slot, Action<T> assignLoadedValue) |
| | | 28 | | { |
| | 7 | 29 | | _deferredLinks.Add(new DeferredRecordLink<T>(name, id, slot, assignLoadedValue)); |
| | 6 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Attempts to resolve all deferred links queued during loading. |
| | | 34 | | /// </summary> |
| | | 35 | | public void ResolveDeferredLinks() |
| | | 36 | | { |
| | 45 | 37 | | if (_deferredLinks.Count == 0) |
| | 39 | 38 | | return; |
| | | 39 | | |
| | 6 | 40 | | ResolveQueuedLinks(); |
| | | 41 | | |
| | 6 | 42 | | if (_deferredLinks.Count > 0) |
| | 4 | 43 | | ThrowUnresolvedDeferredLinks(); |
| | 2 | 44 | | } |
| | | 45 | | |
| | | 46 | | private void ResolveQueuedLinks() |
| | | 47 | | { |
| | 8 | 48 | | while (_deferredLinks.Count > 0) |
| | | 49 | | { |
| | 6 | 50 | | if (ResolveQueuedLinkPass() == 0) |
| | 4 | 51 | | return; |
| | | 52 | | } |
| | 2 | 53 | | } |
| | | 54 | | |
| | | 55 | | private int ResolveQueuedLinkPass() |
| | | 56 | | { |
| | 6 | 57 | | int resolvedCount = 0; |
| | 24 | 58 | | for (int i = _deferredLinks.Count - 1; i >= 0; i--) |
| | | 59 | | { |
| | 6 | 60 | | if (!_deferredLinks[i].TryResolve(Links)) |
| | | 61 | | continue; |
| | | 62 | | |
| | 2 | 63 | | _deferredLinks.RemoveAt(i); |
| | 2 | 64 | | resolvedCount++; |
| | | 65 | | } |
| | | 66 | | |
| | 6 | 67 | | return resolvedCount; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | private void ThrowUnresolvedDeferredLinks() |
| | | 71 | | { |
| | 4 | 72 | | var builder = new StringBuilder(); |
| | 4 | 73 | | builder.Append("Unable to resolve deferred links:"); |
| | 16 | 74 | | foreach (IDeferredRecordLink deferredLink in _deferredLinks) |
| | | 75 | | { |
| | 4 | 76 | | builder.Append(' '); |
| | 4 | 77 | | builder.Append('['); |
| | 4 | 78 | | builder.Append(deferredLink.Describe()); |
| | 4 | 79 | | builder.Append(']'); |
| | | 80 | | } |
| | | 81 | | |
| | 4 | 82 | | throw new InvalidOperationException(builder.ToString()); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | private interface IDeferredRecordLink |
| | | 86 | | { |
| | | 87 | | bool TryResolve(ChronicleLinkRegistry registry); |
| | | 88 | | |
| | | 89 | | string Describe(); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | private sealed class DeferredRecordLink<T> : IDeferredRecordLink |
| | | 93 | | { |
| | | 94 | | private readonly string _name; |
| | | 95 | | private readonly string _id; |
| | | 96 | | private readonly string? _slot; |
| | | 97 | | private readonly Action<T> _assignLoadedValue; |
| | | 98 | | |
| | 7 | 99 | | public DeferredRecordLink(string name, string id, string? slot, Action<T> assignLoadedValue) |
| | | 100 | | { |
| | 7 | 101 | | _name = name; |
| | 7 | 102 | | _id = id; |
| | 7 | 103 | | _slot = slot; |
| | 7 | 104 | | _assignLoadedValue = assignLoadedValue ?? throw new ArgumentNullException(nameof(assignLoadedValue)); |
| | 6 | 105 | | } |
| | | 106 | | |
| | | 107 | | public bool TryResolve(ChronicleLinkRegistry registry) |
| | | 108 | | { |
| | | 109 | | T? value; |
| | 6 | 110 | | if (!registry.TryResolve(_id, out value, _slot)) |
| | 4 | 111 | | return false; |
| | | 112 | | |
| | 2 | 113 | | _assignLoadedValue(value!); |
| | 2 | 114 | | return true; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | public string Describe() |
| | | 118 | | { |
| | 4 | 119 | | string typeName = typeof(T).Name; |
| | 4 | 120 | | if (string.IsNullOrEmpty(_slot)) |
| | 2 | 121 | | return $"{_name}:{typeName}:{_id}"; |
| | | 122 | | |
| | 2 | 123 | | return $"{_name}:{typeName}:{_id}@{_slot}"; |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | } |