|
| 1 | +using System.Linq; |
| 2 | +using CommunityToolkit.Diagnostics; |
| 3 | +using Ipfs; |
| 4 | +using OwlCore.ComponentModel; |
| 5 | +using OwlCore.Kubo; |
| 6 | +using OwlCore.Nomad; |
| 7 | +using OwlCore.Nomad.Kubo; |
| 8 | +using OwlCore.Nomad.Kubo.Events; |
| 9 | + |
| 10 | +namespace WindowsAppCommunity.Sdk.Nomad; |
| 11 | + |
| 12 | +/// <inheritdoc cref="IModifiableLinksCollection" /> |
| 13 | +public class ModifiableLinksCollection : NomadKuboEventStreamHandler<ValueUpdateEvent>, IModifiableLinksCollection, IDelegable<ReadOnlyLinksCollection> |
| 14 | +{ |
| 15 | + /// <inheritdoc /> |
| 16 | + public required ReadOnlyLinksCollection Inner { get; init; } |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// A unique identifier for this instance, persistent across machines and reruns. |
| 20 | + /// </summary> |
| 21 | + public required string Id { get; init; } |
| 22 | + |
| 23 | + /// <inheritdoc /> |
| 24 | + public Link[] Links => Inner.Links; |
| 25 | + |
| 26 | + /// <inheritdoc /> |
| 27 | + public async Task AddLinkAsync(Link link, CancellationToken cancellationToken) |
| 28 | + { |
| 29 | + var newLink = new Models.Link |
| 30 | + { |
| 31 | + Id = link.Id, |
| 32 | + Name = link.Name, |
| 33 | + Description = link.Description, |
| 34 | + Url = link.Url, |
| 35 | + }; |
| 36 | + |
| 37 | + var keyCid = await Client.Dag.PutAsync(link.Id, pin: KuboOptions.ShouldPin, cancel: cancellationToken); |
| 38 | + var valueCid = await Client.Dag.PutAsync(newLink, pin: KuboOptions.ShouldPin, cancel: cancellationToken); |
| 39 | + |
| 40 | + var updateEvent = new ValueUpdateEvent((DagCid)keyCid, (DagCid)valueCid, false); |
| 41 | + |
| 42 | + var appendedEntry = await AppendNewEntryAsync(targetId: Id, eventId: nameof(AddLinkAsync), updateEvent, DateTime.UtcNow, cancellationToken); |
| 43 | + await ApplyEntryUpdateAsync(appendedEntry, updateEvent, newLink, link, cancellationToken); |
| 44 | + |
| 45 | + EventStreamPosition = appendedEntry; |
| 46 | + } |
| 47 | + |
| 48 | + /// <inheritdoc /> |
| 49 | + public async Task RemoveLinkAsync(Link link, CancellationToken cancellationToken) |
| 50 | + { |
| 51 | + var Link = Inner.Inner.Links.FirstOrDefault(img => img.Id == link.Id); |
| 52 | + if (Link == null) |
| 53 | + { |
| 54 | + throw new ArgumentException("Link not found in the collection.", nameof(link)); |
| 55 | + } |
| 56 | + |
| 57 | + var keyCid = await Client.Dag.PutAsync(Link.Id, pin: KuboOptions.ShouldPin, cancel: cancellationToken); |
| 58 | + var valueCid = await Client.Dag.PutAsync(Link, pin: KuboOptions.ShouldPin, cancel: cancellationToken); |
| 59 | + |
| 60 | + var updateEvent = new ValueUpdateEvent((DagCid)keyCid, (DagCid)valueCid, true); |
| 61 | + |
| 62 | + var appendedEntry = await AppendNewEntryAsync(Id, nameof(RemoveLinkAsync), updateEvent, DateTime.UtcNow, cancellationToken); |
| 63 | + await ApplyEntryUpdateAsync(appendedEntry, updateEvent, Link, link, cancellationToken); |
| 64 | + |
| 65 | + EventStreamPosition = appendedEntry; |
| 66 | + } |
| 67 | + |
| 68 | + /// <inheritdoc /> |
| 69 | + public event EventHandler<Link[]>? LinksAdded; |
| 70 | + |
| 71 | + /// <inheritdoc /> |
| 72 | + public event EventHandler<Link[]>? LinksRemoved; |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Applies an event stream update event and raises the relevant events. |
| 76 | + /// </summary> |
| 77 | + /// <remarks> |
| 78 | + /// This method will call <see cref="ReadOnlyLinksCollection.GetAsync(string, CancellationToken)"/> and create a new instance to pass to the event handlers. |
| 79 | + /// <para/> |
| 80 | + /// If already have a resolved instance of <see cref="Link"/>, you should call <see cref="ApplyEntryUpdateAsync(EventStreamEntry{DagCid}, ValueUpdateEvent, Models.Link, Link, CancellationToken)"/> instead. |
| 81 | + /// </remarks> |
| 82 | + /// <param name="eventStreamEntry">The event stream entry to apply.</param> |
| 83 | + /// <param name="updateEvent">The update event to apply.</param> |
| 84 | + /// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param> |
| 85 | + public override async Task ApplyEntryUpdateAsync(EventStreamEntry<DagCid> eventStreamEntry, ValueUpdateEvent updateEvent, CancellationToken cancellationToken) |
| 86 | + { |
| 87 | + cancellationToken.ThrowIfCancellationRequested(); |
| 88 | + |
| 89 | + if (eventStreamEntry.TargetId != Id) |
| 90 | + return; |
| 91 | + |
| 92 | + Guard.IsNotNull(updateEvent.Value); |
| 93 | + var (link, _) = await Client.ResolveDagCidAsync<Models.Link>(updateEvent.Value.Value, nocache: !KuboOptions.UseCache, cancellationToken); |
| 94 | + |
| 95 | + Guard.IsNotNull(link); |
| 96 | + await ApplyEntryUpdateAsync(eventStreamEntry, updateEvent, link, null, cancellationToken); |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Applies an event stream update event and raises the relevant events. |
| 101 | + /// </summary> |
| 102 | + /// <param name="eventStreamEntry">The event stream entry to apply.</param> |
| 103 | + /// <param name="updateEvent">The update event to apply.</param> |
| 104 | + /// <param name="linkModel">The deserialized Link model data for this event.</param> |
| 105 | + /// <param name="linkAppModel">The runtime application model for this event.</param> |
| 106 | + /// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param> |
| 107 | + public async Task ApplyEntryUpdateAsync(EventStreamEntry<DagCid> eventStreamEntry, ValueUpdateEvent updateEvent, Models.Link linkModel, Link? linkAppModel = null, CancellationToken cancellationToken = default) |
| 108 | + { |
| 109 | + cancellationToken.ThrowIfCancellationRequested(); |
| 110 | + |
| 111 | + switch (eventStreamEntry.EventId) |
| 112 | + { |
| 113 | + case nameof(AddLinkAsync): |
| 114 | + { |
| 115 | + Inner.Inner.Links = [.. Inner.Inner.Links, linkModel]; |
| 116 | + linkAppModel ??= await Inner.GetAsync(linkModel.Id, cancellationToken); |
| 117 | + LinksAdded?.Invoke(this, [linkAppModel]); |
| 118 | + break; |
| 119 | + } |
| 120 | + case nameof(RemoveLinkAsync): |
| 121 | + { |
| 122 | + Inner.Inner.Links = [.. Inner.Inner.Links.Except([linkModel])]; |
| 123 | + linkAppModel ??= await Inner.GetAsync(linkModel.Id, cancellationToken); |
| 124 | + LinksRemoved?.Invoke(this, [linkAppModel]); |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /// <inheritdoc /> |
| 131 | + public override Task ResetEventStreamPositionAsync(CancellationToken cancellationToken) |
| 132 | + { |
| 133 | + EventStreamPosition = null; |
| 134 | + Inner.Inner.Links = []; |
| 135 | + |
| 136 | + return Task.CompletedTask; |
| 137 | + } |
| 138 | +} |
0 commit comments