diff --git a/src/js/service/BackupService.js b/src/js/service/BackupService.js index a154a3c..d58104d 100644 --- a/src/js/service/BackupService.js +++ b/src/js/service/BackupService.js @@ -89,11 +89,17 @@ return; } - var oldestSession = sessions.sort(function (s1, s2) { + // Prepare an array containing all the ids of the sessions to be deleted. + var sessionIdsToDelete = sessions.sort(function (s1, s2) { return s1.startDate - s2.startDate; - })[0].id; + }).map(function (s) { + return s.id; + }).slice(0, sessions.length - MAX_SESSIONS); - return this.deleteSession(oldestSession); + // Delete all the extra sessions. + return Q.all(sessionIdsToDelete.map(function (id) { + return this.deleteSession(id); + }.bind(this))); }.bind(this)); }.bind(this)); } diff --git a/src/js/service/BeforeUnloadService.js b/src/js/service/BeforeUnloadService.js index 80ce6a2..dd96187 100644 --- a/src/js/service/BeforeUnloadService.js +++ b/src/js/service/BeforeUnloadService.js @@ -30,6 +30,8 @@ }; ns.BeforeUnloadService.prototype.onBeforeUnload = function (evt) { + // Attempt one last backup. Some of it may fail due to the asynchronous + // nature of IndexedDB. pskl.app.backupService.backup(); if (pskl.app.savedStatusService.isDirty()) { var confirmationMessage = 'Your current sprite has unsaved changes. Are you sure you want to quit?';