<commit_before>'use strict';

angular.module('phoenixeye')
.service('PollSvc', [
	'$q',
	'$http',
	'$timeout',
	function PollSvc($q, $http, $timeout) {
		var defaultTimeout = 3000;

		return {
			pollUntil: function(httpConfig, stopCondition, timeout) {
				timeout = timeout || defaultTimeout;

				var deferred = $q.defer();

				function errorHandler(resp, status) {
					deferred.reject(resp);
				}

				function successHandler(resp, status) {
					if( ! stopCondition(resp) ) {
						$timeout(function () {
							$http(httpConfig)
								.success(successHandler)
								.error(errorHandler)
							;
						}, timeout);
					} else {
						deferred.resolve(resp);
					}
				}

				$http(httpConfig)
					.success(successHandler)
					.error(errorHandler)
				;

				return deferred;
			}
		}
	}
]);<commit_msg>stop poll on state change<commit_after>'use strict';

angular.module('phoenixeye')
.service('PollSvc', [
	'$q',
	'$http',
	'$timeout',
	'$rootScope',
	function PollSvc($q, $http, $timeout, $rootScope) {
		var defaultTimeout = 3000;

		//list of promises for polls
		var pollers = [];

		//stop all pollers on state change
		$rootScope.$on('$stateChangeSuccess', function () {
			pollers.forEach(function (poller) {
				poller.stopped = true;
			});
		});

		return {
			pollUntil: function(httpConfig, stopCondition, timeout) {
				timeout = timeout || defaultTimeout;

				var deferred = $q.defer();
				pollers.push(deferred);

				//error from server
				function errorHandler(resp, status) {
					deferred.reject(resp);
				}

				function successHandler(resp, status) {
					//not stopped
					if( deferred.stopped ) {
						return deferred.reject();
					}

					//stop condition not met, try again
					if( ! stopCondition(resp) ) {
						$timeout(function () {
							$http(httpConfig)
								.success(successHandler)
								.error(errorHandler)
							;
						}, timeout);
					} else {
						deferred.resolve(resp);
					}
				}

				$http(httpConfig)
					.success(successHandler)
					.error(errorHandler)
				;

				return deferred;
			}
		}
	}
]);
{'fraction_non_alphanumeric': 0.14265129682997119, 'fraction_numerical': 0.0038424591738712775, 'mean_word_length': 2.686725663716814, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3686186', 'n_tokens_mistral': 921, 'n_tokens_neox': 653, 'n_words': 133}