I am trying to debounce a savefunction that takes an object to save as a parameter for automatic saving that fires when a key is pressed. Failure cancels the save until the user stops typing, or at least the idea. Sort of:
var save = _.debounce(function(obj) {
...
}, delay);
If this falls apart, I will try to save two objects in quick succession. Since debounce does not take into account the object passed in the object, only the second save call will be saved, and only one object will be saved.
save(obj1);
save(obj2);
Saves only obj2, for example.
I could make an objinstance of a class that has its own method savethat takes care that debouncing only keeps this object. Or keep a list of partial / curry functions somewhere, but I hope there will be a one-stop solution. Sort of:
var save = _.easySolution(function(obj) {
...
}, delay);
I am looking for the next save line to save each object, but only save each object once.
save(obj1);
save(obj2);
save(obj3);
save(obj2);
save(obj2);
save(obj3);
save(obj2);
save(obj1);
EDIT: Something like this, maybe just not so confusing, and something that doesn't mutate objwith a function __save?
function save(obj) {
if(!obj.__save) {
obj.__save = _.debounce(_.partial(function(obj) {
...
}, obj), delay);
}
obj.__save();
}
source
share