{"size": 2034, "ext": "js", "max_stars_repo_path": "src/removeNodeFromStore.js", "max_stars_repo_name": "zth/relay-utils", "max_stars_repo_stars_event_min_datetime": "2019-05-10T19:38:51.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-10T22:44:58.000Z", "max_issues_repo_path": "src/removeNodeFromStore.js", "max_issues_repo_name": "zth/relay-utils", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/removeNodeFromStore.js", "max_forks_repo_name": "zth/relay-utils", "max_forks_repo_forks_event_min_datetime": "2019-10-26T20:46:42.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-15T04:35:57.000Z"}
{"max_stars_count": 36, "max_issues_count": null, "max_forks_count": 4, "avg_line_length": 24.8048780488, "max_line_length": 76, "alphanum_fraction": 0.6130776794}
// @flow
import { ConnectionHandler } from 'relay-runtime';
import type {
  RecordProxy,
  RecordSourceProxy
} from 'relay-runtime/store/RelayStoreTypes';
import type { ConnectionConfig } from './createAndAddEdgeToConnections';
import type { LinkedRecordConfig } from './createAndAddEdgeToLinkedRecords';

type RemoveNodeAndConnectionsConfig = {|
  node: RecordProxy,
  connections?: Array<ConnectionConfig>,
  linkedRecords?: Array<LinkedRecordConfig>
|};

export function removeNodeFromStore(
  store: RecordSourceProxy,
  config: RemoveNodeAndConnectionsConfig
): void {
  const { node } = config;

  if (!node) {
    if (__DEV__) {
      console.warn('Could not find source node');
    }
    return;
  }

  if (config.connections) {
    config.connections.forEach(connectionConfig => {
      const owner = store.get(connectionConfig.parentID);

      if (owner) {
        const connection = ConnectionHandler.getConnection(
          owner,
          connectionConfig.key,
          connectionConfig.filters
        );

        if (connection) {
          ConnectionHandler.deleteNode(connection, node.getDataID());
        } else {
          if (__DEV__) {
            console.warn('Could not find connection.');
          }
        }
      } else {
        if (__DEV__) {
          console.warn('Could not find owner');
        }
      }
    });
  }

  if (config.linkedRecords) {
    config.linkedRecords.forEach(linkedRecordConfig => {
      const owner = store.get(linkedRecordConfig.parentID);

      if (owner) {
        const linkedRecord = owner.getLinkedRecord(
          linkedRecordConfig.key,
          linkedRecordConfig.filters
        );

        if (linkedRecord) {
          ConnectionHandler.deleteNode(linkedRecord, node.getDataID());
        } else {
          if (__DEV__) {
            console.warn('Could not find linked record');
          }
        }
      } else {
        if (__DEV__) {
          console.warn('Could not find owner');
        }
      }
    });
  }

  store.delete(node.getDataID());
}
