|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using WindowsAppCommunity.Sdk; |
| 5 | +using Ipfs; |
| 6 | +using Ipfs.CoreApi; |
| 7 | +using OwlCore.Nomad; |
| 8 | +using OwlCore.Nomad.Kubo; |
| 9 | + |
| 10 | +namespace WindowsAppCommunity.Sdk.Nomad; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Represents a modifiable connections collection. |
| 14 | +/// </summary> |
| 15 | +public class ModifiableConnectionsCollection : IModifiableConnectionsCollection |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// The connections associated with this entity. |
| 19 | + /// </summary> |
| 20 | + public IReadOnlyConnection[] Connections { get; private set; } = new IReadOnlyConnection[0]; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Raised when items are added to the <see cref="Connections"/> collection. |
| 24 | + /// </summary> |
| 25 | + public event EventHandler<IReadOnlyConnection[]>? ConnectionsAdded; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Raised when items are removed from the <see cref="Connections"/> collection. |
| 29 | + /// </summary> |
| 30 | + public event EventHandler<IReadOnlyConnection[]>? ConnectionsRemoved; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// The unique identifier for this collection. |
| 34 | + /// </summary> |
| 35 | + public required string Id { get; init; } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// The inner read-only connections collection. |
| 39 | + /// </summary> |
| 40 | + public required IReadOnlyConnectionsCollection Inner { get; init; } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// The roaming key for this collection. |
| 44 | + /// </summary> |
| 45 | + public required IKey RoamingKey { get; init; } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// The event stream handler ID for this collection. |
| 49 | + /// </summary> |
| 50 | + public required string EventStreamHandlerId { get; init; } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// The local event stream for this collection. |
| 54 | + /// </summary> |
| 55 | + public required EventStream<DagCid> LocalEventStream { get; init; } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// The local event stream key for this collection. |
| 59 | + /// </summary> |
| 60 | + public required IKey LocalEventStreamKey { get; init; } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// The sources for this collection. |
| 64 | + /// </summary> |
| 65 | + public required ICollection<Cid> Sources { get; init; } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// The Kubo options for this collection. |
| 69 | + /// </summary> |
| 70 | + public required IKuboOptions KuboOptions { get; init; } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// The IPFS client for this collection. |
| 74 | + /// </summary> |
| 75 | + public required ICoreApi Client { get; init; } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Adds a connection to this entity. |
| 79 | + /// </summary> |
| 80 | + public async Task AddConnectionAsync(IReadOnlyConnection connection, CancellationToken cancellationToken) |
| 81 | + { |
| 82 | + var connectionsList = new List<IReadOnlyConnection>(Connections) { connection }; |
| 83 | + Connections = connectionsList.ToArray(); |
| 84 | + ConnectionsAdded?.Invoke(this, new[] { connection }); |
| 85 | + await Task.CompletedTask; |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Removes a connection from this entity. |
| 90 | + /// </summary> |
| 91 | + public async Task RemoveConnectionAsync(IReadOnlyConnection connection, CancellationToken cancellationToken) |
| 92 | + { |
| 93 | + var connectionsList = new List<IReadOnlyConnection>(Connections); |
| 94 | + connectionsList.Remove(connection); |
| 95 | + Connections = connectionsList.ToArray(); |
| 96 | + ConnectionsRemoved?.Invoke(this, new[] { connection }); |
| 97 | + await Task.CompletedTask; |
| 98 | + } |
| 99 | +} |
0 commit comments