<commit_before>(function($) {
	
	function isBinary(data) {
		var string = Object.prototype.toString.call(data);
		return string === "[object Blob]" || string === "[object ArrayBuffer]";
	}
	
	function param(url, name) {
		var match = new RegExp("[?&]" + name + "=([^&]+)").exec(url);
		return match ? decodeURIComponent(match[1]) : null;
	}
	
	// The server-side view of the socket handling
	$.socket.transports.test = function(socket) {
		var // Is it accepted?
			accepted,
			// Connection event helper
			connectionEvent;
		
		return {
			open: function() {
				var // Heartbeat
					heartbeat,
					heartbeatTimer,
					// Connection object for the server
					connection = {
						send: function(event, data) {
							setTimeout(function() {
								if (accepted) {
									if (data === undefined) {
										data = event;
										event = "message";
									}
									
									socket.notify(isBinary(data) ? data : $.stringifyJSON({type: event, data: data}));
								}
							}, 5);
							return this;
						},
						close: function() {
							setTimeout(function() {
								if (accepted) {
									socket.fire("close", ["done"]);
									connectionEvent.triggerHandler("close", [1000, null]);
								}
							}, 5);
							return this;
						},
						on: function(type, fn) {
							connectionEvent.on(type, function() {
								fn.apply(connection, Array.prototype.slice.call(arguments, 1));
							});
							return this;
						}
					},
					// Request object for the server
					request = {
						accept: function() {
							accepted = true;
							connectionEvent = connectionEvent || $({});
							return connection;
						},
						reject: function() {
							accepted = false;
						}
					};
				
				function resetHeartbeatTimer() {
					if (heartbeatTimer) {
						clearTimeout(heartbeatTimer);
					}
					
					heartbeatTimer = setTimeout(function() {
						socket.close("error");
					}, heartbeat);
				}
				
				accepted = connectionEvent = undefined;
				if (socket.options.server) {
					socket.options.server(request);
				}
				
				setTimeout(function() {
					switch (accepted) {
					case true:
						heartbeat = param(socket.data("url"), "heartbeat");
						if (heartbeat > 0) {
							resetHeartbeatTimer();
							connectionEvent.on("heartbeat", function() {
								resetHeartbeatTimer();
								connection.send("heartbeat", null);
							});
						}
						
						socket.fire("open");
						connectionEvent.triggerHandler("open");
						break;
					case false:<commit_msg>Transport should cancel the heartbeat timer when the connection is closed<commit_after>(function($) {
	
	function isBinary(data) {
		var string = Object.prototype.toString.call(data);
		return string === "[object Blob]" || string === "[object ArrayBuffer]";
	}
	
	function param(url, name) {
		var match = new RegExp("[?&]" + name + "=([^&]+)").exec(url);
		return match ? decodeURIComponent(match[1]) : null;
	}
	
	// The server-side view of the socket handling
	$.socket.transports.test = function(socket) {
		var // Is it accepted?
			accepted,
			// Connection event helper
			connectionEvent;
		
		return {
			open: function() {
				var // Heartbeat
					heartbeat,
					heartbeatTimer,
					// Connection object for the server
					connection = {
						send: function(event, data) {
							setTimeout(function() {
								if (accepted) {
									if (data === undefined) {
										data = event;
										event = "message";
									}
									
									socket.notify(isBinary(data) ? data : $.stringifyJSON({type: event, data: data}));
								}
							}, 5);
							return this;
						},
						close: function() {
							setTimeout(function() {
								if (accepted) {
									socket.fire("close", ["done"]);
									connectionEvent.triggerHandler("close", [1000, null]);
								}
							}, 5);
							return this;
						},
						on: function(type, fn) {
							connectionEvent.on(type, function() {
								fn.apply(connection, Array.prototype.slice.call(arguments, 1));
							});
							return this;
						}
					},
					// Request object for the server
					request = {
						accept: function() {
							accepted = true;
							connectionEvent = connectionEvent || $({});
							return connection;
						},
						reject: function() {
							accepted = false;
						}
					};
				
				function resetHeartbeatTimer() {
					if (heartbeatTimer) {
						clearTimeout(heartbeatTimer);
					}
					
					heartbeatTimer = setTimeout(function() {
						socket.close("error");
					}, heartbeat);
				}
				
				accepted = connectionEvent = undefined;
				if (socket.options.server) {
					socket.options.server(request);
				}
				
				setTimeout(function() {
					switch (accepted) {
					case true:
						heartbeat = param(socket.data("url"), "heartbeat");
						if (heartbeat > 0) {
							resetHeartbeatTimer();
							connectionEvent.on("heartbeat", function() {
								resetHeartbeatTimer();
								connection.send("heartbeat", null);
							})
							.on("close", function() {
								if (heartbeatTimer) {
									clearTimeout(heartbeatTimer);
								}
							});
						}
						
						socket.fire("open");
						connectionEvent.triggerHandler("open");
						break;
					case false:
{'fraction_non_alphanumeric': 0.1399846801991574, 'fraction_numerical': 0.0034469551895825352, 'mean_word_length': 2.1388221153846154, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5304700', 'n_tokens_mistral': 2384, 'n_tokens_neox': 1462, 'n_words': 328}