From ecfe8ce943d66af3fd4ededfdf5e40688dc31924 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Mon, 30 Sep 2013 14:45:02 -0700 Subject: [PATCH] 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. --- js/id/id.js | 22 +++++++++++++++++----- js/id/modes/browse.js | 2 -- js/id/modes/select.js | 2 -- 3 files changed, 17 insertions(+), 9 deletions(-) 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); });