<commit_before>/*global WorkerGlobalScope, self*/

var easy5 = {

	Worker: function(){

		var constants = {
			EASY5_FUNCTION_PREFIX: 'easy5.FUNCTION:'
		};

		var isSelfWorker = function(){ // check if code is running inside Worker
			return typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
		};

		var args = Array.prototype.slice.call(arguments), // convert arguments passed to constructor into standard JS array
			callbacks = [], // stores functions exchanged between Web Worker and its parent
			freeCallbacks = [], // stores callbacks free keys
			config = {}, // default config value
			jsonRpcConfig = {}, // default jsonRpcConfig value
			worker; // Web Worker or window object (see line 30)

		if(args.length > 1){ // check which version of constructor is used
			config = args[0];
			jsonRpcConfig = args[1];
		} else{
			jsonRpcConfig = args[0];
		}

		jsonRpcConfig.local = jsonRpcConfig.local || {};
		jsonRpcConfig.remote = jsonRpcConfig.remote || {};

		worker = config.worker && !isSelfWorker() ? new Worker(config.worker) : self; // check if code is not running inside Web Worker and there's config.worker property; if so - set worker to new Worker object, otherwise - set it to self (WorkerGlobalScope)

		var returnResult = function(id, result, called){ // return result from Web Worker to parent or vice versa using JSON-RPC protocol; "called" argument is true when function is called from code, not by returning result using "return" keyword
			worker.postMessage({
				result: called ? result[0] : result,
				error: null,
				id: id
			});
		};

		worker.addEventListener('message', function(e){
			if(e.data.hasOwnProperty('method') && jsonRpcConfig.local.hasOwnProperty(e.data.method)){
				for(var i in e.data.params){
					if(e.data.params.hasOwnProperty(i) && typeof e.data.params[i] === "string" && e.data.params[i].indexOf(constants.EASY5_FUNCTION_PREFIX) === 0){ // check if arg[i] should be converted to function
						var id = e.data.params[i].split(':').pop();
						e.data.params[i] = function(){
							returnResult(id, Array.prototype.slice.call(arguments), true);
						};
					}
				}
				var result = jsonRpcConfig.local[e.data.method].apply(null, e.data.params);
				if(result && e.data.hasOwnProperty('id')){ // check if local function returned any data and we know where to hand on it
					returnResult(e.data.id, result);
				}
			} else if(e.data.hasOwnProperty('result') && callbacks[e.data.id]){
				callbacks[e.data.id].apply(null, [e.data.result]);
				callbacks[e.data.id] = null; // function has been already called so we can delete it from callbacks array
				freeCallbacks.push(e.data.id); // add e.data.id to freeCallbacks "cache"; next time callback won't be added to the end of callbacks array, but placed in free space; it prevents callbacks array to be like [null, null, ..., null, some_function]
			}
		}, false);

		var call = function(name, args){ // "call" remote function
			args = Array.prototype.slice.call(args); // convert arguments into standard JS array
			var id = null; // set id for response to null (default)
			if(jsonRpcConfig.remote[name].hasOwnProperty('return') && jsonRpcConfig.remote[name].return === true){ // check if remote function is returning result using "return" keyword
				if(freeCallbacks.length > 0){ // check if there are free callbacks keys
					id = freeCallbacks.shift();
					callbacks[id] = args.pop();<commit_msg>update one comment<commit_after>/*global WorkerGlobalScope, self*/

var easy5 = {

	Worker: function(){

		var constants = {
			EASY5_FUNCTION_PREFIX: 'easy5.FUNCTION:'
		};

		var isSelfWorker = function(){ // check if code is running inside Worker
			return typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
		};

		var args = Array.prototype.slice.call(arguments), // convert arguments passed to constructor into standard JS array
			callbacks = [], // stores functions exchanged between Web Worker and its parent
			freeCallbacks = [], // stores callbacks free keys
			config = {}, // default config value
			jsonRpcConfig = {}, // default jsonRpcConfig value
			worker; // Web Worker or window object (see line 30)

		if(args.length > 1){ // check which version of constructor is used
			config = args[0];
			jsonRpcConfig = args[1];
		} else{
			jsonRpcConfig = args[0];
		}

		jsonRpcConfig.local = jsonRpcConfig.local || {};
		jsonRpcConfig.remote = jsonRpcConfig.remote || {};

		worker = config.worker && !isSelfWorker() ? new Worker(config.worker) : self; // check if code is not running inside Web Worker and there's config.worker property; if so - set worker to new Worker object, otherwise - set it to self (WorkerGlobalScope)

		var returnResult = function(id, result, called){ // return result from Web Worker to parent or vice versa using JSON-RPC protocol; "called" argument is true when function is called from code, not by returning result using "return" keyword
			worker.postMessage({
				result: called ? result[0] : result,
				error: null,
				id: id
			});
		};

		worker.addEventListener('message', function(e){
			if(e.data.hasOwnProperty('method') && jsonRpcConfig.local.hasOwnProperty(e.data.method)){
				for(var i in e.data.params){
					if(e.data.params.hasOwnProperty(i) && typeof e.data.params[i] === "string" && e.data.params[i].indexOf(constants.EASY5_FUNCTION_PREFIX) === 0){ // check if arg[i] should be converted to function
						var id = e.data.params[i].split(':').pop();
						e.data.params[i] = function(){
							returnResult(id, Array.prototype.slice.call(arguments), true);
						};
					}
				}
				var result = jsonRpcConfig.local[e.data.method].apply(null, e.data.params);
				if(result && e.data.hasOwnProperty('id')){ // check if local function returned any data and we know where to hand on it
					returnResult(e.data.id, result);
				}
			} else if(e.data.hasOwnProperty('result') && callbacks[e.data.id]){
				callbacks[e.data.id].apply(null, [e.data.result]);
				callbacks[e.data.id] = null; // function has been already called so we can delete it from callbacks array
				freeCallbacks.push(e.data.id); // add e.data.id to freeCallbacks "cache"; next time callback won't be added to the end of callbacks array, but placed in free space; it prevents callbacks array to be like [null, null, ..., null, some_function]
			}
		}, false);

		var call = function(name, args){ // "call" remote function
			args = Array.prototype.slice.call(args); // convert arguments into standard JS array
			var id = null; // set id for response to null (default)
			if(jsonRpcConfig.remote[name].hasOwnProperty('return') && jsonRpcConfig.remote[name].return === true){ // check if remote function should return result using "return" keyword
				if(freeCallbacks.length > 0){ // check if there are free callbacks keys
					id = freeCallbacks.shift();
					callbacks[id] = args.pop();
{'fraction_non_alphanumeric': 0.12764722947490573, 'fraction_numerical': 0.0037713954163040323, 'mean_word_length': 3.7000681663258352, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4238194', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2002, 'n_words': 657}