Skip to content

Instantly share code, notes, and snippets.

@lemiant
Created December 3, 2014 18:33
Show Gist options
  • Save lemiant/e87dc2165c7f825aad60 to your computer and use it in GitHub Desktop.
Save lemiant/e87dc2165c7f825aad60 to your computer and use it in GitHub Desktop.
markDirty()/cleanUp() function decorator
(function() {
window.batch_function = function(target) {
var state = "clean";
var lastArgs = [window, []];
var run = function() {
if (state === "clean") {
state = "running";
target.apply(this, arguments);
}
else {
state = "dirty";
lastArgs = [this, arguments];
}
};
run.force = function() {
target.apply(this, arguments);
state = "running";
};
run.done = function() {
var lastState = state;
state = "clean";
if (lastState === "dirty") {
run.apply(lastArgs[0], lastArgs[1]);
}
};
run.state = function() {
return state;
};
return run;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment