Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async storage for saving/getting large data [DO NOT MERGE] #4418

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion modules/core/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import _isString from 'lodash-es/isString';
import { dispatch as d3_dispatch } from 'd3-dispatch';
import { json as d3_json } from 'd3-request';
import { select as d3_select } from 'd3-selection';
import localforage from 'localforage';

import {
t,
Expand Down Expand Up @@ -103,6 +104,29 @@ export function coreContext() {
}
};

context.asyncStorage = function(k, v, callback) {
var action;

if (typeof v === 'function') {
callback = v;
v = undefined;
}

if (v === undefined) {
action = localforage.getItem(k, callback);
} else if (v === null) {
action = localforage.removeItem(k, callback);
} else {
action = localforage.setItem(k, v, callback);
}

return action.catch(function(err) {
/* eslint-disable no-console */
console.error('async storage error', err);
/* eslint-enable no-console */
});
};


/* Straight accessors. Avoid using these if you can. */
var ui, connection, history;
Expand Down Expand Up @@ -431,7 +455,7 @@ export function coreContext() {

// Debounce save, since it's a synchronous localStorage write,
// and history changes can happen frequently (e.g. when dragging).
context.debouncedSave = _debounce(context.save, 350);
context.debouncedSave = _debounce(context.save, 100);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why you changed this? (If anything I think we could slow down the saves, not speed them up)

function withDebouncedSave(fn) {
return function() {
var result = fn.apply(history, arguments);
Expand Down
29 changes: 21 additions & 8 deletions modules/core/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,14 +554,14 @@ export function coreHistory(context) {


save: function() {
if (lock.locked()) context.storage(getKey('saved_history'), history.toJSON() || null);
if (lock.locked()) context.asyncStorage(getKey('saved_history'), history.toJSON() || null);
return history;
},


clearSaved: function() {
context.debouncedSave.cancel();
if (lock.locked()) context.storage(getKey('saved_history'), null);
if (lock.locked()) context.asyncStorage(getKey('saved_history'), null);
return history;
},

Expand All @@ -577,18 +577,31 @@ export function coreHistory(context) {


// is iD not open in another window and it detects that
// there's a history stored in localStorage that's recoverable?
restorableChanges: function() {
return lock.locked() && !!context.storage(getKey('saved_history'));
// there's a history stored in async storage that's recoverable?
restorableChanges: function(callback) {
var isLocked = lock.locked();

if (isLocked) {
return callback(true);
}

return context.asyncStorage(getKey('saved_history'), function(err, savedHistory) {
if (err) {
return callback(false);
}

callback(!!savedHistory);
});
},


// load history from a version stored in localStorage
// load history from a version stored in async storage
restore: function() {
if (!lock.locked()) return;

var json = context.storage(getKey('saved_history'));
if (json) history.fromJSON(json, true);
context.asyncStorage(getKey('saved_history'), function(err, json) {
if (!err && json) history.fromJSON(json, true);
});
},


Expand Down
135 changes: 71 additions & 64 deletions modules/ui/restore.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,78 @@ import { uiModal } from './modal';
export function uiRestore(context) {

return function(selection) {
if (!context.history().lock() || !context.history().restorableChanges())
if (!context.history().lock())
return;

var modalSelection = uiModal(selection, true);

modalSelection.select('.modal')
.attr('class', 'modal fillL col6');

var introModal = modalSelection.select('.content');

introModal
.attr('class','cf');

introModal
.append('div')
.attr('class', 'modal-section')
.append('h3')
.text(t('restore.heading'));

introModal
.append('div')
.attr('class','modal-section')
.append('p')
.text(t('restore.description'));

var buttonWrap = introModal
.append('div')
.attr('class', 'modal-actions cf');

var restore = buttonWrap
.append('button')
.attr('class', 'restore col6')
.on('click', function() {
context.history().restore();
modalSelection.remove();
});

restore
.append('svg')
.attr('class', 'logo logo-restore')
.append('use')
.attr('xlink:href', '#logo-restore');

restore
.append('div')
.text(t('restore.restore'));

var reset = buttonWrap
.append('button')
.attr('class', 'reset col6')
.on('click', function() {
context.history().clearSaved();
modalSelection.remove();
});

reset
.append('svg')
.attr('class', 'logo logo-reset')
.append('use')
.attr('xlink:href', '#logo-reset');

reset
.append('div')
.text(t('restore.reset'));

restore.node().focus();
context.history().restorableChanges(function(restorableChanges) {
if (!restorableChanges) {
return;
}

var modalSelection = uiModal(selection, true);

modalSelection.select('.modal')
.attr('class', 'modal fillL col6');

var introModal = modalSelection.select('.content');

introModal
.attr('class','cf');

introModal
.append('div')
.attr('class', 'modal-section')
.append('h3')
.text(t('restore.heading'));

introModal
.append('div')
.attr('class','modal-section')
.append('p')
.text(t('restore.description'));

var buttonWrap = introModal
.append('div')
.attr('class', 'modal-actions cf');

var restore = buttonWrap
.append('button')
.attr('class', 'restore col6')
.on('click', function() {
context.history().restore();
modalSelection.remove();
});

restore
.append('svg')
.attr('class', 'logo logo-restore')
.append('use')
.attr('xlink:href', '#logo-restore');

restore
.append('div')
.text(t('restore.restore'));

var reset = buttonWrap
.append('button')
.attr('class', 'reset col6')
.on('click', function() {
context.history().clearSaved();
modalSelection.remove();
});

reset
.append('svg')
.attr('class', 'logo logo-reset')
.append('use')
.attr('xlink:href', '#logo-reset');

reset
.append('div')
.text(t('restore.reset'));

restore.node().focus();

});
};
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@mapbox/sexagesimal": "1.1.0",
"@mapbox/togeojson": "0.16.0",
"diacritics": "1.3.0",
"localforage": "^1.5.1",
"lodash-es": "4.17.4",
"marked": "0.3.6",
"osm-auth": "1.0.2",
Expand Down