Skip to content

Commit ad9e8ce

Browse files
committed
Init
1 parent d7cbd95 commit ad9e8ce

10 files changed

Lines changed: 293 additions & 6 deletions

src/Nomad/ModifiableConnections.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using OwlCore.Nomad.Kubo;
2+
using OwlCore.Nomad.Kubo.Events;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using WindowsAppCommunity.Sdk.Models;
8+
9+
namespace WindowsAppCommunity.Sdk.Nomad
10+
{
11+
public class ModifiableConnections : NomadKuboEventStreamHandler<ValueUpdateEvent>, IModifiableConnection
12+
{
13+
private readonly Dictionary<string, IModifiableConnection> _connections;
14+
15+
public ModifiableConnections()
16+
{
17+
_connections = new Dictionary<string, IModifiableConnection>();
18+
}
19+
20+
public Task AddConnectionAsync(IModifiableConnection connection, CancellationToken cancellationToken = default)
21+
{
22+
if (connection == null)
23+
throw new ArgumentNullException(nameof(connection));
24+
25+
_connections[connection.Id] = connection;
26+
return Task.CompletedTask;
27+
}
28+
29+
public Task RemoveConnectionAsync(IModifiableConnection connection, CancellationToken cancellationToken = default)
30+
{
31+
if (connection == null)
32+
throw new ArgumentNullException(nameof(connection));
33+
34+
_connections.Remove(connection.Id);
35+
return Task.CompletedTask;
36+
}
37+
38+
public Task<IReadOnlyDictionary<string, IModifiableConnection>> GetConnectionsAsync(CancellationToken cancellationToken = default)
39+
{
40+
return Task.FromResult((IReadOnlyDictionary<string, IModifiableConnection>)_connections);
41+
}
42+
43+
public string Id { get; private set; }
44+
public string Value { get; private set; }
45+
public event EventHandler<string>? ValueUpdated;
46+
47+
public Task UpdateValueAsync(string newValue, CancellationToken cancellationToken = default)
48+
{
49+
Value = newValue;
50+
ValueUpdated?.Invoke(this, newValue);
51+
return Task.CompletedTask;
52+
}
53+
}
54+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}

src/Nomad/ModifiableProject.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,18 @@ public static ModifiableProject FromHandlerConfig(NomadKuboEventStreamHandlerCon
3939
ReadOnlyEntity readOnlyEntity = readOnlyProject.InnerEntity;
4040

4141
// Modifiable virtual event stream handlers
42-
IModifiableConnectionsCollection modifiableConnectionsCollection = null!;
42+
IModifiableConnectionsCollection modifiableConnectionsCollection = new ModifiableConnectionsCollection
43+
{
44+
Id = handlerConfig.RoamingKey.Id,
45+
Inner = readOnlyEntity.InnerConnections,
46+
RoamingKey = handlerConfig.RoamingKey,
47+
EventStreamHandlerId = handlerConfig.RoamingKey.Id,
48+
LocalEventStream = handlerConfig.LocalValue,
49+
LocalEventStreamKey = handlerConfig.LocalKey,
50+
Sources = handlerConfig.RoamingValue.Sources,
51+
KuboOptions = kuboOptions,
52+
Client = client,
53+
};
4354
IModifiableLinksCollection modifiableLinksCollection = null!;
4455
ModifiableImagesCollection modifiableImagesCollection = new()
4556
{

src/Nomad/ModifiablePublisher.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,19 @@ public static ModifiablePublisher FromHandlerConfig(NomadKuboEventStreamHandlerC
4949
Sources = handlerConfig.RoamingValue.Sources,
5050
};
5151

52-
IModifiableConnectionsCollection modifiableConnectionsCollection = null!;
52+
IModifiableConnectionsCollection modifiableConnectionsCollection = new ModifiableConnectionsCollection
53+
{
54+
Id = handlerConfig.RoamingKey.Id,
55+
Inner = readonlyPublisher.InnerEntity.InnerConnections,
56+
RoamingKey = handlerConfig.RoamingKey,
57+
EventStreamHandlerId = handlerConfig.RoamingKey.Id,
58+
LocalEventStream = handlerConfig.LocalValue,
59+
LocalEventStreamKey = handlerConfig.LocalKey,
60+
Sources = handlerConfig.RoamingValue.Sources,
61+
KuboOptions = kuboOptions,
62+
Client = client,
63+
};
64+
5365
IModifiableLinksCollection modifiableLinksCollection = null!;
5466

5567
ModifiableEntity modifiableEntity = new()

src/Nomad/ModifiableUser.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,18 @@ public static ModifiableUser FromHandlerConfig(NomadKuboEventStreamHandlerConfig
3737
var readOnlyEntity = readOnlyUser.InnerEntity;
3838

3939
// Modifiable virtual event stream handlers
40-
IModifiableConnectionsCollection modifiableConnectionsCollection = null!;
40+
IModifiableConnectionsCollection modifiableConnectionsCollection = new ModifiableConnectionsCollection
41+
{
42+
Id = handlerConfig.RoamingKey.Id,
43+
Inner = readOnlyEntity.InnerConnections,
44+
RoamingKey = handlerConfig.RoamingKey,
45+
EventStreamHandlerId = handlerConfig.RoamingKey.Id,
46+
LocalEventStream = handlerConfig.LocalValue,
47+
LocalEventStreamKey = handlerConfig.LocalKey,
48+
Sources = handlerConfig.RoamingValue.Sources,
49+
KuboOptions = kuboOptions,
50+
Client = client,
51+
};
4152
IModifiableLinksCollection modifiableLinksCollection = null!;
4253
ModifiableImagesCollection modifiableImagesCollection = new()
4354
{

src/Nomad/ReadOnlyConnection.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace WindowsAppCommunity.Sdk.Nomad;
2+
3+
/// <summary>
4+
/// Represents a read-only connection.
5+
/// </summary>
6+
public class ReadOnlyConnection : IReadOnlyConnection
7+
{
8+
/// <inheritdoc />
9+
public required string Id { get; init; }
10+
11+
/// <inheritdoc />
12+
public required string Value { get; init; }
13+
14+
/// <inheritdoc />
15+
public event EventHandler<string>? ValueUpdated;
16+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Ipfs;
6+
using Ipfs.CoreApi;
7+
using OwlCore.ComponentModel;
8+
using WindowsAppCommunity.Sdk.Models;
9+
10+
namespace WindowsAppCommunity.Sdk.Nomad;
11+
12+
/// <summary>
13+
/// Represents a read-only collection of connections.
14+
/// </summary>
15+
public class ReadOnlyConnectionsCollection : IReadOnlyConnectionsCollection
16+
{
17+
/// <summary>
18+
/// The client to use for communicating with ipfs.
19+
/// </summary>
20+
public required ICoreApi Client { get; init; }
21+
22+
/// <summary>
23+
/// The connections associated with this collection.
24+
/// </summary>
25+
public required IConnections Inner { get; init; }
26+
27+
/// <inheritdoc />
28+
public IReadOnlyConnection[] Connections => GetConnections();
29+
30+
/// <inheritdoc />
31+
public event EventHandler<IReadOnlyConnection[]>? ConnectionsAdded;
32+
33+
/// <inheritdoc />
34+
public event EventHandler<IReadOnlyConnection[]>? ConnectionsRemoved;
35+
36+
private IReadOnlyConnection[] GetConnections()
37+
{
38+
var connections = new List<IReadOnlyConnection>();
39+
40+
foreach (var kvp in Inner.Connections)
41+
{
42+
connections.Add(new ReadOnlyConnection
43+
{
44+
Id = kvp.Key,
45+
Value = kvp.Value.ToString()
46+
});
47+
}
48+
49+
return connections.ToArray();
50+
}
51+
52+
/// <summary>
53+
/// Handles the addition of connections.
54+
/// </summary>
55+
/// <param name="connections">The connections to add.</param>
56+
public void OnConnectionsAdded(IReadOnlyConnection[] connections)
57+
{
58+
ConnectionsAdded?.Invoke(this, connections);
59+
}
60+
61+
/// <summary>
62+
/// Handles the removal of connections.
63+
/// </summary>
64+
/// <param name="connections">The connections to remove.</param>
65+
public void OnConnectionsRemoved(IReadOnlyConnection[] connections)
66+
{
67+
ConnectionsRemoved?.Invoke(this, connections);
68+
}
69+
}

src/Nomad/ReadOnlyProject.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ public static ReadOnlyProject FromHandlerConfig(NomadKuboEventStreamHandlerConfi
3636
Client = client,
3737
};
3838

39-
IReadOnlyConnectionsCollection readOnlyConnectionsCollection = null!;
39+
IReadOnlyConnectionsCollection readOnlyConnectionsCollection = new ReadOnlyConnectionsCollection
40+
{
41+
Inner = handlerConfig.RoamingValue,
42+
Client = client,
43+
};
44+
4045
IReadOnlyLinksCollection readOnlyLinksCollection = null!;
4146

4247
ReadOnlyEntity readOnlyEntity = new ReadOnlyEntity

src/Nomad/ReadOnlyPublisher.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ public static ReadOnlyPublisher FromHandlerConfig(NomadKuboEventStreamHandlerCon
3535
Client = client,
3636
};
3737

38-
IReadOnlyConnectionsCollection readOnlyConnectionsCollection = null!;
38+
IReadOnlyConnectionsCollection readOnlyConnectionsCollection = new ReadOnlyConnectionsCollection
39+
{
40+
Inner = handlerConfig.RoamingValue,
41+
Client = client,
42+
};
43+
3944
IReadOnlyLinksCollection readOnlyLinksCollection = null!;
4045

4146
ReadOnlyEntity readOnlyEntity = new ReadOnlyEntity

src/Nomad/ReadOnlyUser.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ public static ReadOnlyUser FromHandlerConfig(NomadKuboEventStreamHandlerConfig<U
3333
Client = client,
3434
};
3535

36-
IReadOnlyConnectionsCollection readOnlyConnectionsCollection = null!;
36+
IReadOnlyConnectionsCollection readOnlyConnectionsCollection = new ReadOnlyConnectionsCollection
37+
{
38+
Inner = handlerConfig.RoamingValue,
39+
Client = client,
40+
};
41+
3742
IReadOnlyLinksCollection readOnlyLinksCollection = null!;
3843

3944
ReadOnlyEntity readOnlyEntity = new ReadOnlyEntity

0 commit comments

Comments
 (0)