Created
September 6, 2014 19:23
-
-
Save witoldsz/534e0f061f2a194a946e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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