Adjust the timing of localStorage saves

If localStorage writes were free, we'd want to just save
on every history change. Second best is to debounce the
write.

Writing on mode change is problematic; it sometimes happens
when not desired and sometimes doesn't happen when desired.

Fixes #1857.
This commit is contained in:
John Firebaugh
2013-09-30 14:45:02 -07:00
parent 17325fde83
commit ecfe8ce943
3 changed files with 17 additions and 9 deletions

View File

@@ -76,11 +76,6 @@ window.iD = function () {
/* History */
context.graph = history.graph;
context.perform = history.perform;
context.replace = history.replace;
context.pop = history.pop;
context.undo = history.undo;
context.redo = history.redo;
context.changes = history.changes;
context.intersects = history.intersects;
@@ -104,6 +99,23 @@ window.iD = function () {
return context;
};
// Debounce save, since it's a synchronous localStorage write,
// and history changes can happen frequently (e.g. when dragging).
var debouncedSave = _.debounce(context.save, 350);
function withDebouncedSave(fn) {
return function() {
var result = fn.apply(history, arguments);
debouncedSave();
return result;
}
}
context.perform = withDebouncedSave(history.perform);
context.replace = withDebouncedSave(history.replace);
context.pop = withDebouncedSave(history.pop);
context.undo = withDebouncedSave(history.undo);
context.redo = withDebouncedSave(history.redo);
/* Graph */
context.hasEntity = function(id) {
return history.graph().hasEntity(id);

View File

@@ -15,8 +15,6 @@ iD.modes.Browse = function(context) {
iD.modes.DragNode(context).behavior];
mode.enter = function() {
context.save();
behaviors.forEach(function(behavior) {
context.install(behavior);
});

View File

@@ -70,8 +70,6 @@ iD.modes.Select = function(context, selectedIDs) {
};
mode.enter = function() {
context.save();
behaviors.forEach(function(behavior) {
context.install(behavior);
});