<commit_before>import {WSMessage} from "../message/wsmessage";
import {WS,wsOpts} from "./websocket";
declare var console;
export class WSChannel {
	private ws;
	private messageListeners;
	private eventListeners;
	constructor(wsAddress) {
	    this.ws = new WS(wsAddress,undefined,wsOpts);
	    this.messageListeners = {};
	    this.eventListeners = {};
	    this.ws.onmessage = (result) =>{
	    	var data = JSON.parse(result.data);	    	
	    	//normal responses
	    	if(data.method!="onEvent" && typeof data.error === "undefined") {
	    		var id = data.id;
		    	this.messageListeners[id]["resolve"](data);
		    	delete this.messageListeners[id];	
	    	}
	    	//errors
	    	else if(typeof data.error !== "undefined") {
	    		var id = data.id;
	    		this.messageListeners[id]["reject"](data);
	    		delete this.messageListeners[id];
	    	}
	    	//events
	    	else {
	    		var index = data.params.value.object + "|" + data.params.value.type;
	    		for(var i in this.eventListeners[index]) {
					this.eventListeners[index][i](data.params.value.data.candidate);
				}
	    	}
	    }
	}

	public on(objectId,methodName,callback) {
		var index = objectId+"|"+methodName;
		if(typeof this.eventListeners[index] === "undefined") {
			this.eventListeners[index] = [];
		}
		this.eventListeners[index].push(callback);
	}

	public send(data:WSMessage):Promise<any> {
		var id = data.id;
		var t = this;
		return new Promise((resolve,reject) => {
			try {
				t.ws.send(JSON.stringify(data));
				t.messageListeners[id] = {resolve:resolve,reject:reject};
			}
			catch(e) {
				reject(e);
			}
			setTimeout(()=>{
				reject("timeoutError");
				delete t.messageListeners[id];
			},10000);
		});
	}
}<commit_msg>added MiniMQ to the WSChannel, so that KCL will be more resilient to intermittent websocket disconnections<commit_after>import {WSMessage} from "../message/wsmessage";
import {MiniMQ} from "minimq";
import {WS,wsOpts} from "./websocket";
declare var console;
export class WSChannel {
	private ws;
	private messageListeners;
	private eventListeners;
	private queue;
	constructor(wsAddress) {
		this.messageListeners = {};
	    this.eventListeners = {};

		this.queue = new MiniMQ();
		this.queue.handlerFunction = (el,prm,resolve,reject) => {
			try {
				this.ws.send(JSON.stringify(el));
				this.messageListeners[el.id] = {resolve:resolve,reject:reject};
			}
			catch(e) {
				reject(e);
			}
			setTimeout(()=>{
				reject("timeoutError");
				delete this.messageListeners[el.id];
			},10000);
		}

	    this.ws = new WS(wsAddress,undefined,wsOpts);
	    this.ws.onopen = () => {
	    	this.queue.openQueue();
	    };
	    this.ws.onclose = () => {
	    	this.queue.closeQueue();
	    }
	    this.ws.onmessage = (result) =>{
	    	var data = JSON.parse(result.data);	    	
	    	//normal responses
	    	if(data.method!="onEvent" && typeof data.error === "undefined") {
	    		var id = data.id;
		    	this.messageListeners[id]["resolve"](data);
		    	delete this.messageListeners[id];	
	    	}
	    	//errors
	    	else if(typeof data.error !== "undefined") {
	    		var id = data.id;
	    		this.messageListeners[id]["reject"](data);
	    		delete this.messageListeners[id];
	    	}
	    	//events
	    	else {
	    		var index = data.params.value.object + "|" + data.params.value.type;
	    		for(var i in this.eventListeners[index]) {
					this.eventListeners[index][i](data.params.value.data.candidate);
				}
	    	}
	    }
	}

	public on(objectId,methodName,callback) {
		var index = objectId+"|"+methodName;
		if(typeof this.eventListeners[index] === "undefined") {
			this.eventListeners[index] = [];
		}
		this.eventListeners[index].push(callback);
	}

	public send(data:WSMessage):Promise<any> {
		return this.queue.addElement(data);
	}
}
{'fraction_non_alphanumeric': 0.15557911476278824, 'fraction_numerical': 0.0026504108136761197, 'mean_word_length': 3.0932754880694144, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '7450227', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1283, 'n_words': 217}