From 58d2f71e0976e2c1fbb32972764057527bf0125b Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 1 Mar 2017 17:57:34 -0800 Subject: [PATCH] Add migration to clean up old expiring messages Expiring messages received before 0.31.0 may not have an expires_at time populated. Loading these messages once will update their expires_at if it wasn't already set. To avoid loading too many messages into memory, add them individually, and remove them from the collection as soon as they are added, allowing them to be garbage collected immediately. // FREEBIE --- js/database.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/js/database.js b/js/database.js index ff4c3af3..6ae4a5a9 100644 --- a/js/database.js +++ b/js/database.js @@ -199,6 +199,25 @@ messages.createIndex('expires_at', 'expires_at', { unique: false }); next(); } + }, + { + version: "12.0", + migrate: function(transaction, next) { + console.log('migration 12.0'); + console.log('cleaning up expiring messages with no expires_at'); + var messages = transaction.objectStore('messages'); + window.addEventListener('storage_ready', function() { + var messages = new Whisper.MessageCollection(); + messages.fetch({ + conditions: {expireTimer: {$gt: 0}}, + addIndividually: true + }); + messages.on('add', function(m) { + messages.remove(m); + }); + }); + next(); + } } ]; }());