From 63b846cf5f7fa75414b95e12051ca6ea76f6e8af Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 11 May 2015 16:15:53 -0600 Subject: [PATCH] Safari storage migration: code review --- platform/safari/vapi-background.js | 88 +++++++++++++++--------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/platform/safari/vapi-background.js b/platform/safari/vapi-background.js index f9bf50b90..6e63afcc3 100644 --- a/platform/safari/vapi-background.js +++ b/platform/safari/vapi-background.js @@ -223,49 +223,6 @@ }; /******************************************************************************/ - if(!safari.extension.settings.migratedStorage) { - var migrationMap = { - "cached_asset_content://assets/user/filters.txt": "userFilters" - }; - - var delayed = []; - - vAPI.storage.preferences = { - get: function(a, b) { - delayed.push(settingsStorage.get.bind(settingsStorage, a, b)); - }, - set: function(a, b) { - delayed.push(settingsStorage.set.bind(settingsStorage, a, b)); - }, - remove: function(a, b) { - delayed.push(settingsStorage.remove.bind(settingsStorage, a, b)); - }, - clear: function() { - delayed.push(settingsStorage.clear.bind(settingsStorage)); - }, - }; - - localforage.iterate(function(value, key) { - if(migrationMap[key]) { - safari.extension.settings[migrationMap[key]] = value; - return; - } - if(key.lastIndexOf("cached_asset", 0) === 0) { - return; - } - safari.extension.settings[key] = value; - localforage.removeItem(key); - }, function() { - var func; - while(func = delayed.pop()) { - func(); - } - delayed = null; - vAPI.storage.preferences = settingsStorage; - }); - safari.extension.settings.migratedStorage = true; - } - var settingsStorage = { _storage: safari.extension.settings, get: function(keys, callback) { @@ -340,6 +297,51 @@ } }; + vAPI.storage.preferences = settingsStorage; + + if(!safari.extension.settings.migratedStorage) { // if we haven't already migrated + var migrationMap = { + "cached_asset_content://assets/user/filters.txt": "userFilters" + }; + + var delayed = []; + + vAPI.storage.preferences = { + get: function(a, b) { + delayed.push(settingsStorage.get.bind(settingsStorage, a, b)); + }, + set: function(a, b) { + delayed.push(settingsStorage.set.bind(settingsStorage, a, b)); + }, + remove: function(a, b) { + delayed.push(settingsStorage.remove.bind(settingsStorage, a, b)); + }, + clear: function() { + delayed.push(settingsStorage.clear.bind(settingsStorage)); + }, + }; + + localforage.iterate(function(value, key) { + if(migrationMap[key]) { + safari.extension.settings[migrationMap[key]] = value; + return; + } + if(key.lastIndexOf("cached_asset", 0) === 0) { + return; + } + safari.extension.settings[key] = value; + localforage.removeItem(key); + }, function() { + var func; + while(func = delayed.pop()) { + func(); + } + delayed = null; + vAPI.storage.preferences = settingsStorage; + }); + safari.extension.settings.migratedStorage = true; + } + /******************************************************************************/ vAPI.tabs = {