-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60ca19e
commit ea9d45b
Showing
13 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Akka.Actor; | ||
using Akka.Cluster.Sharding; | ||
|
||
namespace DrawTogether.Actors; | ||
|
||
/// <summary> | ||
/// A generic "child per entity" parent actor. | ||
/// </summary> | ||
/// <remarks> | ||
/// Intended for simplifying unit tests where we don't want to use Akka.Cluster.Sharding. | ||
/// </remarks> | ||
public sealed class GenericChildPerEntityParent : UntypedActor | ||
{ | ||
public static Props Props(IMessageExtractor extractor, Func<string, Props> propsFactory) | ||
{ | ||
return Akka.Actor.Props.Create(() => new GenericChildPerEntityParent(extractor, propsFactory)); | ||
} | ||
|
||
/* | ||
* Re-use Akka.Cluster.Sharding's infrastructure here to keep things simple. | ||
*/ | ||
private readonly IMessageExtractor _extractor; | ||
private readonly Func<string, Props> _propsFactory; | ||
|
||
public GenericChildPerEntityParent(IMessageExtractor extractor, Func<string, Props> propsFactory) | ||
{ | ||
_extractor = extractor; | ||
_propsFactory = propsFactory; | ||
} | ||
|
||
protected override void OnReceive(object message) | ||
{ | ||
var result = _extractor.EntityId(message); | ||
if (result is null) | ||
{ | ||
Unhandled(message); | ||
return; | ||
} | ||
|
||
Context.Child(result).GetOrElse(() => Context.ActorOf(_propsFactory(result), result)) | ||
.Forward(_extractor.EntityMessage(message)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Akka.Cluster.Hosting" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Drawings\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace DrawTogether.Entities; | ||
|
||
public enum ResultCode | ||
{ | ||
Ok, | ||
NoOp, | ||
BadRequest, | ||
Unauthorized, | ||
TimeOut | ||
} | ||
|
||
public sealed record CommandResult | ||
{ | ||
public ResultCode Code { get; init; } | ||
|
||
public string? Message { get; init; } | ||
|
||
public static CommandResult Ok() => new() { Code = ResultCode.Ok }; | ||
|
||
public bool IsError => Code != ResultCode.Ok && Code != ResultCode.NoOp; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,4 @@ | |
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Users\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace DrawTogether.Entities.Drawings; | ||
|
||
public sealed class DrawingSessionId(string SessionId); | ||
Check warning on line 3 in src/DrawTogether.Entities/Drawings/DrawingSessionId.cs GitHub Actions / Test-ubuntu-latest
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Collections.Immutable; | ||
using DrawTogether.Entities.Drawings.Messages; | ||
using DrawTogether.Entities.Users; | ||
|
||
namespace DrawTogether.Entities.Drawings; | ||
|
||
public sealed record DrawingSessionState(DrawingSessionId DrawingSessionId) : IWithDrawingSessionId | ||
{ | ||
public ImmutableDictionary<StrokeId, ConnectedStroke> Strokes { get; init; } = ImmutableDictionary<StrokeId, ConnectedStroke>.Empty; | ||
|
||
public ImmutableHashSet<UserId> ConnectedUsers { get; init; } = ImmutableHashSet<UserId>.Empty; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/DrawTogether.Entities/Drawings/Messages/DrawingSessionCommands.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using DrawTogether.Entities.Users; | ||
|
||
namespace DrawTogether.Entities.Drawings.Messages; | ||
|
||
public interface IDrawingSessionCommand : IWithDrawingSessionId{ } | ||
|
||
public static class DrawingSessionCommands | ||
{ | ||
public sealed record AddStroke(DrawingSessionId DrawingSessionId, ConnectedStroke Stroke) : IDrawingSessionCommand; | ||
|
||
public sealed record RemoveStroke(DrawingSessionId DrawingSessionId, StrokeId StrokeId) : IDrawingSessionCommand; | ||
|
||
public sealed record ClearStrokes(DrawingSessionId DrawingSessionId) : IDrawingSessionCommand; | ||
|
||
public sealed record AddUser(DrawingSessionId DrawingSessionId, UserId UserId) : IDrawingSessionCommand; | ||
|
||
public sealed record RemoveUser(DrawingSessionId DrawingSessionId, UserId UserId) : IDrawingSessionCommand; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/DrawTogether.Entities/Drawings/Messages/DrawingSessionEvents.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using DrawTogether.Entities.Users; | ||
|
||
namespace DrawTogether.Entities.Drawings.Messages; | ||
|
||
public interface IDrawingSessionEvent : IWithDrawingSessionId { } | ||
|
||
public static class DrawingSessionEvents | ||
{ | ||
public sealed record DrawingSessionCreated(DrawingSessionId DrawingSessionId) : IDrawingSessionEvent; | ||
|
||
public sealed record StrokeAdded(DrawingSessionId DrawingSessionId, ConnectedStroke Stroke) : IDrawingSessionEvent; | ||
|
||
public sealed record StrokeRemoved(DrawingSessionId DrawingSessionId, StrokeId StrokeId) : IDrawingSessionEvent; | ||
|
||
public sealed record StrokesCleared(DrawingSessionId DrawingSessionId) : IDrawingSessionEvent; | ||
|
||
public sealed record UserAdded(DrawingSessionId DrawingSessionId, UserId UserId) : IDrawingSessionEvent; | ||
|
||
public sealed record UserRemoved(DrawingSessionId DrawingSessionId, UserId UserId) : IDrawingSessionEvent; | ||
|
||
/// <summary> | ||
/// Occurs when the last user leaves the session. | ||
/// </summary> | ||
public sealed record DrawingSessionClosed(DrawingSessionId DrawingSessionId) : IDrawingSessionEvent; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/DrawTogether.Entities/Drawings/Messages/DrawingSessionQueries.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace DrawTogether.Entities.Drawings.Messages; | ||
|
||
public interface IDrawingSessionQuery : IWithDrawingSessionId { } | ||
|
||
public static class DrawingSessionQueries | ||
{ | ||
public sealed record GetDrawingSessionState(DrawingSessionId DrawingSessionId) : IDrawingSessionQuery; | ||
|
||
public sealed record GetDrawingSessionUsers(DrawingSessionId DrawingSessionId) : IDrawingSessionQuery; | ||
|
||
public sealed record SubscribeToDrawingSession(DrawingSessionId DrawingSessionId) : IDrawingSessionQuery; | ||
|
||
public sealed record UnsubscribeFromDrawingSession(DrawingSessionId DrawingSessionId) : IDrawingSessionQuery; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/DrawTogether.Entities/Drawings/Messages/IWithDrawingSessionId.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using DrawTogether.Entities.Users; | ||
|
||
namespace DrawTogether.Entities.Drawings.Messages; | ||
|
||
/// <summary> | ||
/// Marker interface for messages that have a <see cref="DrawingSessionId"/>. | ||
/// </summary> | ||
public interface IWithDrawingSessionId | ||
{ | ||
DrawingSessionId DrawingSessionId { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace DrawTogether.Entities.Users; | ||
|
||
public sealed class UserId(string IdentityName); | ||
Check warning on line 3 in src/DrawTogether.Entities/Users/UserId.cs GitHub Actions / Test-ubuntu-latest
|