<commit_before>var version = '0.1';

var options = {
	appMessage: {
		maxTries: 3,
		retryTimeout: 3000,
		timeout: 100
	},
	http: {
		timeout: 20000
	}
};

var mediaPlayer = {
	PLEX: 0,
	VLC: 1,
	XBMC: 2
};

var appMessageQueue = [];
var players = JSON.parse(localStorage.getItem('players')) || [];

function sendPlayerList() {
	if (players.length === 0) {
		appMessageQueue.push({'message': {index: 0, request: true, title: 'No players found', status: '', player: 255}});
	}
	for (var i = 0; i < players.length; i++) {
		appMessageQueue.push({message: {
			index: i,
			request: true,
			title: players[i].title,
			status: players[i].subtitle,
			player: parseInt(players[i].player)
		}});
	}
	sendAppMessageQueue();
}

function sendAppMessageQueue() {
	if (appMessageQueue.length > 0) {
		currentAppMessage = appMessageQueue[0];
		currentAppMessage.numTries = currentAppMessage.numTries || 0;
		currentAppMessage.transactionId = currentAppMessage.transactionId || -1;
		if (currentAppMessage.numTries < options.appMessage.maxTries) {
			console.log('Sending AppMessage to Pebble: ' + JSON.stringify(currentAppMessage.message));
			Pebble.sendAppMessage(
				currentAppMessage.message,
				function(e) {
					appMessageQueue.shift();
					setTimeout(function() {
						sendAppMessageQueue();
					}, options.appMessage.timeout);
				}, function(e) {
					console.log('Failed sending AppMessage for transactionId:' + e.data.transactionId + '. Error: ' + e.data.error.message);
					appMessageQueue[0].transactionId = e.data.transactionId;
					appMessageQueue[0].numTries++;
					setTimeout(function() {
						sendAppMessageQueue();
					}, options.appMessage.retryTimeout);
				}
			);
		} else {
			console.log('Failed sending AppMessage for transactionId:' + currentAppMessage.transactionId + '. Bailing. ' + JSON.stringify(currentAppMessage.message));
		}
	}
}

function makeRequestToPLEX(index, request) {

}

function makeRequestToVLC(index, request) {
	var xhr = new XMLHttpRequest();
	xhr.open('GET', 'http://' + players[index].server + '/requests/status.json?' + request, true);
	xhr.setRequestHeader('Authorization', 'Basic ' + base64_encode(':' + players[index].password));
	xhr.timeout = options.http.timeout;
	xhr.onload = function(e) {
		if (xhr.readyState == 4) {<commit_msg>Fix a bug where appMessageQueue would not remove item after max tries.<commit_after>var version = '0.1';

var options = {
	appMessage: {
		maxTries: 3,
		retryTimeout: 3000,
		timeout: 100
	},
	http: {
		timeout: 20000
	}
};

var mediaPlayer = {
	PLEX: 0,
	VLC: 1,
	XBMC: 2
};

var appMessageQueue = [];
var players = JSON.parse(localStorage.getItem('players')) || [];

function sendPlayerList() {
	if (players.length === 0) {
		appMessageQueue.push({'message': {index: 0, request: true, title: 'No players found', status: '', player: 255}});
	}
	for (var i = 0; i < players.length; i++) {
		appMessageQueue.push({message: {
			index: i,
			request: true,
			title: players[i].title,
			status: players[i].subtitle,
			player: parseInt(players[i].player)
		}});
	}
	sendAppMessageQueue();
}

function sendAppMessageQueue() {
	if (appMessageQueue.length > 0) {
		currentAppMessage = appMessageQueue[0];
		currentAppMessage.numTries = currentAppMessage.numTries || 0;
		currentAppMessage.transactionId = currentAppMessage.transactionId || -1;
		if (currentAppMessage.numTries < options.appMessage.maxTries) {
			console.log('Sending AppMessage to Pebble: ' + JSON.stringify(currentAppMessage.message));
			Pebble.sendAppMessage(
				currentAppMessage.message,
				function(e) {
					appMessageQueue.shift();
					setTimeout(function() {
						sendAppMessageQueue();
					}, options.appMessage.timeout);
				}, function(e) {
					console.log('Failed sending AppMessage for transactionId:' + e.data.transactionId + '. Error: ' + e.data.error.message);
					appMessageQueue[0].transactionId = e.data.transactionId;
					appMessageQueue[0].numTries++;
					setTimeout(function() {
						sendAppMessageQueue();
					}, options.appMessage.retryTimeout);
				}
			);
		} else {
			appMessageQueue.shift();
			console.log('Failed sending AppMessage for transactionId:' + currentAppMessage.transactionId + '. Bailing. ' + JSON.stringify(currentAppMessage.message));
		}
	} else {
		console.log('AppMessage queue is empty.');
	}
}

function makeRequestToPLEX(index, request) {

}

function makeRequestToVLC(index, request) {
	var xhr = new XMLHttpRequest();
	xhr.open('GET', 'http://' + players[index].server + '/requests/status.json?' + request, true);
	xhr.setRequestHeader('Authorization', 'Basic ' + base64_encode(':' + players[index].password));
	xhr.timeout = options.http.timeout;
	xhr.onload = function(e) {
		if (xhr.readyState == 4) {
{'fraction_non_alphanumeric': 0.15071972904318373, 'fraction_numerical': 0.01397121083827265, 'mean_word_length': 4.285234899328859, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '6924724', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1547, 'n_words': 303}