From 5671eb47825012ad063be63824bd67c76db77c00 Mon Sep 17 00:00:00 2001 From: juliandescottes Date: Sun, 6 Aug 2017 17:42:31 +0200 Subject: [PATCH] Delete all extra backup sessions if MAX is reached --- src/js/service/BackupService.js | 12 +++++++++--- src/js/service/BeforeUnloadService.js | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) 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?';