| | | 1 | | using SwiftCollections; |
| | | 2 | | using System; |
| | | 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 | | { |
| | 101 | 12 | | private readonly SwiftList<IDeferredRecordLink> _deferredLinks = new(); |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Creates a new context with an empty link registry. |
| | | 16 | | /// </summary> |
| | 101 | 17 | | public ChronicleContext() |
| | 101 | 18 | | { |
| | 101 | 19 | | Links = new ChronicleLinkRegistry(); |
| | 101 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the registry used to save and load stable links to external or runtime-owned objects. |
| | | 24 | | /// </summary> |
| | 74 | 25 | | public ChronicleLinkRegistry Links { get; } |
| | | 26 | | |
| | | 27 | | internal void QueueDeferredLink<T>(string name, string id, string? slot, Action<T> assignLoadedValue) |
| | 6 | 28 | | { |
| | 6 | 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() |
| | 40 | 36 | | { |
| | 40 | 37 | | if (_deferredLinks.Count == 0) |
| | 34 | 38 | | return; |
| | | 39 | | |
| | 8 | 40 | | while (_deferredLinks.Count > 0) |
| | 6 | 41 | | { |
| | 6 | 42 | | int resolvedCount = 0; |
| | 24 | 43 | | for (int i = _deferredLinks.Count - 1; i >= 0; i--) |
| | 6 | 44 | | { |
| | 6 | 45 | | if (!_deferredLinks[i].TryResolve(Links)) |
| | 4 | 46 | | continue; |
| | | 47 | | |
| | 2 | 48 | | _deferredLinks.RemoveAt(i); |
| | 2 | 49 | | resolvedCount++; |
| | 2 | 50 | | } |
| | | 51 | | |
| | 6 | 52 | | if (resolvedCount == 0) |
| | 4 | 53 | | break; |
| | 2 | 54 | | } |
| | | 55 | | |
| | 6 | 56 | | if (_deferredLinks.Count == 0) |
| | 2 | 57 | | return; |
| | | 58 | | |
| | 4 | 59 | | var builder = new StringBuilder(); |
| | 4 | 60 | | builder.Append("Unable to resolve deferred links:"); |
| | 20 | 61 | | foreach (IDeferredRecordLink deferredLink in _deferredLinks) |
| | 4 | 62 | | { |
| | 4 | 63 | | builder.Append(' '); |
| | 4 | 64 | | builder.Append('['); |
| | 4 | 65 | | builder.Append(deferredLink.Describe()); |
| | 4 | 66 | | builder.Append(']'); |
| | 4 | 67 | | } |
| | | 68 | | |
| | 4 | 69 | | throw new InvalidOperationException(builder.ToString()); |
| | 36 | 70 | | } |
| | | 71 | | |
| | | 72 | | private interface IDeferredRecordLink |
| | | 73 | | { |
| | | 74 | | bool TryResolve(ChronicleLinkRegistry registry); |
| | | 75 | | |
| | | 76 | | string Describe(); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | private sealed class DeferredRecordLink<T> : IDeferredRecordLink |
| | | 80 | | { |
| | | 81 | | private readonly string _name; |
| | | 82 | | private readonly string _id; |
| | | 83 | | private readonly string? _slot; |
| | | 84 | | private readonly Action<T> _assignLoadedValue; |
| | | 85 | | |
| | 6 | 86 | | public DeferredRecordLink(string name, string id, string? slot, Action<T> assignLoadedValue) |
| | 6 | 87 | | { |
| | 6 | 88 | | _name = name; |
| | 6 | 89 | | _id = id; |
| | 6 | 90 | | _slot = slot; |
| | 6 | 91 | | _assignLoadedValue = assignLoadedValue ?? throw new ArgumentNullException(nameof(assignLoadedValue)); |
| | 6 | 92 | | } |
| | | 93 | | |
| | | 94 | | public bool TryResolve(ChronicleLinkRegistry registry) |
| | 6 | 95 | | { |
| | | 96 | | T? value; |
| | 6 | 97 | | if (!registry.TryResolve(_id, out value, _slot)) |
| | 4 | 98 | | return false; |
| | | 99 | | |
| | 2 | 100 | | _assignLoadedValue(value!); |
| | 2 | 101 | | return true; |
| | 6 | 102 | | } |
| | | 103 | | |
| | | 104 | | public string Describe() |
| | 4 | 105 | | { |
| | 4 | 106 | | string typeName = typeof(T).Name; |
| | 4 | 107 | | if (string.IsNullOrEmpty(_slot)) |
| | 2 | 108 | | return $"{_name}:{typeName}:{_id}"; |
| | | 109 | | |
| | 2 | 110 | | return $"{_name}:{typeName}:{_id}@{_slot}"; |
| | 4 | 111 | | } |
| | | 112 | | } |
| | | 113 | | } |