init 1.0.0
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
using LehrerApp.Sync.Models;
|
||||
|
||||
namespace LehrerApp.Sync;
|
||||
|
||||
/// <summary>
|
||||
/// Desktop gewinnt gegen Companion.
|
||||
/// Bei Gleichstand: späterer Timestamp gewinnt.
|
||||
/// </summary>
|
||||
public class ConflictResolver(EventQueue queue)
|
||||
{
|
||||
public ConflictEntry? TryResolve(SyncEvent remote, string localDeviceId)
|
||||
{
|
||||
var local = queue.GetPending()
|
||||
.FirstOrDefault(e => e.EntityType == remote.EntityType
|
||||
&& e.EntityId == remote.EntityId
|
||||
&& e.DeviceId != remote.DeviceId);
|
||||
if (local is null) return null;
|
||||
|
||||
var winner = (local.DeviceType, remote.DeviceType) switch
|
||||
{
|
||||
(DeviceType.Desktop, DeviceType.Companion) => local,
|
||||
(DeviceType.Companion, DeviceType.Desktop) => remote,
|
||||
_ => local.Timestamp >= remote.Timestamp ? local : remote,
|
||||
};
|
||||
|
||||
if (winner == remote) queue.Acknowledge([local.EventId]);
|
||||
|
||||
return new ConflictEntry
|
||||
{
|
||||
LocalEvent = local,
|
||||
RemoteEvent = remote,
|
||||
Resolution = winner == local ? "LocalWon" : "RemoteWon",
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user