merge method

Hlc merge(
  1. Hlc remote, {
  2. DateTime? wallTime,
})

Compares and validates a timestamp from a remote system with the local timestamp to preserve monotonicity. Local wall time will be used if wallTime isn't supplied.

Implementation

Hlc merge(Hlc remote, {DateTime? wallTime}) {
  // Retrieve the local wall time if millis is null
  wallTime = (wallTime ?? DateTime.now()).toUtc();

  // No need to do any more work if our date + counter is same or higher
  if (remote.dateTime.isBefore(dateTime) ||
      (remote.dateTime.isAtSameMomentAs(dateTime) &&
          remote.counter <= counter)) return this;

  // Assert the node id
  if (nodeId == remote.nodeId) {
    throw DuplicateNodeException(nodeId);
  }
  // Assert the remote clock drift
  if (remote.dateTime.difference(wallTime) > _maxDrift) {
    throw ClockDriftException(remote.dateTime, wallTime);
  }

  return remote.apply(nodeId: nodeId);
}