Skip to content

Commit 16eee01

Browse files
committed
Refactor, upgrade to latest version of OwlCore.Nomad.Kubo
1 parent f2d6873 commit 16eee01

File tree

4 files changed

+27
-120
lines changed

4 files changed

+27
-120
lines changed

src/Models/ValueUpdateEvent.cs

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/Nomad/ModifiableAccentColor.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using OwlCore.Kubo;
44
using OwlCore.Nomad;
55
using OwlCore.Nomad.Kubo;
6-
using WindowsAppCommunity.Sdk.Models;
6+
using OwlCore.Nomad.Kubo.Events;
77

88
namespace WindowsAppCommunity.Sdk.Nomad;
99

@@ -25,11 +25,11 @@ public class ModifiableAccentColor : NomadKuboEventStreamHandler<ValueUpdateEven
2525
public event EventHandler<string?>? AccentColorUpdated;
2626

2727
/// <inheritdoc />
28-
public override async Task ApplyEntryUpdateAsync(ValueUpdateEvent updateEvent, CancellationToken cancellationToken)
28+
public override async Task ApplyEntryUpdateAsync(EventStreamEntry<DagCid> eventStreamEntry, ValueUpdateEvent updateEvent, CancellationToken cancellationToken)
2929
{
3030
cancellationToken.ThrowIfCancellationRequested();
3131

32-
if (updateEvent.TargetId != Id)
32+
if (eventStreamEntry.TargetId != Id)
3333
return;
3434

3535
string? accentColor = null;
@@ -39,20 +39,21 @@ public override async Task ApplyEntryUpdateAsync(ValueUpdateEvent updateEvent, C
3939
(accentColor, _) = await Client.ResolveDagCidAsync<string>(updateEvent.Value, nocache: !KuboOptions.UseCache, cancellationToken);
4040
}
4141

42-
await ApplyEntryUpdateAsync(updateEvent, accentColor, cancellationToken);
42+
await ApplyEntryUpdateAsync(eventStreamEntry, updateEvent, accentColor, cancellationToken);
4343
}
4444

4545
/// <summary>
4646
/// Applies an event stream update event and raises the relevant events.
4747
/// </summary>
48+
/// <param name="eventStreamEntry">The event stream entry to apply.</param>
4849
/// <param name="updateEvent">The update event to apply.</param>
4950
/// <param name="accentColor">The resolved accent color data for this event.</param>
5051
/// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param>
51-
public Task ApplyEntryUpdateAsync(ValueUpdateEvent updateEvent, string? accentColor, CancellationToken cancellationToken)
52+
public Task ApplyEntryUpdateAsync(EventStreamEntry<DagCid> eventStreamEntry, ValueUpdateEvent updateEvent, string? accentColor, CancellationToken cancellationToken)
5253
{
5354
cancellationToken.ThrowIfCancellationRequested();
5455

55-
if (updateEvent.EventId is not nameof(UpdateAccentColorAsync))
56+
if (eventStreamEntry.EventId is not nameof(UpdateAccentColorAsync))
5657
return Task.CompletedTask;
5758

5859
Inner.Inner.AccentColor = accentColor;
@@ -61,14 +62,6 @@ public Task ApplyEntryUpdateAsync(ValueUpdateEvent updateEvent, string? accentCo
6162
return Task.CompletedTask;
6263
}
6364

64-
/// <inheritdoc cref="INomadKuboEventStreamHandler{TEventEntryContent}.AppendNewEntryAsync" />
65-
public override async Task<EventStreamEntry<Cid>> AppendNewEntryAsync(ValueUpdateEvent updateEvent, CancellationToken cancellationToken = default)
66-
{
67-
var localUpdateEventCid = await Client.Dag.PutAsync(updateEvent, pin: KuboOptions.ShouldPin, cancel: cancellationToken);
68-
var newEntry = await this.AppendEventStreamEntryAsync(localUpdateEventCid, updateEvent.EventId, updateEvent.TargetId, cancellationToken);
69-
return newEntry;
70-
}
71-
7265
/// <inheritdoc />
7366
public override Task ResetEventStreamPositionAsync(CancellationToken cancellationToken)
7467
{
@@ -88,10 +81,10 @@ public async Task UpdateAccentColorAsync(string? accentColor, CancellationToken
8881
valueCid = (DagCid)cid;
8982
}
9083

91-
var updateEvent = new ValueUpdateEvent(Id, nameof(UpdateAccentColorAsync), null, valueCid, accentColor is null);
84+
var updateEvent = new ValueUpdateEvent(null, valueCid, accentColor is null);
9285

93-
await ApplyEntryUpdateAsync(updateEvent, accentColor, cancellationToken);
94-
var appendedEntry = await AppendNewEntryAsync(updateEvent, cancellationToken);
86+
var appendedEntry = await AppendNewEntryAsync(targetId: Id, eventId: nameof(UpdateAccentColorAsync), updateEvent, DateTime.UtcNow, cancellationToken);
87+
await ApplyEntryUpdateAsync(appendedEntry, updateEvent, accentColor, cancellationToken);
9588

9689
EventStreamPosition = appendedEntry;
9790
}

src/Nomad/ModifiableImagesCollection.cs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using OwlCore.Kubo;
88
using OwlCore.Nomad;
99
using OwlCore.Nomad.Kubo;
10+
using OwlCore.Nomad.Kubo.Events;
1011
using OwlCore.Storage;
1112
using WindowsAppCommunity.Sdk.Models;
1213

@@ -38,10 +39,10 @@ public async Task AddImageAsync(IFile imageFile, CancellationToken cancellationT
3839
var keyCid = await Client.Dag.PutAsync(newImage.Id, pin: KuboOptions.ShouldPin, cancel: cancellationToken);
3940
var valueCid = await Client.Dag.PutAsync(newImage, pin: KuboOptions.ShouldPin, cancel: cancellationToken);
4041

41-
var updateEvent = new ValueUpdateEvent(Id, nameof(AddImageAsync), (DagCid)keyCid, (DagCid)valueCid, false);
42+
var updateEvent = new ValueUpdateEvent((DagCid)keyCid, (DagCid)valueCid, false);
4243

43-
await ApplyEntryUpdateAsync(updateEvent, newImage, cancellationToken);
44-
var appendedEntry = await AppendNewEntryAsync(updateEvent, cancellationToken);
44+
var appendedEntry = await AppendNewEntryAsync(targetId: Id, eventId: nameof(AddImageAsync), updateEvent, DateTime.UtcNow, cancellationToken);
45+
await ApplyEntryUpdateAsync(appendedEntry, updateEvent, newImage, cancellationToken);
4546

4647
EventStreamPosition = appendedEntry;
4748
}
@@ -58,10 +59,10 @@ public async Task RemoveImageAsync(IFile imageFile, CancellationToken cancellati
5859
var keyCid = await Client.Dag.PutAsync(image.Id, pin: KuboOptions.ShouldPin, cancel: cancellationToken);
5960
var valueCid = await Client.Dag.PutAsync(image, pin: KuboOptions.ShouldPin, cancel: cancellationToken);
6061

61-
var updateEvent = new ValueUpdateEvent(Id, nameof(RemoveImageAsync), (DagCid)keyCid, (DagCid)valueCid, true);
62+
var updateEvent = new ValueUpdateEvent((DagCid)keyCid, (DagCid)valueCid, true);
6263

63-
await ApplyEntryUpdateAsync(updateEvent, image, cancellationToken);
64-
var appendedEntry = await AppendNewEntryAsync(updateEvent, cancellationToken);
64+
var appendedEntry = await AppendNewEntryAsync(Id, nameof(RemoveImageAsync), updateEvent, DateTime.UtcNow, cancellationToken);
65+
await ApplyEntryUpdateAsync(appendedEntry, updateEvent, image, cancellationToken);
6566

6667
EventStreamPosition = appendedEntry;
6768
}
@@ -81,35 +82,37 @@ public async Task RemoveImageAsync(IFile imageFile, CancellationToken cancellati
8182
/// <remarks>
8283
/// This method will call <see cref="ReadOnlyImagesCollection.GetAsync(string, CancellationToken)"/> and create a new instance to pass to the event handlers.
8384
/// <para/>
84-
/// If already have a resolved instance of <see cref="Image"/>, you should call <see cref="ApplyEntryUpdateAsync(ValueUpdateEvent, Image, CancellationToken)"/> instead.
85+
/// If already have a resolved instance of <see cref="Image"/>, you should call <see cref="ApplyEntryUpdateAsync(EventStreamEntry{DagCid}, ValueUpdateEvent, Image, CancellationToken)"/> instead.
8586
/// </remarks>
87+
/// <param name="eventStreamEntry">The event stream entry to apply.</param>
8688
/// <param name="updateEvent">The update event to apply.</param>
8789
/// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param>
88-
public override async Task ApplyEntryUpdateAsync(ValueUpdateEvent updateEvent, CancellationToken cancellationToken)
90+
public override async Task ApplyEntryUpdateAsync(EventStreamEntry<DagCid> eventStreamEntry, ValueUpdateEvent updateEvent, CancellationToken cancellationToken)
8991
{
9092
cancellationToken.ThrowIfCancellationRequested();
9193

92-
if (updateEvent.TargetId != Id)
94+
if (eventStreamEntry.TargetId != Id)
9395
return;
9496

9597
Guard.IsNotNull(updateEvent.Value);
9698
var (image, _) = await Client.ResolveDagCidAsync<Image>(updateEvent.Value.Value, nocache: !KuboOptions.UseCache, cancellationToken);
9799

98100
Guard.IsNotNull(image);
99-
await ApplyEntryUpdateAsync(updateEvent, image, cancellationToken);
101+
await ApplyEntryUpdateAsync(eventStreamEntry, updateEvent, image, cancellationToken);
100102
}
101103

102104
/// <summary>
103105
/// Applies an event stream update event and raises the relevant events.
104106
/// </summary>
107+
/// <param name="eventStreamEntry">The event stream entry to apply.</param>
105108
/// <param name="updateEvent">The update event to apply.</param>
106109
/// <param name="image">The resolved image data for this event.</param>
107110
/// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param>
108-
public async Task ApplyEntryUpdateAsync(ValueUpdateEvent updateEvent, Image image, CancellationToken cancellationToken)
111+
public async Task ApplyEntryUpdateAsync(EventStreamEntry<DagCid> eventStreamEntry, ValueUpdateEvent updateEvent, Image image, CancellationToken cancellationToken)
109112
{
110113
cancellationToken.ThrowIfCancellationRequested();
111114

112-
switch (updateEvent.EventId)
115+
switch (eventStreamEntry.EventId)
113116
{
114117
case nameof(AddImageAsync):
115118
{
@@ -128,15 +131,6 @@ public async Task ApplyEntryUpdateAsync(ValueUpdateEvent updateEvent, Image imag
128131
}
129132
}
130133

131-
/// <inheritdoc cref="INomadKuboEventStreamHandler{TEventEntryContent}.AppendNewEntryAsync" />
132-
public override async Task<EventStreamEntry<Cid>> AppendNewEntryAsync(ValueUpdateEvent updateEvent, CancellationToken cancellationToken = default)
133-
{
134-
// Use extension method for code deduplication (can't use inheritance).
135-
var localUpdateEventCid = await Client.Dag.PutAsync(updateEvent, pin: KuboOptions.ShouldPin, cancel: cancellationToken);
136-
var newEntry = await this.AppendEventStreamEntryAsync(localUpdateEventCid, updateEvent.EventId, updateEvent.TargetId, cancellationToken);
137-
return newEntry;
138-
}
139-
140134
/// <inheritdoc />
141135
public override Task ResetEventStreamPositionAsync(CancellationToken cancellationToken)
142136
{

src/WindowsAppCommunity.Sdk.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ Initial release of WindowsAppCommunity.Sdk.
5555
</ItemGroup>
5656

5757
<ItemGroup>
58-
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.0" />
58+
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.2" />
5959
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
6060
<PackageReference Include="OwlCore.Kubo" Version="0.20.1" />
61-
<PackageReference Include="OwlCore.Nomad" Version="0.9.0" />
62-
<PackageReference Include="OwlCore.Nomad.Kubo" Version="0.14.0" />
61+
<PackageReference Include="OwlCore.Nomad.Kubo" Version="0.15.1" />
6362
<PackageReference Include="PolySharp" Version="1.15.0">
6463
<PrivateAssets>all</PrivateAssets>
6564
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

0 commit comments

Comments
 (0)