<commit_before>dojo.require("dojox.cometd");

var room = {
    _last: "",
    _username: null,

    join: function(name){
        
        if(name == null || name.length==0 ){
            alert('Please enter a username!');
        }else{
		
            dojox.cometd.init(new String(document.location).replace(/http:\/\/[^\/]*/,'').replace(/\/examples\/.*$/,'')+"/cometd");
            // dojox.cometd.init("http://127.0.0.1:8080/cometd");
		
            this._username=name;
            dojo.byId('join').className='hidden';
            dojo.byId('joined').className='';
            dojo.byId('phrase').focus();
	    
            // subscribe and join
	    dojox.cometd.startBatch();
            dojox.cometd.subscribe("/chat/demo", room, "_chat");
            dojox.cometd.publish("/chat/demo", { user: room._username, join: true, chat : room._username+" has joined"});
	    dojox.cometd.endBatch();
	    
            // handle cometd failures while in the room
            room._meta=dojo.subscribe("/cometd/meta",function(event){
                console.debug(event);   
                if (event.action=="handshake") {
	            room._chat({data:{join:true,user:"SERVER",chat:"reconnected"}});
                    dojox.cometd.subscribe("/chat/demo", room, "_chat");
                } else if (event.action=="connect" && !event.successful) {
                    room._chat({data:{leave:true,user:"SERVER",chat:"disconnected!"}});
	        }
            });
        }
    },

    leave: function(){
        if (room._username==null)
            return;
	    
	if (room._meta)
            dojo.unsubscribe(room._meta);
	room._meta=null;
	
	dojox.cometd.startBatch();
        dojox.cometd.unsubscribe("/chat/demo", room, "_chat");
        dojox.cometd.publish("/chat/demo", { user: room._username, leave: true, chat : room._username+" has left"});
	dojox.cometd.endBatch();

        // switch the input form
        dojo.byId('join').className='';
        dojo.byId('joined').className='hidden';
        dojo.byId('username').focus();
        room._username=null;
        dojox.cometd.disconnect();
    },
      
    chat: function(text){
        if(!text || !text.length){ return false; }
        dojox.cometd.publish("/chat/demo", { user: room._username, chat: text});
    },

    _chat: function(message){
        var chat=dojo.byId('chat');
        if(!message.data){
            alert("bad message format "+message);
            return;
        }
        var from=message.data.user;
        var special=message.data.join || message.data.leave;
        var text=message.data.chat;
        if(!text){ return; }

        if( !special && from == room._last ){
            from="...";
        }else{
            room._last=from;
            from+=":";
        }

        if(special){
            chat.innerHTML += "<span class=\"alert\"><span class=\"from\">"+from+"&nbsp;</span><span class=\"text\">"+text+"</span></span><br/>";
            room._last="";
        }else{
            chat.innerHTML += "<span class=\"from\">"+from+"&nbsp;</span><span class=\"text\">"+text+"</span><br/>";
        } 
        chat.scrollTop = chat.scrollHeight - chat.clientHeight;    
    },
  
  _init: function(){
        dojo.byId('join').className='';
        dojo.byId('joined').className='hidden';
        dojo.byId('username').focus();
	
        var element=dojo.byId('username');
        element.setAttribute("autocomplete","OFF"); 
        dojo.connect(element, "onkeyup", function(e){   
            if(e.keyCode == dojo.keys.ENTER){
                room.join(dojo.byId('username').value);
                return false;
            }
            return true;
	});
  
        element=dojo.byId('joinB');
        element.onclick = function(){
            room.join(dojo.byId('username').value);
            return false;
	}
  
        element=dojo.byId('phrase');
        element.setAttribute("autocomplete","OFF");
        dojo.connect(element, "onkeyup", function(e){   
            if(e.keyCode == dojo.keys.ENTER){
                room.chat(dojo.byId('phrase').value);
                dojo.byId('phrase').value='';
                return false;
            }
            return true;
	});
  
        element=dojo.byId('sendB');
        element.onclick = function(){
          room.chat(dojo.byId('phrase').value);
          dojo.byId('phrase').value='';
	}
  
        element=dojo.byId('leaveB');
        element.onclick = function(){
          room.leave();
	}
    } 
};

dojo.addOnLoad(room, "_init");
dojo.addOnUnload(room,"leave");
<commit_msg>Major refactor for optimization and extension<commit_after>dojo.require("dojox.cometd");
dojo.require("dojox.cometd.timesync");

var room = {
    _last: "",
    _username: null,

    join: function(name){
        
        if(name == null || name.length==0 ){
            alert('Please enter a username!');
        }else{
		
            dojox.cometd.init(new String(document.location).replace(/http:\/\/[^\/]*/,'').replace(/\/examples\/.*$/,'')+"/cometd");
            // dojox.cometd.init("http://127.0.0.2:8080/cometd");
		
            this._username=name;
            dojo.byId('join').className='hidden';
            dojo.byId('joined').className='';
            dojo.byId('phrase').focus();
	    
            // subscribe and join
	    dojox.cometd.startBatch();
            dojox.cometd.subscribe("/chat/demo", room, "_chat");
            dojox.cometd.publish("/chat/demo", { user: room._username, join: true, chat : room._username+" has joined"});
	    dojox.cometd.endBatch();
	    
            // handle cometd failures while in the room
            room._meta=dojo.subscribe("/cometd/meta",function(event){
                console.debug(event);   
                if (event.action=="handshake") {
	            room._chat({data:{join:true,user:"SERVER",chat:"reconnected"}});
                    dojox.cometd.subscribe("/chat/demo", room, "_chat");
                } else if (event.action=="connect" && !event.successful) {
                    room._chat({data:{leave:true,user:"SERVER",chat:"disconnected!"}});
	        }
            });
        }
    },

    leave: function(){
        if (room._username==null)
            return;
	    
	if (room._meta)
            dojo.unsubscribe(room._meta);
	room._meta=null;
	
	dojox.cometd.startBatch();
        dojox.cometd.unsubscribe("/chat/demo", room, "_chat");
        dojox.cometd.publish("/chat/demo", { user: room._username, leave: true, chat : room._username+" has left"});
	dojox.cometd.endBatch();

        // switch the input form
        dojo.byId('join').className='';
        dojo.byId('joined').className='hidden';
        dojo.byId('username').focus();
        room._username=null;
        dojox.cometd.disconnect();
    },
      
    chat: function(text){
        if(!text || !text.length){ return false; }
        dojox.cometd.publish("/chat/demo", { user: room._username, chat: text});
    },

    _chat: function(message){
        var chat=dojo.byId('chat');
        if(!message.data){
            alert("bad message format "+message);
            return;
        }
        var from=message.data.user;
        var special=message.data.join || message.data.leave;
        var text=message.data.chat;
        if(!text){ return; }

        if( !special && from == room._last ){
            from="...";
        }else{
            room._last=from;
            from+=":";
        }

        if(special){
            chat.innerHTML += "<span class=\"alert\"><span class=\"from\">"+from+"&nbsp;</span><span class=\"text\">"+text+"</span></span><br/>";
            room._last="";
        }else{
            chat.innerHTML += "<span class=\"from\">"+from+"&nbsp;</span><span class=\"text\">"+text+"</span><br/>";
        } 
        chat.scrollTop = chat.scrollHeight - chat.clientHeight;    
    },
  
  _init: function(){
        dojo.byId('join').className='';
        dojo.byId('joined').className='hidden';
        dojo.byId('username').focus();
	
        var element=dojo.byId('username');
        element.setAttribute("autocomplete","OFF"); 
        dojo.connect(element, "onkeyup", function(e){   
            if(e.keyCode == dojo.keys.ENTER){
                room.join(dojo.byId('username').value);
                return false;
            }
            return true;
	});
  
        element=dojo.byId('joinB');
        element.onclick = function(){
            room.join(dojo.byId('username').value);
            return false;
	}
  
        element=dojo.byId('phrase');
        element.setAttribute("autocomplete","OFF");
        dojo.connect(element, "onkeyup", function(e){   
            if(e.keyCode == dojo.keys.ENTER){
                room.chat(dojo.byId('phrase').value);
                dojo.byId('phrase').value='';
                return false;
            }
            return true;
	});
  
        element=dojo.byId('sendB');
        element.onclick = function(){
          room.chat(dojo.byId('phrase').value);
          dojo.byId('phrase').value='';
	}
  
        element=dojo.byId('leaveB');
        element.onclick = function(){
          room.leave();
	}
    } 
};

dojo.addOnLoad(room, "_init");
dojo.addOnUnload(room,"leave");

{'fraction_non_alphanumeric': 0.18199672667757774, 'fraction_numerical': 0.0024004364429896343, 'mean_word_length': 1.99934554973822, 'pattern_counts': {'":': 2, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1164281', 'n_tokens_mistral': 2883, 'n_tokens_neox': 2801, 'n_words': 394}