Skip to content

Instantly share code, notes, and snippets.

@mschuerig
Last active June 17, 2016 17:49
Show Gist options
  • Save mschuerig/ce6ec9300ff79750a0e821557791350e to your computer and use it in GitHub Desktop.
Save mschuerig/ce6ec9300ff79750a0e821557791350e to your computer and use it in GitHub Desktop.
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