export function diffMonitors()

in src/push/monitor.ts [79:115]


export function diffMonitors(
  local: MonitorHashID[],
  remote: MonitorHashID[]
): RemoteDiffResult {
  const result = new RemoteDiffResult();
  const localMonitorsIDToHash = new Map<string, string>();
  for (const hashID of local) {
    localMonitorsIDToHash.set(hashID.journey_id, hashID.hash);
  }
  const remoteMonitorsIDToHash = new Map<string, string>();
  for (const hashID of remote) {
    remoteMonitorsIDToHash.set(hashID.journey_id, hashID.hash);
  }

  // Compare local to remote
  for (const [localID, localHash] of localMonitorsIDToHash) {
    // Hash is reset to '' when a monitor is edited on the UI
    if (!remoteMonitorsIDToHash.has(localID)) {
      result.newIDs.add(localID);
    } else {
      const remoteHash = remoteMonitorsIDToHash.get(localID);
      if (remoteHash != localHash) {
        result.changedIDs.add(localID);
      } else if (remoteHash === localHash) {
        result.unchangedIDs.add(localID);
      }
    }
    // We no longer need to process this ID, removing it here
    // reduces the numbers considered in the next phase
    remoteMonitorsIDToHash.delete(localID);
  }

  for (const [id] of remoteMonitorsIDToHash) {
    result.removedIDs.add(id);
  }
  return result;
}