diff --git a/js/id/id.js b/js/id/id.js index b526fc5a8..53b57d3e1 100644 --- a/js/id/id.js +++ b/js/id/id.js @@ -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); diff --git a/js/id/modes/browse.js b/js/id/modes/browse.js index be15b9d10..2b6178cf0 100644 --- a/js/id/modes/browse.js +++ b/js/id/modes/browse.js @@ -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); }); diff --git a/js/id/modes/select.js b/js/id/modes/select.js index 45f2cf1bd..550a7bdf2 100644 --- a/js/id/modes/select.js +++ b/js/id/modes/select.js @@ -70,8 +70,6 @@ iD.modes.Select = function(context, selectedIDs) { }; mode.enter = function() { - context.save(); - behaviors.forEach(function(behavior) { context.install(behavior); });