<commit_before>/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* global angular */
/* global angularSync */

angularSync.factory('AngularSyncInterceptor', ['AngularSync', 'AngularSyncMode', 'AngularSyncHistory', '$q', '$timeout', function (AngularSync, SyncMode, history, $q, $timeout) {
  var commands = {};

  // Prevent requests to be triggered in parallel
  // Useful to not trigger the same POST request...
  commands[SyncMode.PREVENT] = function(config) {
    if (history.contains(config.url, config.method)) {
      config.ngSync.preventError = true;
      return $q.reject(config);
    }

    history.add(config);
    return config;
  };

  // Request will be triggered whatever happens
  commands[SyncMode.FORCE] = function(config) {
    history.add(config);
    return config;
  };

  // Pending requests will be aborted and incoming request
  // will be triggered
  commands[SyncMode.ABORT] = function(config) {
    angular.forEach(history.pendings(config.url, config.method), function(rq) {
      rq.config.ngSync.preventError = true;
      rq.config.ngSync.$q.resolve();
    });

    history.clear(config.url, config.method).add(config);
    return config;
  };

  return {
    request: function (config) {
      var method = config.method;

      // Get request mode
      var ngSync = config.ngSync = config.ngSync || {};
      var mode = ngSync.mode = ngSync.mode || AngularSync.mode(method);

      // Add timeout to abort request if needed
      var deferred = ngSync.$q = $q.defer();
      var promise = ngSync.$promise = deferred.promise;

      // If a timeout promise is already defined, we must resolve new
      // timeout promise when original timeout is resolved to force abortion
      var timeout = config.timeout;
      var timeoutTask,timeoutDeferred, timeoutPromise;
      if (timeout) {
        // If timeout is specified as a number, then we should abort request
        // when timeout is reached
        if (timeout > 0) {
          timeoutDeferred = $q.defer();
          timeoutPromise = timeoutDeferred.promise;
          timeoutTask = $timeout(timeoutDeferred.resolve, timeout);
          timeout = timeoutPromise;
        }

        ngSync.$timeoutTask = timeoutTask;
        ngSync.$timeout = timeout;
        timeout.then(deferred.resolve);
      }

      var free = function() {
        // Set everything to null...
        deferred = promise = timeout = timeoutTask =
          timeoutDeferred = timeoutPromise = ngSync = free = null;
      };

      if (timeoutPromise) {
        timeoutPromise.then(free, free);
      }
<commit_msg>fix:  function does not need a digest cycle<commit_after>/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* global angular */
/* global angularSync */

angularSync.factory('AngularSyncInterceptor', ['AngularSync', 'AngularSyncMode', 'AngularSyncHistory', '$q', '$timeout', function (AngularSync, SyncMode, history, $q, $timeout) {
  var commands = {};

  // Prevent requests to be triggered in parallel
  // Useful to not trigger the same POST request...
  commands[SyncMode.PREVENT] = function(config) {
    if (history.contains(config.url, config.method)) {
      config.ngSync.preventError = true;
      return $q.reject(config);
    }

    history.add(config);
    return config;
  };

  // Request will be triggered whatever happens
  commands[SyncMode.FORCE] = function(config) {
    history.add(config);
    return config;
  };

  // Pending requests will be aborted and incoming request
  // will be triggered
  commands[SyncMode.ABORT] = function(config) {
    angular.forEach(history.pendings(config.url, config.method), function(rq) {
      rq.config.ngSync.preventError = true;
      rq.config.ngSync.$q.resolve();
    });

    history.clear(config.url, config.method).add(config);
    return config;
  };

  return {
    request: function (config) {
      var method = config.method;

      // Get request mode
      var ngSync = config.ngSync = config.ngSync || {};
      var mode = ngSync.mode = ngSync.mode || AngularSync.mode(method);

      // Add timeout to abort request if needed
      var deferred = ngSync.$q = $q.defer();
      var promise = ngSync.$promise = deferred.promise;

      // If a timeout promise is already defined, we must resolve new
      // timeout promise when original timeout is resolved to force abortion
      var timeout = config.timeout;
      var timeoutTask,timeoutDeferred, timeoutPromise;
      if (timeout) {
        // If timeout is specified as a number, then we should abort request
        // when timeout is reached
        if (timeout > 0) {
          timeoutDeferred = $q.defer();
          timeoutPromise = timeoutDeferred.promise;
          timeoutTask = $timeout(timeoutDeferred.resolve, timeout, false);
          timeout = timeoutPromise;
        }

        ngSync.$timeoutTask = timeoutTask;
        ngSync.$timeout = timeout;
        timeout.then(deferred.resolve);
      }

      var free = function() {
        // Set everything to null...
        deferred = promise = timeout = timeoutTask =
          timeoutDeferred = timeoutPromise = ngSync = free = null;
      };

      if (timeoutPromise) {
        timeoutPromise.then(free, free);
      }

{'fraction_non_alphanumeric': 0.09538002980625931, 'fraction_numerical': 0.0013548299688389107, 'mean_word_length': 3.038293216630197, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5606294', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1899, 'n_words': 740}