Last active
June 17, 2016 17:49
-
-
Save mschuerig/ce6ec9300ff79750a0e821557791350e 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
function reentrant(f, opts) { | |
opts = $.extend({catchup: false}); | |
var state = { | |
running: false, | |
reentry: false | |
}; | |
var rf = function() { | |
if (state.running) { | |
state.reentry = true; | |
return; | |
} | |
state.running = true; | |
var r = f.apply(this, arguments); | |
if (state.reentry && opts.catchup) { | |
setTimeout(rf, 0); | |
} | |
state.running = false; | |
state.reentry = false; | |
return r; | |
}; | |
return rf; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment