<commit_before>/*
1000 => Attribute "action" with value "set" or "get" not present
1001 => "call" needs to be called with a "Consult" object
1002 => Attribute "action" not present or not a string
1003 => Attribute "widget" not present or incorrect
*/

/*
Consult pattern:

var consult = {
	'action'    : 'get',
	'widget'    : widgetID | 'global',
	'variables' : 'variable1'
}

var consult = {
	'action'    : 'get',
	'widget'    : widgetID | 'global',
	'variables' : ['variable1', ...]
}

var consult = {
	'action'    : 'set',
	'widget'    : widgetID | 'global',
	'variables' : {'variable1':'value1', ...}
}
*/

// 0 = get
// 1 = set
var API_F = (function(){
	var max_wait_GET_SET_request = [100, 100]; //ms
	
	var timeout_GET_SET = [0, 0];
	
	var callbacks_GET_SET_request = [[],[]];
	
	var next_GET_SET_request = [{}, {}];
	
	var precall = function(parameters, callback){
		var command = {
			'action':parameters['action'],
			'widgets':{}
		};
		command['widgets'][parameters['widget']] = parameters['variables'];
		
		call(command, callback);
	}
	
	var call = function(parameters, callback){
		if(parameters){
			if(parameters["action"] === 0 || parameters["action"] === 1){
				if(typeof parameters["widgets"] === "object"){
					// Una variable
					var mode = parameters["action"];
					var widgets = parameters["widgets"];
					for(var widget in widgets){
						if(typeof widgets[widget] === 'string'){
							widgets[widget] = [widgets[widget]];
						}
					}
					
					for(var widget in widgets){
						if(next_GET_SET_request[mode][widget] === undefined){
							next_GET_SET_request[mode][widget] = {};
						}
						for(var i in widgets[widget]){
							next_GET_SET_request[mode][widget][i] = widgets[widget][i];
						}
					}
					
					callbacks_GET_SET_request[mode].push({"callback":callback,"widgets":widgets});
					clearTimeout(timeout_GET_SET[mode]);
					timeout_GET_SET[mode] = mode === 0 ?
						setTimeout(execute_GET, max_wait_GET_SET_request[mode]) :
						setTimeout(execute_SET, max_wait_GET_SET_request[mode]);
				}
				else{
					callback(fail(1003));
				}
			}
			else{
				callback(fail(1002));
			}
		}
		else{
			callback(fail(1001));
		}
	}
	
	
	var fail = function(n){
		return {'response':'FAIL',"content":n};
	}
	
	var execute = function(action, next_request, callbacksConsulta){
		var req = new XMLHttpRequest();
		req.open('POST', 'api.php', true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.onreadystatechange = function(aEvt){
			if(req.readyState == 4){
				if(req.status == 200){
					//console.log(req.responseText);
					var response = JSON.parse(req.responseText);
					
					// Go over the callbacks and generate a response
					if(response['response']==='OK'){
						for(var i in callbacksConsulta){<commit_msg>Simplifying the net api<commit_after>/*
Consult pattern:

var consult = {
	'action'    : 'get',
	'widget'    : widgetID | 'global',
	'variables' : 'variable1'
}

var consult = {
	'action'    : 'get',
	'widget'    : widgetID | 'global',
	'variables' : ['variable1', ...]
}

var consult = {
	'action'    : 'set',
	'widget'    : widgetID | 'global',
	'variables' : {'variable1':'value1', ...}
}
*/

// 0 = get
// 1 = set
var API_F = (function(){
	var max_wait_GET_SET_request = [100, 100]; //ms
	var timeout_GET_SET = [0, 0];
	var callbacks_GET_SET_request = [[],[]];
	var next_GET_SET_request = [{}, {}];
	
	var precall = function(parameters, callback){
		var command = {
			'action':parameters['action'],
			'widgets':{}
		};
		command['widgets'][parameters['widget']] = parameters['variables'];
		
		call(command, callback);
	}
	
	var call = function(parameters, callback){
		if(
			parameters &&
			(parameters["action"] === 0 || parameters["action"] === 1) &&
			typeof parameters["widgets"] === "object"
		){
			// Una variable
			var mode = parameters["action"];
			var widgets = parameters["widgets"];
			for(var widget in widgets){
				if(typeof widgets[widget] === 'string'){
					widgets[widget] = [widgets[widget]];
				}
			}
			
			for(var widget in widgets){
				if(next_GET_SET_request[mode][widget] === undefined){
					next_GET_SET_request[mode][widget] = {};
				}
				for(var i in widgets[widget]){
					next_GET_SET_request[mode][widget][i] = widgets[widget][i];
				}
			}
			
			callbacks_GET_SET_request[mode].push({"callback":callback,"widgets":widgets});
			clearTimeout(timeout_GET_SET[mode]);
			timeout_GET_SET[mode] = mode === 0 ?
				setTimeout(execute_GET, max_wait_GET_SET_request[mode]) :
				setTimeout(execute_SET, max_wait_GET_SET_request[mode]);
		}
		else{
			callback(undefined);
		}
	}
	
	var execute = function(action, next_request, callbacksConsulta){
		var req = new XMLHttpRequest();
		req.open('POST', 'api.php', true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.onreadystatechange = function(aEvt){
			if(req.readyState == 4){
				if(req.status == 200){
					//console.log(req.responseText);
					var response = JSON.parse(req.responseText);
					
					// Go over the callbacks and generate a response
					if(response['response']==='OK'){
						for(var i in callbacksConsulta){
{'fraction_non_alphanumeric': 0.17240712074303405, 'fraction_numerical': 0.013544891640866873, 'mean_word_length': 3.648381294964029, 'pattern_counts': {'":': 5, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4188888', 'n_tokens_mistral': 2112, 'n_tokens_neox': 1712, 'n_words': 352}