LwwMerger.swift 28 lignes · 1095 octets
import Foundation

enum LwwMerger {
    /// Returns remote if remoteTs >= localTs (remote wins ties), else local.
    static func pickLww<T>(local: T, localTs: Int64, remote: T, remoteTs: Int64) -> T {
        remoteTs >= localTs ? remote : local
    }

    struct TaskPlacement: Equatable {
        var colonneId: String
        var ordre: Int
        var updatedAt: Int64
    }

    static func pickTaskMove(local: TaskPlacement, remote: TaskPlacement) -> TaskPlacement {
        pickLww(local: local, localTs: local.updatedAt, remote: remote, remoteTs: remote.updatedAt)
    }

    /// Append-only: insert interaction only if serverId is not already known locally.
    static func shouldInsertInteraction(existingServerIds: Set<String>, remoteServerId: String) -> Bool {
        !existingServerIds.contains(remoteServerId)
    }

    /// Caller deletes local rows by serverId; returns remaining ids after tombstone application.
    static func applyTombstoneIds(localServerIds: Set<String>, tombstoneIds: Set<String>) -> Set<String> {
        localServerIds.subtracting(tombstoneIds)
    }
}