-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModifiableProjectCollection.cs
More file actions
112 lines (91 loc) · 4.64 KB
/
ModifiableProjectCollection.cs
File metadata and controls
112 lines (91 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System.Collections.Generic;
using System.Linq;
using CommunityToolkit.Diagnostics;
using Ipfs;
using OwlCore.Nomad;
using OwlCore.Nomad.Kubo;
using OwlCore.Nomad.Kubo.Events;
namespace WindowsAppCommunity.Sdk.Nomad;
/// <summary>
/// A modifiable handler for roaming project collection data.
/// </summary>
public class ModifiableProjectCollection : NomadKuboEventStreamHandler<ValueUpdateEvent>, IModifiableProjectCollection<IReadOnlyProject>
{
/// <inheritdoc/>
public required string Id { get; init; }
/// <inheritdoc/>
public required ReadOnlyProjectCollection Inner { get; init; }
/// <summary>
/// The repository to use for getting modifiable or readonly project instances.
/// </summary>
public required INomadKuboRepositoryBase<ModifiableProject, IReadOnlyProject> ProjectRepository { get; init; }
/// <inheritdoc/>
public event EventHandler<IReadOnlyProject[]>? ProjectsAdded;
/// <inheritdoc/>
public event EventHandler<IReadOnlyProject[]>? ProjectsRemoved;
/// <inheritdoc/>
public IAsyncEnumerable<IReadOnlyProject> GetProjectsAsync(CancellationToken cancellationToken) => Inner.GetProjectsAsync(cancellationToken);
/// <inheritdoc/>
public async Task AddProjectAsync(IReadOnlyProject project, CancellationToken cancellationToken)
{
var keyCid = await Client.Dag.PutAsync(project.Id, pin: KuboOptions.ShouldPin, cancel: cancellationToken);
var updateEvent = new ValueUpdateEvent(Key: null, Value: (DagCid)keyCid, false);
var appendedEntry = await AppendNewEntryAsync(targetId: Id, eventId: nameof(AddProjectAsync), updateEvent, DateTime.UtcNow, cancellationToken);
await ApplyAddProjectEntryAsync(appendedEntry, updateEvent, project, cancellationToken);
EventStreamPosition = appendedEntry;
}
/// <inheritdoc/>
public async Task RemoveProjectAsync(IReadOnlyProject project, CancellationToken cancellationToken)
{
var keyCid = await Client.Dag.PutAsync(project.Id, pin: KuboOptions.ShouldPin, cancel: cancellationToken);
var updateEvent = new ValueUpdateEvent(Key: null, Value: (DagCid)keyCid, true);
var appendedEntry = await AppendNewEntryAsync(targetId: Id, eventId: nameof(RemoveProjectAsync), updateEvent, DateTime.UtcNow, cancellationToken);
await ApplyRemoveProjectEntryAsync(appendedEntry, updateEvent, project, cancellationToken);
EventStreamPosition = appendedEntry;
}
/// <inheritdoc/>
public override async Task ApplyEntryUpdateAsync(EventStreamEntry<DagCid> streamEntry, ValueUpdateEvent updateEvent, CancellationToken cancellationToken)
{
switch (streamEntry.EventId)
{
case nameof(AddProjectAsync):
{
Guard.IsNotNull(updateEvent.Value);
var projectId = await Client.Dag.GetAsync<Cid>(updateEvent.Value, cancel: cancellationToken);
var project = await ProjectRepository.GetAsync(projectId, cancellationToken);
await ApplyAddProjectEntryAsync(streamEntry, updateEvent, project, cancellationToken);
}
break;
case nameof(RemoveProjectAsync):
{
Guard.IsNotNull(updateEvent.Value);
var projectId = await Client.Dag.GetAsync<Cid>(updateEvent.Value, cancel: cancellationToken);
var project = await ProjectRepository.GetAsync(projectId, cancellationToken);
await ApplyRemoveProjectEntryAsync(streamEntry, updateEvent, project, cancellationToken);
}
break;
default:
throw new InvalidOperationException($"Unknown event id: {streamEntry.EventId}");
}
}
/// <inheritdoc/>
public Task ApplyAddProjectEntryAsync(EventStreamEntry<DagCid> streamEntry, ValueUpdateEvent updateEvent, IReadOnlyProject project, CancellationToken cancellationToken)
{
Inner.Inner.Projects = [.. Inner.Inner.Projects, project.Id];
ProjectsAdded?.Invoke(this, [project]);
return Task.CompletedTask;
}
/// <inheritdoc/>
public Task ApplyRemoveProjectEntryAsync(EventStreamEntry<DagCid> streamEntry, ValueUpdateEvent updateEvent, IReadOnlyProject project, CancellationToken cancellationToken)
{
Inner.Inner.Projects = [.. Inner.Inner.Projects.Where(id => id != project.Id)];
ProjectsRemoved?.Invoke(this, [project]);
return Task.CompletedTask;
}
/// <inheritdoc/>
public override Task ResetEventStreamPositionAsync(CancellationToken cancellationToken)
{
Inner.Inner.Projects = [];
return Task.CompletedTask;
}
}