Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Commit

Permalink
working on how the receiving updates are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakshan Perera committed Aug 25, 2010
1 parent f58a21f commit 7fa728c
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 25 deletions.
104 changes: 79 additions & 25 deletions public/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,15 @@ $(function() {
"#BABDFF", "#FFD4EB", "#AAFF75", "#FF9EAB", "#DCFF91", "#8088FF"
];
var assigned_colors = {};
var diff_queue = [];
var patching_process_running = false;
var update_queue = [];
var updating_process_running = false;
var playback_mode = false;
var take_diffs = true;

//Client Socket Methods
var socket = new WebSocket('ws://localhost:8080');
socket.onmessage = function(ev){
received_msg = JSON.parse(ev.data);
console.log(received_msg);

switch(received_msg["channel"]){
case "initial":
Expand All @@ -157,16 +156,16 @@ $(function() {
newChatMessage(received_msg["payload"]["user"], received_msg["payload"]["message"]);
break;
case "add_line":
if(received_msg["payload"]["user"] != user_id)
//addLine(received_msg["payload"]["user"]);
//store the update in the queue
update_queue.push(received_msg);
break;
case "modify_line":
if(received_msg["payload"]["user"] != user_id)
//modifyLine(received_msg["payload"]["user"]);
//store the update in the queue
update_queue.push(received_msg);
break;
case "remove_line":
if(received_msg["payload"]["user"] != user_id)
//removeLine(received_msg["payload"]["user"]);
//store the update in the queue
update_queue.push(received_msg);
break;
// case "diff":
// //store the diff in a queue
Expand Down Expand Up @@ -196,24 +195,77 @@ $(function() {
set_editable_content($('#editable_content'), 'hello\nim testing this');
}

// *Receiving updates*
var applyPatch = function(uid, patch){
//do the updates sequentially.
//updates are stored in a stack.
var checkForUpdates = function(){
if(update_queue.length > 0 && updating_process_running == false) {
var current_update = update_queue.shift();

if(!playback_mode && (current_update["payload"]["user"] == user_id))
return false;

applyUpdate(current_update["channel"], current_update["payload"]);
updating_process_running = true;
}
}

// perform received updates to the pad
// should send the action to perform - add line, modify line or remove line
// also all update parameters enclosed in a hash
var applyUpdate = function(action, update){
switch(action){
case "add_line":
addLine(update);
break;
case "modify_line":
modifyLine(update);
break;
case "remove_line":
removeLine(update);
break;
default:
console.log("invalid update");
};
// when function is fired assign current text in editable area to a variable
current_text = get_editable_content(); //$("#editable_content").text();
patch_worker.postMessage([uid, patch, current_text]);
//current_text = get_editable_content(); //$("#editable_content").text();
//patch_worker.postMessage([uid, patch, current_text]);
};

var checkForPatches = function(){
if(diff_queue.length > 0 && patching_process_running == false) {
current_patch = diff_queue.shift();

if(!playback_mode && (current_patch["user"] == user_id))
return false;
//To add a line we need:
//it's uuid, previous line uuid and next line uuid and content
var addLine = function(update){
//find the line with next uuid
//insert before next uuid
//apply syntax highlighting
//highlight the line
//else find the line with previous uuid
//insert after previous uuid
//apply syntax highlighting
//highlight the line
// insert as the first line
//apply syntax highlighting
//highlight the line
};

applyPatch(current_patch["user"], current_patch["patch"]);
patching_process_running = true;
}
}
//To modify a line we need:
// the uuid of the line and diff
var modifyLine = function(update){
//find the line with uuid

// get the line content
//
// send the uuid, line content and diff to patch worker
};

//To remove a line we need:
// the uuid of the line
var removeLine = function(update){
//find the line with uuid

//highlight the line

// remove the line from the pad
};

$("#editable_content").keydown(function(ev){
//don't delete the beyond p
Expand Down Expand Up @@ -355,7 +407,8 @@ $(function() {
stored_lines[new_uuid] = {"content": content}

//send 'add line' message to server
socket.send('{"type": "add_line", "uuid":' + JSON.stringify(new_uuid) + ', "previous_uuid":' + JSON.stringify(prev_uuid) + ', "next_uuid":' + JSON.stringify(next_uuid) + ', "message":' + JSON.stringify(content) + '}');
var line_msg = { "uuid": new_uuid, "previous_uuid": prev_uuid, "next_uuid": next_uuid, "content": content }
socket.send('{"type": "add_line", message:' + JSON.stringify(line_msg) + '}');
console.log(stored_lines[new_uuid]);
}

Expand Down Expand Up @@ -388,7 +441,8 @@ $(function() {
delete stored_lines[this];

//send 'remove line' message to server
socket.send('{"type": "remove_line", "uuid":' + JSON.stringify(this) + '}');
var line_msg = {"uuid": this}
socket.send('{"type": "remove_line", "message":' + JSON.stringify(line_msg) + '}');
console.log("removed line" + this);
});
}
Expand Down
142 changes: 142 additions & 0 deletions public/javascripts/jquery.timers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
jQuery.fn.extend({
everyTime: function(interval, label, fn, times, belay) {
return this.each(function() {
jQuery.timer.add(this, interval, label, fn, times, belay);
});
},
oneTime: function(interval, label, fn) {
return this.each(function() {
jQuery.timer.add(this, interval, label, fn, 1);
});
},
stopTime: function(label, fn) {
return this.each(function() {
jQuery.timer.remove(this, label, fn);
});
}
});

jQuery.extend({
timer: {
guid: 1,
global: {},
regex: /^([0-9]+)\s*(.*s)?$/,
powers: {
// Yeah this is major overkill...
'ms': 1,
'cs': 10,
'ds': 100,
's': 1000,
'das': 10000,
'hs': 100000,
'ks': 1000000
},
timeParse: function(value) {
if (value == undefined || value == null)
return null;
var result = this.regex.exec(jQuery.trim(value.toString()));
if (result[2]) {
var num = parseInt(result[1], 10);
var mult = this.powers[result[2]] || 1;
return num * mult;
} else {
return value;
}
},
add: function(element, interval, label, fn, times, belay) {
var counter = 0;

if (jQuery.isFunction(label)) {
if (!times)
times = fn;
fn = label;
label = interval;
}

interval = jQuery.timer.timeParse(interval);

if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
return;

if (times && times.constructor != Number) {
belay = !!times;
times = 0;
}

times = times || 0;
belay = belay || false;

if (!element.$timers)
element.$timers = {};

if (!element.$timers[label])
element.$timers[label] = {};

fn.$timerID = fn.$timerID || this.guid++;

var handler = function() {
if (belay && this.inProgress)
return;
this.inProgress = true;
if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
jQuery.timer.remove(element, label, fn);
this.inProgress = false;
};

handler.$timerID = fn.$timerID;

if (!element.$timers[label][fn.$timerID])
element.$timers[label][fn.$timerID] = window.setInterval(handler,interval);

if ( !this.global[label] )
this.global[label] = [];
this.global[label].push( element );

},
remove: function(element, label, fn) {
var timers = element.$timers, ret;

if ( timers ) {

if (!label) {
for ( label in timers )
this.remove(element, label, fn);
} else if ( timers[label] ) {
if ( fn ) {
if ( fn.$timerID ) {
window.clearInterval(timers[label][fn.$timerID]);
delete timers[label][fn.$timerID];
}
} else {
for ( var fn in timers[label] ) {
window.clearInterval(timers[label][fn]);
delete timers[label][fn];
}
}

for ( ret in timers[label] ) break;
if ( !ret ) {
ret = null;
delete timers[label];
}
}

for ( ret in timers ) break;
if ( !ret )
element.$timers = null;
}
}
}
});

if (jQuery.browser.msie)
jQuery(window).one("unload", function() {
var global = jQuery.timer.global;
for ( var label in global ) {
var els = global[label], i = els.length;
while ( --i )
jQuery.timer.remove(els[i], label);
}
});


0 comments on commit 7fa728c

Please sign in to comment.