{"size": 1350, "ext": "js", "max_stars_repo_path": "src/helpers/realtimeThreads.js", "max_stars_repo_name": "LetItRock/spectrum", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/helpers/realtimeThreads.js", "max_issues_repo_name": "LetItRock/spectrum", "max_issues_repo_issues_event_min_datetime": "2021-09-20T22:20:02.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-29T10:12:21.000Z", "max_forks_repo_path": "src/helpers/realtimeThreads.js", "max_forks_repo_name": "LetItRock/spectrum", "max_forks_repo_forks_event_min_datetime": "2021-01-17T06:08:49.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-17T06:08:49.000Z"}
{"max_stars_count": null, "max_issues_count": 16, "max_forks_count": 1, "avg_line_length": 31.3953488372, "max_line_length": 105, "alphanum_fraction": 0.6355555556}
import { addActivityIndicator } from '../actions/newActivityIndicator';

// used to update feed caches with new threads in real time
// takes an array of existing threads in the cache and figures out how to insert the newly updated thread
export default (
  prevThreads: Array,
  updatedThread: Object,
  dispatch: Function
) => {
  // get an array of thread ids based on the threads already in cache
  const prevThreadIds = prevThreads.map(thread => thread.node.id);

  // determine if the newly updated thread exists in the cache or not
  // if it doesn't exist in cache, it's a new thread! if it does exist in cache, it just means its
  // been updated with a new message
  const hasNewThread = prevThreadIds.indexOf(updatedThread.id) < 0;

  // if the updated thread is new, show the activity indicator in the ui
  if (hasNewThread) {
    dispatch(addActivityIndicator());
  }

  return hasNewThread
    ? [
        {
          node: updatedThread,
          cursor: '__this-is-a-cursor__',
          __typename: 'Thread',
        },
        ...prevThreads,
      ]
    : [
        ...prevThreads.map(thread => {
          if (thread.node.id !== updatedThread.id) return thread;
          return {
            node: updatedThread,
            cursor: '__this-is-a-cursor__',
            __typename: 'Thread',
          };
        }),
      ];
};
