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

FREEBIE
This commit is contained in:
Scott Nonnenberg 2017-08-30 09:32:16 -07:00
parent 9a2587eaca
commit 7a2c8e815c
No known key found for this signature in database
GPG Key ID: A4931C09644C654B
2 changed files with 29 additions and 17 deletions

View File

@ -29,7 +29,9 @@
console.log('Called storage.put before storage is ready. key:', key);
}
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) {
@ -44,8 +46,11 @@
var item = items.get("" + key);
if (item) {
items.remove(item);
item.destroy();
return new Promise(function(resolve, reject) {
item.destroy().then(resolve, reject);
});
}
return Promise.resolve();
},
onready: function(callback) {

View File

@ -25,13 +25,13 @@
return this.isStarted() && !this.isComplete();
},
start: function() {
storage.put(IMPORT_STARTED, true);
return storage.put(IMPORT_STARTED, true);
},
complete: function() {
storage.put(IMPORT_COMPLETE, true);
return storage.put(IMPORT_COMPLETE, true);
},
saveLocation: function(location) {
storage.put(IMPORT_LOCATION, location);
return storage.put(IMPORT_LOCATION, location);
},
reset: function() {
return Whisper.Backup.clearDatabase();
@ -103,7 +103,10 @@
this.doImport(directory);
}.bind(this), function(error) {
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
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();
}).then(function() {
Whisper.Import.start();
return Whisper.Backup.importFromDirectory(directory);
return Promise.all([
Whisper.Import.start(),
Whisper.Backup.importFromDirectory(directory)
]);
}).then(function() {
// Catching in-memory cache up with what's in indexeddb now...
// 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.
return storage.fetch();
}).then(function() {
// Clearing any migration-related state inherited from the Chrome App
storage.remove('migrationState');
storage.remove('migrationEnabled');
storage.remove('migrationEverCompleted');
storage.remove('migrationStorageLocation');
Whisper.Import.saveLocation(directory);
Whisper.Import.complete();
return Promise.all([
// Clearing any migration-related state inherited from the Chrome App
storage.remove('migrationState'),
storage.remove('migrationEnabled'),
storage.remove('migrationEverCompleted'),
storage.remove('migrationStorageLocation'),
Whisper.Import.saveLocation(directory),
Whisper.Import.complete()
]);
}).then(function() {
this.state = State.COMPLETE;
this.render();
}.bind(this)).catch(function(error) {