Skip to content

Instantly share code, notes, and snippets.

@lprhodes
Created August 5, 2014 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lprhodes/7d5c99abfa60217eb4a7 to your computer and use it in GitHub Desktop.
Save lprhodes/7d5c99abfa60217eb4a7 to your computer and use it in GitHub Desktop.
processing of userDataQueue item.
processQueueItem = function(value, callback) {
var queueObj = value;
var adjustment = queueObj.adjustment;
var userUID = queueObj.userUID;
var obj = queueObj.obj;
var objs = queueObj.objs;
//We can store multilpe objects within a single queue'd item so that they get processed together.
if (objs) {
var multipleObjectAdjustment = 0;
finish.ordered.map(objs, function(obj, done) {
multipleObjectAdjustment += 0.1; //Make sure these objects don't have the exact same timestamp
var value = {};
value.obj = obj;
value.userUID = userUID;
value.adjustment = multipleObjectAdjustment;
_public.processQueueItem(value, done);
}, function(err, results) {
if (callback) {
callback();
}
});
return;
}
if (!userUID || !obj) {
if (callback) {
return callback();
}
return;
}
if (!adjustment) {
adjustment = 0;
}
var objClassName = obj['class'];
var objUID = obj['uid'];
var identifier = objClassName + '_' + objUID;
var indexRef = privateFirebaseRef.child('v2/userDataQueue/' + userUID + '/index/' + identifier);
indexRef.once('value', function(existingSnapshots) {
var existingObjects = {};
if (existingSnapshots.val() != null) {
existingSnapshots.forEach(function(existingSnapshot) {
existingObjects[existingSnapshot.name()] = existingSnapshot.val();
});
}
var keys = Object.keys(existingObjects);
if (keys.length > 0) {
keys.forEach(function(key) {
var existingObj = existingObjects[key];
for (var propertyName in obj) {
if (propertyName == '.priority' || propertyName == 'dataQueueCreationTimestamp' || propertyName == 'keepSingleInstanceInDataQueue') {
continue;
}
existingObj[propertyName] = obj[propertyName];
}
existingObj['.priority'] = existingObj['dataQueueCreationTimestamp'];
});
}
if (obj['keepSingleInstanceInDataQueue'] != true || keys.length == 0) {
var newPushRef = indexRef.push();
var newIdentifier = newPushRef.name();
if (!adjustment) {
adjustment = 0;
}
if (obj['dataQueueCreationTimestamp'] > 0) {
obj['pregeneratedTimestamp'] = true;
obj['dataQueueCreationTimestamp'] = (obj['dataQueueCreationTimestamp'] + adjustment);
} else {
obj['dataQueueCreationTimestamp'] = (Date.now() + adjustment);
}
obj['.priority'] = obj['dataQueueCreationTimestamp'];
obj['isDownloadCompleted'] = 1;
if (obj['keepSingleInstanceInDataQueue']) {
delete obj['keepSingleInstanceInDataQueue'];
}
existingObjects[newIdentifier] = obj;
}
var keys = Object.keys(existingObjects);
if (keys.length > 0) {
keys.forEach(function(key) {
var existingObj = existingObjects[key];
});
}
indexRef.update(existingObjects, function() {
privateFirebaseRef.child('v2/userDataQueue/' + userUID + '/data/').update(existingObjects, function() {
obj = null;
if (callback) {
return callback();
}
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment