mirror of
https://github.com/wassname/HackFlowy.git
synced 2026-07-14 01:10:13 +08:00
REVISION CONTROL
This commit is contained in:
+15
-7
@@ -14,6 +14,8 @@ var sessionStore = new MemoryStore();
|
||||
var io;
|
||||
var online = [];
|
||||
var lastExchangeData = {};
|
||||
var makeCommit = require("./makeCommit.js").makeCommit;
|
||||
var getAndSendRevHistory = require("./revAlgorithm.js").getAndSendRevHistory;
|
||||
|
||||
|
||||
console.log("\n\nLOOK HERE!!")
|
||||
@@ -32,11 +34,7 @@ module.exports = {
|
||||
// },
|
||||
createUser: function(username, password, callback){
|
||||
User.addUser(username, password, callback);
|
||||
},
|
||||
|
||||
getNodes: function(){
|
||||
return Node.findNodes();
|
||||
},
|
||||
},
|
||||
|
||||
getSessionStore: function(){
|
||||
return sessionStore;
|
||||
@@ -46,9 +44,18 @@ module.exports = {
|
||||
io = require('socket.io').listen(server);
|
||||
io.sockets.on('connection', function(socket){
|
||||
|
||||
socket.on("COMMIT", function(){
|
||||
makeCommit();
|
||||
socket.emit("commitReceived");
|
||||
});
|
||||
|
||||
socket.on("revHistoryRequest", function(){
|
||||
getAndSendRevHistory(null, socket);
|
||||
});
|
||||
|
||||
//socket.emit('news', {hello: "world"});
|
||||
socket.on('nodeRequest', function(data){
|
||||
var nodes = Node.findNodes(socket); //finds, then sends through socket.
|
||||
var nodes = Node.findAndSendSocket(socket); //finds, then sends through socket.
|
||||
//var nodes = {'keep': 'calm'};
|
||||
//socket.emit('nodeData', nodes); (emit is in findNodes);
|
||||
})
|
||||
@@ -69,7 +76,7 @@ module.exports = {
|
||||
|
||||
|
||||
socket.on("blurred", function(data){
|
||||
console.log('\nBLURRED\n')
|
||||
// console.log('\nBLURRED\n')
|
||||
socket.broadcast.emit("blurred", data);
|
||||
var id = data[0];
|
||||
var text = data[1];
|
||||
@@ -111,6 +118,7 @@ module.exports = {
|
||||
});
|
||||
|
||||
socket.on("removeNode", function(data){
|
||||
// console.log("removeNode!!!");
|
||||
var thisId = data[0];
|
||||
var thisIndex = data[1];
|
||||
var parId = data[2];
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
var MyNode = require('../models/Node.js').MyNode;
|
||||
var snapMoudule = require('../models/Snap.js');
|
||||
var MySnap = snapMoudule.MySnap;
|
||||
var addSnap = snapMoudule.addSnap;
|
||||
var _ = require("underscore");
|
||||
|
||||
module.exports.makeCommit = makeCommit;
|
||||
|
||||
|
||||
|
||||
//http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example
|
||||
function createCallback(node, now){
|
||||
return function(err,snapTimestamp){
|
||||
console.log("\t\tSNAPTIMESTAMP");
|
||||
console.log(snapTimestamp);
|
||||
console.log("\t\tsnapNode");
|
||||
console.log(node);
|
||||
|
||||
if(snapTimestamp.length == 0){//noSnaps in DB means MyNode is newNode.
|
||||
if(node.parents.length == 0){node.remove();}//(node was created and deleted before being committed)
|
||||
else{addSnap(node, now);}
|
||||
return; //just returns from callback, not from the _.each function.
|
||||
}
|
||||
|
||||
snapTimestamp = snapTimestamp[0].timestamp;
|
||||
if(snapTimestamp == node.timestamp){} //no edits have happened for this node.
|
||||
else{//edits have occurred, and we need to save them.
|
||||
if(node.parents.length == 0){
|
||||
addSnap(node,now,1); //remove the node with no parents (from NodeCollection).
|
||||
}
|
||||
else{
|
||||
addSnap(node, now);
|
||||
}
|
||||
}//else
|
||||
}//findSnapTimeStamp => saveSnap.
|
||||
}
|
||||
|
||||
function makeCommit(){
|
||||
var now = Date.now();
|
||||
MyNode.find(function(err,nodes){
|
||||
_.each(nodes, function(node){
|
||||
console.log("NODE");
|
||||
console.log(node)
|
||||
MySnap.find({cur_id: node._id}, 'timestamp')
|
||||
.sort({timestamp: -1}).limit(1).exec(createCallback(node, now));
|
||||
});//.each
|
||||
});
|
||||
}//function
|
||||
@@ -0,0 +1,122 @@
|
||||
var _ = require('underscore');
|
||||
var MySnap = require('../models/Snap.js').MySnap
|
||||
module.exports.getAndSendRevHistory = getAndSendRevHistory;
|
||||
|
||||
|
||||
function getAndSendRevHistory(rootId, sock){
|
||||
globalList = [];
|
||||
snapHash = {};
|
||||
timeHash = {};
|
||||
depth = 0;
|
||||
socket = sock;
|
||||
|
||||
var rootId = "53ea3f506dc8d39342bf4f9f";
|
||||
// FetchSelfAndChildrenBack(rootId, asyncLoopGetChildren);
|
||||
asyncLoopGetChildren([rootId]);
|
||||
}
|
||||
|
||||
//This is all the backSnaps for ONE rootId.
|
||||
function updateGlobals(rootSnaps){
|
||||
var rootId = rootSnaps[0].cur_id;
|
||||
snapHash[rootId] = snapHash[rootId] || rootSnaps;
|
||||
//snapHash[rootId] = rootSnaps; //should be equivalent.
|
||||
|
||||
_.each(rootSnaps, function(snap){
|
||||
(timeHash[snap.timestamp] = timeHash[snap.timestamp] || []).push(snap);
|
||||
});
|
||||
|
||||
globalList.push(rootId);
|
||||
}
|
||||
|
||||
//This should only be called once per ID.
|
||||
function FetchSelfAndChildrenBack(rootId, callback){
|
||||
MySnap.find({cur_id: rootId}).sort({timestamp: 1}).exec(function(err, rootSnaps){
|
||||
if(err){console.log("ERROR HERE")}
|
||||
|
||||
updateGlobals(rootSnaps);
|
||||
|
||||
var metaArray = [];
|
||||
_.each(rootSnaps,function(snap){metaArray.push(snap.children)})
|
||||
|
||||
mergeArrays(metaArray, callback); //(mergeArrays, then get children)
|
||||
});
|
||||
}
|
||||
|
||||
function mergeArrays(metaArray, callback){
|
||||
// console.log("MERGE_SNAPs_CHILDREN");
|
||||
// console.log("metaArray")
|
||||
// console.log(metaArray);
|
||||
mergedArray = _.union(_.flatten(metaArray));
|
||||
mergedArray = mergedArray.filter( function(a){if (!this[a]) {this[a] = 1; return a;}},{});
|
||||
// console.log("mergedArray");
|
||||
// console.log(mergedArray);
|
||||
|
||||
callback(mergedArray); //d2temp.push(merged); loopStep();
|
||||
}
|
||||
|
||||
|
||||
//d2=nodes in the next depth.//(maybe been FetchSelfAndChildrenBack'd)
|
||||
//globalList=nodes in this depth. //(have bene fetch'd)
|
||||
//temp = nodes to fetch next.
|
||||
function MergeAndDiffArraysSync(d2){
|
||||
var temp = [];
|
||||
_.each(d2, function(el){
|
||||
if(globalList.indexOf(el) == -1){
|
||||
temp.push(el);
|
||||
}
|
||||
});
|
||||
console.log("d2");
|
||||
console.log(d2);
|
||||
globalList = _.union(globalList, temp);
|
||||
return temp
|
||||
}
|
||||
|
||||
|
||||
//BFS from one node to all of its adjacents.
|
||||
//d1Union has all the unique nodes that have ever been a child of root.
|
||||
function asyncLoopGetChildren(d1Union){
|
||||
var i = 0;
|
||||
var numTimes = d1Union.length;
|
||||
var nextDepthList = [];
|
||||
|
||||
|
||||
if(typeof d1Union == 'undefined' || d1Union.length == 0){//bottomed out recursion = end of bfs
|
||||
console.log("finished!!!!")
|
||||
console.log("snapHash");
|
||||
console.log(snapHash);
|
||||
console.log("timeHash");
|
||||
console.log(timeHash);
|
||||
console.log("globalList");
|
||||
console.log(globalList);
|
||||
|
||||
socket.emit("revHistory", [snapHash, timeHash]);
|
||||
return;
|
||||
}
|
||||
|
||||
function loopStep(){
|
||||
if(i == numTimes){//call completion function
|
||||
d2Union = _.union(_.flatten(nextDepthList)); //array of arrays.
|
||||
|
||||
//these can easily be done using callbacks if this causes a bug.
|
||||
var temp = MergeAndDiffArraysSync(d2Union);
|
||||
asyncLoopGetChildren(temp);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// fn(loopStep);
|
||||
FetchSelfAndChildrenBack(d1Union[i], function(mergedArray){
|
||||
nextDepthList.push(mergedArray);
|
||||
loopStep();
|
||||
});
|
||||
i+=1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
loopStep();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user