<commit_before>/* @flow */

import React, { Component, PropTypes } from 'react';
import invariant from 'invariant';
import Loading from './Loading';
import parseSchema from '../utils/parseSchema';
import createStoreShape from '../store/createStoreShape';
import createCacheStore from '../store/createCacheStore';
import request from '../network/request';
import normalize from '../utils/normalize';
import { UPDATE_CACHE } from '../constants';

export default class Adrenaline extends Component {
  static childContextTypes = {
    store: createStoreShape(PropTypes).isRequired,
    Loading: PropTypes.func.isRequired,
    schema: PropTypes.object.isRequired,
    performQuery: PropTypes.func.isRequired,
    performMutation: PropTypes.func.isRequired,
  };

  static propTypes = {
    children: PropTypes.func.isRequired,
    createStore: PropTypes.func,
    endpoint: PropTypes.string,
    renderLoading: PropTypes.func,
  }

  static defaultProps = {
    renderLoading: Loading,
    endpoint: '/graphql',
  }

  getChildContext() {
    return {
      store: this.store,
      Loading: this.props.renderLoading,
      schema: this.props.schema,
      performQuery: this.performQuery.bind(this),
      performMutation: this.performMutation.bind(this),
    };
  }

  constructor(props, context) {
    super(props, context);

    this.pendingQueries = [];
    this.pendingMutations = [];
    this.parsedSchema = parseSchema(props.schema);
    this.store = createCacheStore(this.parsedSchema, props.createStore);
  }

  performQuery(query, params) {
    if (this.pendingQueries.indexOf(query) > -1) return;
    this.pendingQueries = this.pendingQueries.concat(query);

    const { endpoint } = this.props;
    const { parsedSchema, store } = this;
    const { dispatch } = store;

    request(endpoint, { query, params })
      .then(json => {
        dispatch({
          type: UPDATE_CACHE,
          payload: normalize(parsedSchema, json.data),
        });
        this.pendingQueries = this.pendingQueries.filter(p => p === request);
      })
      .catch(err => {
        dispatch({ type: UPDATE_CACHE, payload: err, error: true });
        this.pendingQueries = this.pendingQueries.filter(p => p === request);
      });
  }

  performMutation({ mutation, updateCache = [] }, params, files) {
    invariant(
      mutation !== undefined && mutation !== null,
      'You have to declare "mutation" field in your mutation'
    );

    const { endpoint } = this.props;
    const { parsedSchema, store } = this;
    const { dispatch } = store;

    request(endpoint, { mutation, params }, files)
      .then(json => {
        const payload = normalize(parsedSchema, json.data);
        dispatch({ type: UPDATE_CACHE, payload });

        updateCache.forEach((fn) => {
          const { parentId, parentType, resolve } = fn(Object.values(json.data)[0]);
          const state = store.getState();
          const parent = state[parentType][parentId];
          if (!parent) return;
          dispatch({
            type: UPDATE_CACHE,
            payload: {
              [parentType]: {
                [parent.id]: resolve(parent),
              },
            },
          });
        });
      })
      .catch(err => {
        dispatch({ type: UPDATE_CACHE, payload: err, error: true });
      });
  }

  render() {
    const { children } = this.props;
    return children();
  }
}
<commit_msg>removed pending in order to than make network layer<commit_after>/* @flow */

import React, { Component, PropTypes } from 'react';
import invariant from 'invariant';
import Loading from './Loading';
import parseSchema from '../utils/parseSchema';
import createStoreShape from '../store/createStoreShape';
import createCacheStore from '../store/createCacheStore';
import request from '../network/request';
import normalize from '../utils/normalize';
import { UPDATE_CACHE } from '../constants';

export default class Adrenaline extends Component {
  static childContextTypes = {
    store: createStoreShape(PropTypes).isRequired,
    Loading: PropTypes.func.isRequired,
    schema: PropTypes.object.isRequired,
    performQuery: PropTypes.func.isRequired,
    performMutation: PropTypes.func.isRequired,
  };

  static propTypes = {
    children: PropTypes.func.isRequired,
    createStore: PropTypes.func,
    endpoint: PropTypes.string,
    renderLoading: PropTypes.func,
  }

  static defaultProps = {
    renderLoading: Loading,
    endpoint: '/graphql',
  }

  getChildContext() {
    return {
      store: this.store,
      Loading: this.props.renderLoading,
      schema: this.props.schema,
      performQuery: this.performQuery.bind(this),
      performMutation: this.performMutation.bind(this),
    };
  }

  constructor(props, context) {
    super(props, context);

    this.pendingQueries = [];
    this.pendingMutations = [];
    this.parsedSchema = parseSchema(props.schema);
    this.store = createCacheStore(this.parsedSchema, props.createStore);
  }

  performQuery(query, params) {
    const { endpoint } = this.props;
    const { parsedSchema, store } = this;
    const { dispatch } = store;

    request(endpoint, { query, params })
      .then(json => {
        dispatch({
          type: UPDATE_CACHE,
          payload: normalize(parsedSchema, json.data),
        });
      })
      .catch(err => {
        dispatch({ type: UPDATE_CACHE, payload: err, error: true });
      });
  }

  performMutation({ mutation, updateCache = [] }, params, files) {
    invariant(
      mutation !== undefined && mutation !== null,
      'You have to declare "mutation" field in your mutation'
    );

    const { endpoint } = this.props;
    const { parsedSchema, store } = this;
    const { dispatch } = store;

    request(endpoint, { mutation, params }, files)
      .then(json => {
        const payload = normalize(parsedSchema, json.data);
        dispatch({ type: UPDATE_CACHE, payload });

        updateCache.forEach((fn) => {
          const { parentId, parentType, resolve } = fn(Object.values(json.data)[0]);
          const state = store.getState();
          const parent = state[parentType][parentId];
          if (!parent) return;
          dispatch({
            type: UPDATE_CACHE,
            payload: {
              [parentType]: {
                [parent.id]: resolve(parent),
              },
            },
          });
        });
      })
      .catch(err => {
        dispatch({ type: UPDATE_CACHE, payload: err, error: true });
      });
  }

  render() {
    const { children } = this.props;
    return children();
  }
}

{'fraction_non_alphanumeric': 0.13032198992827712, 'fraction_numerical': 0.0004578055852281398, 'mean_word_length': 2.694475760992108, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6019859', 'n_tokens_mistral': 1850, 'n_tokens_neox': 1801, 'n_words': 432}