function trackGeneratorChanges(generator, oldCollectorName, newCollectorName) {
  if (oldCollectorName === newCollectorName) {
    return trackChangesForCollector(oldCollectorName, UPDATED, generator);
  } else {
    return Promise.all([
      trackChangesForCollector(oldCollectorName, DELETED, generator),
      trackChangesForCollector(newCollectorName, ADDED, generator),
    ]);
  }

  function trackChangesForCollector(collectorName, change, generator) {
    const genId = generator.id;
    if (!collectorName) return Promise.resolve();
    return getChangedIds(collectorName)
    .then((changedIds) => {

      // determine if this generator has already been changed since the last heartbeat
      const alreadyAdded = changedIds.added.includes(genId);
      const alreadyDeleted = changedIds.deleted.includes(genId);
      const alreadyUpdated = changedIds.updated.includes(genId);
      const alreadyChanged = alreadyAdded || alreadyDeleted || alreadyUpdated;

      // account for previous changes
      let undoChange;
      if (alreadyChanged) {
        if (alreadyAdded && change === DELETED) {
          undoChange = ADDED;
          change = NOCHANGE;
        } else if (alreadyDeleted && change === ADDED) {
          undoChange = DELETED;
          change = UPDATED;
        } else if (alreadyUpdated && change === DELETED) {
          undoChange = UPDATED;
          change = DELETED;
        } else {
          change = NOCHANGE;
        }
      }

      // update redis
      return Promise.all([
        removeFromSet(collectorName, undoChange, genId),
        addToSet(collectorName, change, genId),
      ]);
    });
  }

} // trackGeneratorChanges
/**
 * Track generator changes to be sent in the heartbeat response.
 * Maintain redis keys that map collector names to sets of
 * generators added, deleted, and updated since the last heartbeat from that
 * collector.
 *
 * @param {Object} generator - The Generator that has been changed
 * @param {Array} oldCollector - The name of the Collector this Generator was
 *  assigned to before the update.
 * @param {Array} newCollector - The name of the Collector this Generator is
 *  assigned to after the update.
 */
Track generator changes to be sent in the heartbeat response.
Maintain redis keys that map collector names to sets of
generators added, deleted, and updated since the last heartbeat from that
collector.

@param {Object} generator - The Generator that has been changed
@param {Array} oldCollector - The name of the Collector this Generator was
assigned to before the update.
@param {Array} newCollector - The name of the Collector this Generator is
assigned to after the update.
Track generator changes to be sent in the heartbeat response . Maintain redis keys that map collector names to sets of generators added deleted and updated since the last heartbeat from that collector . @param { Object } generator - The Generator that has been changed @param { Array } oldCollector - The name of the Collector this Generator was assigned to before the update . @param { Array } newCollector - The name of the Collector this Generator is assigned to after the update .
function trackGeneratorChanges(generator, oldCollectorName, newCollectorName) {
  if (oldCollectorName === newCollectorName) {
    return trackChangesForCollector(oldCollectorName, UPDATED, generator);
  } else {
    return Promise.all([
      trackChangesForCollector(oldCollectorName, DELETED, generator),
      trackChangesForCollector(newCollectorName, ADDED, generator),
    ]);
  }
  function trackChangesForCollector(collectorName, change, generator) {
    const genId = generator.id;
    if (!collectorName) return Promise.resolve();
    return getChangedIds(collectorName)
    .then((changedIds) => {
      const alreadyAdded = changedIds.added.includes(genId);
      const alreadyDeleted = changedIds.deleted.includes(genId);
      const alreadyUpdated = changedIds.updated.includes(genId);
      const alreadyChanged = alreadyAdded || alreadyDeleted || alreadyUpdated;
      let undoChange;
      if (alreadyChanged) {
        if (alreadyAdded && change === DELETED) {
          undoChange = ADDED;
          change = NOCHANGE;
        } else if (alreadyDeleted && change === ADDED) {
          undoChange = DELETED;
          change = UPDATED;
        } else if (alreadyUpdated && change === DELETED) {
          undoChange = UPDATED;
          change = DELETED;
        } else {
          change = NOCHANGE;
        }
      }
      return Promise.all([
        removeFromSet(collectorName, undoChange, genId),
        addToSet(collectorName, change, genId),
      ]);
    });
  }
} 
function trackGeneratorChanges ( generator , oldCollectorName , newCollectorName ) { if ( oldCollectorName === newCollectorName ) { return trackChangesForCollector ( oldCollectorName , UPDATED , generator ) ; } else { return Promise . all ( [ trackChangesForCollector ( oldCollectorName , DELETED , generator ) , trackChangesForCollector ( newCollectorName , ADDED , generator ) , ] ) ; } function trackChangesForCollector ( collectorName , change , generator ) { const genId = generator . id ; if ( ! collectorName ) return Promise . resolve ( ) ; return getChangedIds ( collectorName ) . then ( ( changedIds ) => { const alreadyAdded = changedIds . added . includes ( genId ) ; const alreadyDeleted = changedIds . deleted . includes ( genId ) ; const alreadyUpdated = changedIds . updated . includes ( genId ) ; const alreadyChanged = alreadyAdded || alreadyDeleted || alreadyUpdated ; let undoChange ; if ( alreadyChanged ) { if ( alreadyAdded && change === DELETED ) { undoChange = ADDED ; change = NOCHANGE ; } else if ( alreadyDeleted && change === ADDED ) { undoChange = DELETED ; change = UPDATED ; } else if ( alreadyUpdated && change === DELETED ) { undoChange = UPDATED ; change = DELETED ; } else { change = NOCHANGE ; } } return Promise . all ( [ removeFromSet ( collectorName , undoChange , genId ) , addToSet ( collectorName , change , genId ) , ] ) ; } ) ; } }
Track generator changes to be sent in the heartbeat response.
Track generator changes to be sent in the heartbeat response .
// determine if this generator has already been changed since the last heartbeat // account for previous changes // update redis // trackGeneratorChanges