Skip to content

Instantly share code, notes, and snippets.

@witoldsz
Created September 6, 2014 19:23
Show Gist options
  • Save witoldsz/534e0f061f2a194a946e to your computer and use it in GitHub Desktop.
Save witoldsz/534e0f061f2a194a946e to your computer and use it in GitHub Desktop.
var lockMonitor = {};
function schedule(messageKey, callback) {
function run(messageObject, trace, deferred) {
var lockKey = messageObject[messageKey];
if (!lockMonitor[lockKey]) {
lockMonitor[lockKey] = true;
var result = callback(messageObject, trace);
deferred.resolve(result);
Q(result).then(function() {
delete lockMonitor[lockKey];
});
} else {
setTimeout(run, 50, messageObject, trace, deferred);
}
}
return function(messageObject, trace) {
var deferred = Q.defer();
run(messageObject, trace, deferred);
return deferred.promise;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment