Skip to content

Commit

Permalink
IEvent.Apply need not be async
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Dec 13, 2023
1 parent 9220312 commit b7e8af4
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/NexusMods.EventSourcing.Abstractions/IEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public interface IEvent
/// <summary>
/// Applies the event to the entities attached to the event.
/// </summary>
ValueTask Apply<T>(T context) where T : IEventContext;
void Apply<T>(T context) where T : IEventContext;
}
4 changes: 1 addition & 3 deletions src/NexusMods.EventSourcing/Events/TransactionEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ public class TransactionEvent : IEvent
/// <param name="context"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public ValueTask Apply<T>(T context) where T : IEventContext
public void Apply<T>(T context) where T : IEventContext
{
foreach (var evt in Events)
evt.Event.Apply(context);

return ValueTask.CompletedTask;
}
}
2 changes: 1 addition & 1 deletion tests/NexusMods.EventSourcing.TestModel/Events/AddMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace NexusMods.EventSourcing.TestModel.Events;
[MemoryPackable]
public partial record AddMod(string Name, bool Enabled, EntityId<Mod> ModId, EntityId<Loadout> LoadoutId) : IEvent
{
public async ValueTask Apply<T>(T context) where T : IEventContext
public void Apply<T>(T context) where T : IEventContext
{
context.New(ModId);
context.Emit(ModId, Mod._name, Name);
Expand Down
17 changes: 3 additions & 14 deletions tests/NexusMods.EventSourcing.TestModel/Events/CreateLoadout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,13 @@ namespace NexusMods.EventSourcing.TestModel.Events;

[EventId("63A4CB90-27E2-468A-BE94-CB01A38D8C09")]
[MemoryPackable]
public partial class CreateLoadout : IEvent
public partial record CreateLoadout(EntityId<Loadout> Id, string Name) : IEvent
{
public required string Name { get; init; }

public required EntityId<Loadout> Id { get; init; }


public async ValueTask Apply<T>(T context) where T : IEventContext
public void Apply<T>(T context) where T : IEventContext
{
context.New(Id);
context.Emit(Id, Loadout._name, Name);
context.Emit(LoadoutRegistry.SingletonId, LoadoutRegistry._loadouts, Id);
}

public static CreateLoadout Create(string name) => new() { Name = name, Id = EntityId<Loadout>.NewId() };

public void ModifiedEntities(Action<EntityId> handler)
{
handler(Id.Value);
}
public static CreateLoadout Create(string name) => new(EntityId<Loadout>.NewId(), name);
}
3 changes: 1 addition & 2 deletions tests/NexusMods.EventSourcing.TestModel/Events/DeleteMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ namespace NexusMods.EventSourcing.TestModel.Events;
[MemoryPackable]
public partial record DeleteMod(EntityId<Mod> ModId, EntityId<Loadout> LoadoutId) : IEvent
{
public ValueTask Apply<T>(T context) where T : IEventContext
public void Apply<T>(T context) where T : IEventContext
{
context.Retract(LoadoutId, Loadout._mods, ModId);
context.Retract(ModId, Mod._loadout, LoadoutId);
return ValueTask.CompletedTask;
}
}
10 changes: 2 additions & 8 deletions tests/NexusMods.EventSourcing.TestModel/Events/RenameLoadout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@ namespace NexusMods.EventSourcing.TestModel.Events;

[EventId("95C3D0AF-EBCA-4DA8-ACAE-144E66F48A50")]
[MemoryPackable]
public partial class RenameLoadout : IEvent
public partial record RenameLoadout(EntityId<Loadout> Id, string Name) : IEvent
{
public required EntityId<Loadout> Id { get; init; }
public required string Name { get; init; }

public ValueTask Apply<T>(T context) where T : IEventContext
public void Apply<T>(T context) where T : IEventContext
{
context.Emit(Id, Loadout._name, Name);
return ValueTask.CompletedTask;
}

public static RenameLoadout Create(EntityId<Loadout> id, string name) => new() { Id = id, Name = name };
}
16 changes: 3 additions & 13 deletions tests/NexusMods.EventSourcing.TestModel/Events/SwapModEnabled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,10 @@ namespace NexusMods.EventSourcing.TestModel.Events;

[EventId("8492075A-DED5-42BF-8D01-B4CDCE2526CF")]
[MemoryPackable]
public partial class SwapModEnabled : IEvent
public partial record SwapModEnabled(EntityId<Mod> ModId, bool Enabled) : IEvent
{
public required EntityId<Mod> Id { get; init; }
public required bool Enabled { get; init; }
public async ValueTask Apply<T>(T context) where T : IEventContext
public void Apply<T>(T context) where T : IEventContext
{
context.Emit(Id, Mod._enabled, Enabled);
context.Emit(ModId, Mod._enabled, Enabled);
}

/// <summary>
/// Helper method to create a new event instance.
/// </summary>
/// <param name="id"></param>
/// <param name="enabled"></param>
/// <returns></returns>
public static SwapModEnabled Create(EntityId<Mod> id, bool enabled) => new() { Id = id, Enabled = enabled};
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public async ValueTask Add<T>(T entity) where T : IEvent
{
var data = serializer.Serialize(entity);
var logger = new ModifiedEntityLogger();
await entity.Apply(logger);
entity.Apply(logger);
lock (_events)
{
foreach (var id in logger.Entities)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async void ChangingPropertyChangesTheValue()
var loadout = _ctx.Get(createEvent.Id);
loadout.Name.Should().Be("Test");

await _ctx.Add(RenameLoadout.Create(createEvent.Id, "New Name"));
await _ctx.Add(new RenameLoadout(createEvent.Id, "New Name"));
loadout.Name.Should().Be("New Name");
}

Expand Down
4 changes: 2 additions & 2 deletions tests/NexusMods.EventSourcing.Tests/EventSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public class EventSerializerTests(EventSerializer serializer)
[Fact]
public void CanSerializeEvents()
{
serializer.Serialize(SwapModEnabled.Create(EntityId<Mod>.NewId(), true));
serializer.Serialize(new SwapModEnabled(EntityId<Mod>.NewId(), true));
}

[Fact]
public void CanDeserializeEvents()
{
var id = EntityId<Mod>.NewId();
var @event = SwapModEnabled.Create(id, true);
var @event = new SwapModEnabled(id, true);
var serialized = serializer.Serialize(@event);
var deserialized = serializer.Deserialize(serialized);
deserialized.Should().BeEquivalentTo(@event);
Expand Down

0 comments on commit b7e8af4

Please sign in to comment.