From 6c2fefb1ad355f20619663f046f79644df03d28e Mon Sep 17 00:00:00 2001 From: Curtis SerVaas Date: Fri, 10 Oct 2014 07:52:04 -0400 Subject: [PATCH] Semi-Naive Real-Time-Editing Works (as far as I can tell). Todos= CursorLogic+AuthorSync --- lib/MessageQueue.js | 19 ++++++++++++------- lib/helperLib.js | 19 +++++++++++++------ models/Node.js | 11 +++++++---- public/js/backbone/router.js | 19 +++++++++++++------ public/js/libs/myLogic/addNode.js | 9 ++++++++- 5 files changed, 53 insertions(+), 24 deletions(-) diff --git a/lib/MessageQueue.js b/lib/MessageQueue.js index 353f885..a5f4652 100644 --- a/lib/MessageQueue.js +++ b/lib/MessageQueue.js @@ -76,15 +76,20 @@ function addNode(data, socket){ var now = Date.now(); - var callback = function(err, instance, now){ - socket.emit("updateReceived", [modelJson._id ,instance, data[0]]); //(update the ID) - socket.broadcast.emit("newNode", [ [parId,newIndex] , instance ]); + var callback = function(err, instance, now){ Node.updateParent(parId, instance._id ,newIndex, now ); } + + Node.addNode(modelJson.text, modelJson.children, modelJson.parents, modelJson.author._id, modelJson._id ,callback); + + + // var callback = function(err, instance, now){ + // socket.emit("updateReceived", [modelJson._id ,instance, data[0]]); //(update the ID) + // socket.broadcast.emit("newNode", [ [parId,newIndex] , instance ]); - Node.updateParent(parId, instance._id ,newIndex, now ); - } + // Node.updateParent(parId, instance._id ,newIndex, now ); + // } - // console.log("newNode"); - Node.addNode(modelJson.text, modelJson.children, modelJson.parents, modelJson.author._id ,callback); + // // console.log("newNode"); + // Node.addNode(modelJson.text, modelJson.children, modelJson.parents, modelJson.author._id ,callback); } diff --git a/lib/helperLib.js b/lib/helperLib.js index 3b74f95..5ee9b26 100644 --- a/lib/helperLib.js +++ b/lib/helperLib.js @@ -46,7 +46,8 @@ module.exports = { }); socket.on('nodeRequest', function(data){ - var nodes = Node.findAndSocketSend(socket); //finds, then sends through socket. + console.log("NodeRequst, CURRENT_TIMESTAMP", CURRENT_TIMESTAMP); + var nodes = Node.findAndSocketSend(socket, CURRENT_TIMESTAMP); //finds, then sends through socket. }); socket.on("revHistoryRequest", function(subRootId){ @@ -129,25 +130,31 @@ function attachLogInListeners(socket){ } else{ CURRENT_TIMESTAMP = timestampData[0]; - socket.broadcast.emit(emitData[0], emitData[1]); - MessageQueue.queueOrDo(action); + socket.broadcast.emit(emitData[0], emitData[1], CURRENT_TIMESTAMP); + MessageQueue.queueOrDo(queueData); } } //maybe emit sooner... //need to broadcast parentArray. //timestampData=[CURRENT_TIMESTAMP, oldTime] socket.on("newNode", function(data, timestampData){ - syncValidation(data, timestampData, ['newNode', data, socket], ["dummy", 1234]); + var queueData = ['newNode', data, socket]; + var emitData = ['newNode', data]; + syncValidation(data, timestampData, queueData, emitData); //(todo = set IDs on client (instead of negative Ids)); }); socket.on("removeNode", function(data, timestampData){ - syncValidation(data, timestampData, ['removeNode', data, socket], ["removeNode", data]); + var queueData = ['removeNode', data, socket]; + var emitData = ["removeNode", data]; + syncValidation(data, timestampData, queueData, emitData); }); socket.on("movedNode", function(data, timestampData){ - syncValidation(data, timestampData, ['movedNode', data, socket],[ "movedNode", [data[0],data[2]] ]); + var queueData = ['movedNode', data, socket]; + var emitData = [ "movedNode", [data[0],data[2]] ]; + syncValidation(data, timestampData, queueData, emitData); }); } diff --git a/models/Node.js b/models/Node.js index 06f9aaa..ced97fe 100644 --- a/models/Node.js +++ b/models/Node.js @@ -7,7 +7,8 @@ var NodeSchema = new mongoose.Schema({ parents: {type: Array}, markdown: {type: Boolean}, timestamp: {type: Number}, - author: {type: String, ref: 'User'} //authorId is populated with limited AuthorObject + author: {type: String, ref: 'User'}, //authorId is populated with limited AuthorObject + _id: String }); var MyNode = mongoose.model('nodes', NodeSchema); @@ -62,8 +63,9 @@ function setUpDB(){ MySnap.remove({}, function(err) { console.log('collection removed') }); var curtisId = "53e4079cd7dbc73d16c87c53"; - addNode("0root", [], ["123456"], curtisId ,function(err, rootNode){ - addNode("Welcolme!", [], [rootNode._id], curtisId ,function(err, firstBullet){ + addNode("0root", [], ["123456"], curtisId , "a42a" , function(err, rootNode){ + console.log("rootNode", rootNode); + addNode("Welcolme!", [], [rootNode._id], curtisId , "b42b", function(err, firstBullet){ rootNode.children = [firstBullet._id] var now = rootNode.timestamp = firstBullet.timestamp addSnap(rootNode, now); @@ -191,7 +193,7 @@ function updateParent(parId, newId ,newIndex,now){ } //add Node to the DB. -function addNode(text, children, parents, authorId, callback){ +function addNode(text, children, parents, authorId, ID ,callback){ var instance = new MyNode(); instance.text = text; instance.children = children; @@ -199,6 +201,7 @@ function addNode(text, children, parents, authorId, callback){ instance.markdown = 0; instance.timestamp = Date.now(); instance.authorId = authorId; + instance._id = ID; instance.save(function (err) { if (err) { diff --git a/public/js/backbone/router.js b/public/js/backbone/router.js index 2c7f8a4..37aa46b 100644 --- a/public/js/backbone/router.js +++ b/public/js/backbone/router.js @@ -100,9 +100,13 @@ var that = this; socket.emitWrapper = function(eventName, data){ console.log(this); if(this.socket.connected){ - var oldTime = CURRENT_TIMESTAMP; - CURRENT_TIMESTAMP = Date.now(); - this.emit(eventName, data, [CURRENT_TIMESTAMP, oldTime]); + if(eventName=="newNode" || eventName=="movedNode" || eventName=="removeNode"){ + var oldTime = CURRENT_TIMESTAMP; + CURRENT_TIMESTAMP = Date.now(); + this.emit(eventName, data, [CURRENT_TIMESTAMP, oldTime]); + return; + } + this.emit(eventName, data); } else{ //(alert + lock) alert("Internet connection down. Edits not synced. Refresh when back online."); @@ -190,11 +194,12 @@ socket.on("updateReceived", function(data){ parentModel.get("children")[newIndex] = instance._id; }) -socket.on("newNode", function(data){ +socket.on("newNode", function(data, newTime){ //need the instance + the index. var modelJson = data[1]; //(includes the negative ID to find later); var parId = data[0][0]; var newIndex = data[0][1]; + CURRENT_TIMESTAMP = newTime; var newNode = new NodeModel(modelJson); nodesCollection.add(newNode); @@ -234,8 +239,9 @@ socket.on("blurred", function(data){ }); }); -socket.on("removeNode", function(data){ +socket.on("removeNode", function(data, newTime){ var tempVo = {}; //PROTECTING STATE!! + CURRENT_TIMESTAMP = newTime; // tempVo.author = data[3]; //(actually, unnecessary...) tempVo.thisId = data[0]; @@ -248,7 +254,8 @@ socket.on("removeNode", function(data){ removeNode(tempVo, true); //true means came from broadcast. }); -socket.on("movedNode", function(data){ +socket.on("movedNode", function(data, newTime){ + CURRENT_TIMESTAMP = newTime; var ids = data[0]; var indices = data[1]; var author = data[2]; diff --git a/public/js/libs/myLogic/addNode.js b/public/js/libs/myLogic/addNode.js index d826289..0293562 100644 --- a/public/js/libs/myLogic/addNode.js +++ b/public/js/libs/myLogic/addNode.js @@ -1,5 +1,12 @@ +var guidGenerator = function() { + var S4 = function() { + return (((1+Math.random())*0x10000)|0).toString(16).substring(1); + }; + return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()); +} + addNode = function(botStr, topStr){ - var randomId = ( -1 * Math.floor( Math.random() * 100000000) ) + var randomId = guidGenerator(); var modelJSON = { _id: randomId , text: botStr