<commit_before>import {
  pendingActionCreator,
  doneActionCreator,
  failureActionCreator,
} from './actionCreators';
import createAsyncActionsStateChecker from './createAsyncActionsStateChecker';

export const hasDone = (state, ...checkFor) =>
createAsyncActionsStateChecker(state, ...checkFor).hasDone();

export function createActionCreatorFromPromise(
  manageAsyncStateFor,
  getPromise,
  actionCreator,
  lockIfAlreadyInPending = true,
  debounce = false
) {
  if (!manageAsyncStateFor) {
    throw new Error(`createActionCreatorFromPromise: manageAsyncStateFor is required,${
    ''}it can be a function or a string`);
  }

  if (!getPromise) {
    throw new Error('createActionCreatorFromPromise: getPromise is required');
  }

  let lastRequestKey;

  return (...args) => (dispatch, getState) => {
    let key;
    let group;
    let asyncStateFor;

    if (typeof manageAsyncStateFor === 'function') {
      const manageAsyncStateForArgs = args.slice(0);
      manageAsyncStateForArgs.push(getState);
      asyncStateFor = manageAsyncStateFor(...manageAsyncStateForArgs);
    } else {
      asyncStateFor = manageAsyncStateFor;
    }

    if (typeof asyncStateFor === 'object') {
      key = asyncStateFor.key;
      group = asyncStateFor.group;
    } else if (typeof asyncStateFor === 'string') {
      key = asyncStateFor;
    }

    // use a bool or a string property
    if (lockIfAlreadyInPending !== false) {
      if (
        typeof lockIfAlreadyInPending === 'boolean' &&
        !hasDone(getState(), key)
      ) {
        return;
      }

      if (
        typeof lockIfAlreadyInPending === 'string' &&
        !hasDone(getState(), lockIfAlreadyInPending)
      ) {
        return;
      }

      if (
        typeof lockIfAlreadyInPending === 'object' &&
        lockIfAlreadyInPending.length &&
        !hasDone(getState(), ...lockIfAlreadyInPending)
      ) {
        return;
      }

      if (
        typeof lockIfAlreadyInPending === 'object' &&
        typeof lockIfAlreadyInPending.length === 'undefined' &&
        !hasDone(getState(), lockIfAlreadyInPending)
      ) {
        return;
      }
    }

    const _lastRequestKey = (new Date()).getTime() + (Math.random() + 1000);
    lastRequestKey = _lastRequestKey;

    dispatch(pendingActionCreator(key, group));

    const promiseArgs = args.slice(0);
    promiseArgs.push(getState);

    getPromise(...promiseArgs)
      .then((d) => {
        if (debounce) {
          if (lastRequestKey !== _lastRequestKey) {
            return;
          }
        }

        const doneActionCreatorArgs = args.slice(0);
        doneActionCreatorArgs.push(d, getState);

        dispatch(doneActionCreator(
          key,
          actionCreator(...doneActionCreatorArgs)
        ));
      })
      .catch((e) => {
        if (debounce) {
          if (lastRequestKey !== _lastRequestKey) {
            return;
          }
        }

        dispatch(failureActionCreator(key, e));
      })
    ;
  };
}<commit_msg>refactor(utils): Add Promise.resolve and return the promise in createActionCreatorFromPromise<commit_after>import {
  pendingActionCreator,
  doneActionCreator,
  failureActionCreator,
} from './actionCreators';
import createAsyncActionsStateChecker from './createAsyncActionsStateChecker';

export const hasDone = (state, ...checkFor) =>
createAsyncActionsStateChecker(state, ...checkFor).hasDone();

export function createActionCreatorFromPromise(
  manageAsyncStateFor,
  getPromise,
  actionCreator,
  lockIfAlreadyInPending = true,
  debounce = false
) {
  if (!manageAsyncStateFor) {
    throw new Error(`createActionCreatorFromPromise: manageAsyncStateFor is required,${
    ''}it can be a function or a string`);
  }

  if (!getPromise) {
    throw new Error('createActionCreatorFromPromise: getPromise is required');
  }

  let lastRequestKey;

  return (...args) => (dispatch, getState) => {
    let key;
    let group;
    let asyncStateFor;

    if (typeof manageAsyncStateFor === 'function') {
      const manageAsyncStateForArgs = args.slice(0);
      manageAsyncStateForArgs.push(getState);
      asyncStateFor = manageAsyncStateFor(...manageAsyncStateForArgs);
    } else {
      asyncStateFor = manageAsyncStateFor;
    }

    if (typeof asyncStateFor === 'object') {
      key = asyncStateFor.key;
      group = asyncStateFor.group;
    } else if (typeof asyncStateFor === 'string') {
      key = asyncStateFor;
    }

    // use a bool or a string property
    if (lockIfAlreadyInPending !== false) {
      if (
        typeof lockIfAlreadyInPending === 'boolean' &&
        !hasDone(getState(), key)
      ) {
        return Promise.resolve();
      }

      if (
        typeof lockIfAlreadyInPending === 'string' &&
        !hasDone(getState(), lockIfAlreadyInPending)
      ) {
        return Promise.resolve();
      }

      if (
        typeof lockIfAlreadyInPending === 'object' &&
        lockIfAlreadyInPending.length &&
        !hasDone(getState(), ...lockIfAlreadyInPending)
      ) {
        return Promise.resolve();
      }

      if (
        typeof lockIfAlreadyInPending === 'object' &&
        typeof lockIfAlreadyInPending.length === 'undefined' &&
        !hasDone(getState(), lockIfAlreadyInPending)
      ) {
        return Promise.resolve();
      }
    }

    const _lastRequestKey = (new Date()).getTime() + (Math.random() + 1000);
    lastRequestKey = _lastRequestKey;

    dispatch(pendingActionCreator(key, group));

    const promiseArgs = args.slice(0);
    promiseArgs.push(dispatch, getState);

    return getPromise(...promiseArgs)
      .then((d) => {
        if (debounce) {
          if (lastRequestKey !== _lastRequestKey) {
            return;
          }
        }

        const doneActionCreatorArgs = args.slice(0);
        doneActionCreatorArgs.push(d, getState);

        dispatch(doneActionCreator(
          key,
          actionCreator(...doneActionCreatorArgs)
        ));
      })
      .catch((e) => {
        if (debounce) {
          if (lastRequestKey !== _lastRequestKey) {
            return;
          }
        }

        dispatch(failureActionCreator(key, e));
      })
    ;
  };
}
{'fraction_non_alphanumeric': 0.11721671272963746, 'fraction_numerical': 0.0022760526743618925, 'mean_word_length': 2.601873536299766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2792192', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1775, 'n_words': 358}