Files
HackFlowy/lib/makeCommit.js
Curtis SerVaas 16719fe2cc REVISION CONTROL
2014-08-12 18:25:51 -04:00

48 lines
1.5 KiB
JavaScript

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