<commit_before>#include "duktape/DuktapeEngine.h"

std::string Susi::Duktape::susiJS = R"SUSIJS(

var susi = {  
	_consumerCallbacks: {}, 
	_processorCallbacks: {}, 
	_publishCallbacks: {},
	
	registerConsumer: function(topic,callback) { 
		var consumers = this._consumerCallbacks[topic] || []; 
		var id = this._genID(); 
		consumers.push({callback: callback, id: id}); 
		this._consumerCallbacks[topic] = consumers; 
		if(consumers.length == 1){ 
			_registerConsumer(topic); 
		} 
		return id; 
	},

	registerProcessor: function(topic,callback) { 
		var processors = this._processorCallbacks[topic] || []; 
		var id = this._genID(); 
		processors.push({callback: callback, id: id}); 
		this._processorCallbacks[topic] = processors; 
		if(processors.length == 1){ 
			_registerProcessor(topic); 
		} 
		return id; 
	},

	unregisterConsumer: function(id){ 
		for(var topic in this._consumerCallbacks){ 
			var callbacks = this._consumerCallbacks[topic]; 
			for(var i=0; i<callbacks.length; i++){ 
				if(callbacks[i].id === id){ 
					callbacks.splice(i,1); 
					if(callbacks.length === 0){ 
						delete this._consumerCallbacks[topic]; 
					} 
					return true; 
				} 
			} 
		} 
		return false; 
	},

	unregisterProcessor: function(id){ 
		for(var topic in this._processorCallbacks){ 
			var callbacks = this._processorCallbacks[topic]; 
			for(var i=0; i<callbacks.length; i++){ 
				if(callbacks[i].id === id){ 
					callbacks.splice(i,1); 
					if(callbacks.length === 0){ 
						delete this._processorCallbacks[topic]; 
					} 
					return true; 
				} 
			} 
		} 
		return false; 
	},

	publish: function(event,callback) { 
		if(event.id === undefined){ 
			event.id = ''+this._genID(); 
		} 
		if(callback !== undefined) { 
			this._publishCallbacks[event.id] = callback; 
		} 
		_publish(JSON.stringify(event)); 
	},

	ack: function(event){ 
		_ack(JSON.stringify(event)); 
	},

	//used by js to interact with c++ part
	_processConsumerEvent: function(event,topic){ 
		event = JSON.parse(event); 
		var consumers = this._consumerCallbacks[topic] || []; 
		if(consumers.length > 0){ 
			for (var i = consumers.length - 1; i >= 0; i--) { 
				consumers[i].callback(event); 
			} 
		} 
	},

	_processProcessorEvent: function(event,topic){ 
		event = JSON.parse(event); 
		Duktape.fin(event,function(event){ 
			susi.ack(event); 
		}); 
		var processors = this._processorCallbacks[topic] || []; 
		if(processors.length > 0){ 
			for (var i = processors.length - 1; i >= 0; i--) { 
				processors[i].callback(event); 
			} 
		} 
		Duktape.gc(); 
	},

	_processAck: function(event){ 
		event = JSON.parse(event); 
		var cb = this._publishCallbacks[event.id]; 
		if(cb !== undefined) { 
			delete this._publishCallbacks[event.id]; 
		} 
		cb(event); 
	},

	_genID: function(){ 
		return Math.floor(Math.random()*1000000000000); 
	} 
}; 

//called by c++ part
function _processConsumerEvent(event,topic){ 
	susi._processConsumerEvent(event,topic); 
}

function _processProcessorEvent(event,topic){ <commit_msg>[duktape] fixed bug in processAck;<commit_after>#include "duktape/DuktapeEngine.h"

std::string Susi::Duktape::susiJS = R"SUSIJS(

var susi = {  
	_consumerCallbacks: {}, 
	_processorCallbacks: {}, 
	_publishCallbacks: {},
	
	registerConsumer: function(topic,callback) { 
		var consumers = this._consumerCallbacks[topic] || []; 
		var id = this._genID(); 
		consumers.push({callback: callback, id: id}); 
		this._consumerCallbacks[topic] = consumers; 
		if(consumers.length == 1){ 
			_registerConsumer(topic); 
		} 
		return id; 
	},

	registerProcessor: function(topic,callback) { 
		var processors = this._processorCallbacks[topic] || []; 
		var id = this._genID(); 
		processors.push({callback: callback, id: id}); 
		this._processorCallbacks[topic] = processors; 
		if(processors.length == 1){ 
			_registerProcessor(topic); 
		} 
		return id; 
	},

	unregisterConsumer: function(id){ 
		for(var topic in this._consumerCallbacks){ 
			var callbacks = this._consumerCallbacks[topic]; 
			for(var i=0; i<callbacks.length; i++){ 
				if(callbacks[i].id === id){ 
					callbacks.splice(i,1); 
					if(callbacks.length === 0){ 
						delete this._consumerCallbacks[topic]; 
					} 
					return true; 
				} 
			} 
		} 
		return false; 
	},

	unregisterProcessor: function(id){ 
		for(var topic in this._processorCallbacks){ 
			var callbacks = this._processorCallbacks[topic]; 
			for(var i=0; i<callbacks.length; i++){ 
				if(callbacks[i].id === id){ 
					callbacks.splice(i,1); 
					if(callbacks.length === 0){ 
						delete this._processorCallbacks[topic]; 
					} 
					return true; 
				} 
			} 
		} 
		return false; 
	},

	publish: function(event,callback) { 
		if(event.id === undefined){ 
			event.id = ''+this._genID(); 
		} 
		if(callback !== undefined) { 
			this._publishCallbacks[event.id] = callback; 
		} 
		_publish(JSON.stringify(event)); 
	},

	ack: function(event){ 
		_ack(JSON.stringify(event)); 
	},

	//used by js to interact with c++ part
	_processConsumerEvent: function(event,topic){ 
		event = JSON.parse(event); 
		var consumers = this._consumerCallbacks[topic] || []; 
		if(consumers.length > 0){ 
			for (var i = consumers.length - 1; i >= 0; i--) { 
				consumers[i].callback(event); 
			} 
		} 
	},

	_processProcessorEvent: function(event,topic){ 
		event = JSON.parse(event); 
		Duktape.fin(event,function(event){ 
			susi.ack(event); 
		}); 
		var processors = this._processorCallbacks[topic] || []; 
		if(processors.length > 0){ 
			for (var i = processors.length - 1; i >= 0; i--) { 
				processors[i].callback(event); 
			} 
		} 
		Duktape.gc(); 
	},

	_processAck: function(event){ 
		event = JSON.parse(event); 
		var cb = this._publishCallbacks[event.id]; 
		if(cb !== undefined) { 
			cb(event); 
			delete this._publishCallbacks[event.id]; 
		} 
	},

	_genID: function(){ 
		return Math.floor(Math.random()*1000000000000); 
	} 
}; 

//called by c++ part
function _processConsumerEvent(event,topic){ 
	susi._processConsumerEvent(event,topic); 
}

function _processProcessorEvent(event,topic){ 
{'fraction_non_alphanumeric': 0.15510406342913777, 'fraction_numerical': 0.008919722497522299, 'mean_word_length': 3.478550295857988, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '4650507', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2074, 'n_words': 368}