Import: Wait until db writes resolve before saying we're done (#1401)

FREEBIE
pull/749/head
Scott Nonnenberg 8 years ago
parent 9a2587eaca
commit 7a2c8e815c
No known key found for this signature in database
GPG Key ID: A4931C09644C654B

@ -29,7 +29,9 @@
console.log('Called storage.put before storage is ready. key:', key); console.log('Called storage.put before storage is ready. key:', key);
} }
var item = items.add({id: key, value: value}, {merge: true}); var item = items.add({id: key, value: value}, {merge: true});
item.save(); return new Promise(function(resolve, reject) {
item.save().then(resolve, reject);
});
}, },
get: function(key, defaultValue) { get: function(key, defaultValue) {
@ -44,8 +46,11 @@
var item = items.get("" + key); var item = items.get("" + key);
if (item) { if (item) {
items.remove(item); items.remove(item);
item.destroy(); return new Promise(function(resolve, reject) {
item.destroy().then(resolve, reject);
});
} }
return Promise.resolve();
}, },
onready: function(callback) { onready: function(callback) {

@ -25,13 +25,13 @@
return this.isStarted() && !this.isComplete(); return this.isStarted() && !this.isComplete();
}, },
start: function() { start: function() {
storage.put(IMPORT_STARTED, true); return storage.put(IMPORT_STARTED, true);
}, },
complete: function() { complete: function() {
storage.put(IMPORT_COMPLETE, true); return storage.put(IMPORT_COMPLETE, true);
}, },
saveLocation: function(location) { saveLocation: function(location) {
storage.put(IMPORT_LOCATION, location); return storage.put(IMPORT_LOCATION, location);
}, },
reset: function() { reset: function() {
return Whisper.Backup.clearDatabase(); return Whisper.Backup.clearDatabase();
@ -103,7 +103,10 @@
this.doImport(directory); this.doImport(directory);
}.bind(this), function(error) { }.bind(this), function(error) {
if (error.name !== 'ChooseError') { if (error.name !== 'ChooseError') {
console.log('Error choosing directory:', error && error.stack ? error.stack : error); console.log(
'Error choosing directory:',
error && error.stack ? error.stack : error
);
} }
}); });
}, },
@ -115,26 +118,30 @@
// Wait for prior database interaction to complete // Wait for prior database interaction to complete
this.pending = this.pending.then(function() { this.pending = this.pending.then(function() {
// For resilience to interruptions, clear database both before import and after // For resilience to interruption, clear database both before and on failure
return Whisper.Backup.clearDatabase(); return Whisper.Backup.clearDatabase();
}).then(function() { }).then(function() {
Whisper.Import.start(); return Promise.all([
return Whisper.Backup.importFromDirectory(directory); Whisper.Import.start(),
Whisper.Backup.importFromDirectory(directory)
]);
}).then(function() { }).then(function() {
// Catching in-memory cache up with what's in indexeddb now... // Catching in-memory cache up with what's in indexeddb now...
// NOTE: this fires storage.onready, listened to across the app. We'll restart // NOTE: this fires storage.onready, listened to across the app. We'll restart
// to complete the install to start up cleanly with everything now in the DB. // to complete the install to start up cleanly with everything now in the DB.
return storage.fetch(); return storage.fetch();
}).then(function() { }).then(function() {
// Clearing any migration-related state inherited from the Chrome App return Promise.all([
storage.remove('migrationState'); // Clearing any migration-related state inherited from the Chrome App
storage.remove('migrationEnabled'); storage.remove('migrationState'),
storage.remove('migrationEverCompleted'); storage.remove('migrationEnabled'),
storage.remove('migrationStorageLocation'); storage.remove('migrationEverCompleted'),
storage.remove('migrationStorageLocation'),
Whisper.Import.saveLocation(directory);
Whisper.Import.complete();
Whisper.Import.saveLocation(directory),
Whisper.Import.complete()
]);
}).then(function() {
this.state = State.COMPLETE; this.state = State.COMPLETE;
this.render(); this.render();
}.bind(this)).catch(function(error) { }.bind(this)).catch(function(error) {

Loading…
Cancel
Save