From 974b10340902faaaa88f0477cbe0fcd90ca7ee7d Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 4 Mar 2017 20:56:54 -0800 Subject: [PATCH] initial code for migration from sync storage to local storage --- ext/bg/js/util.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 814a27f6..6d5c030c 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -188,22 +188,29 @@ function optionsVersion(options) { } function optionsLoad() { - if (!chrome.storage.sync) { - chrome.storage.sync = chrome.storage.local; - } - return new Promise((resolve, reject) => { - chrome.storage.sync.get(null, options => resolve(optionsVersion(options))); + if (chrome.storage.sync) { + chrome.storage.sync.getBytesInUse(null, resolve); + } else { + resolve(0); + } + }).then(bytes => { + const storage = bytes === 0 ? chrome.storage.local : chrome.storage.sync; + return new Promise((resolve, reject) => { + storage.get(null, options => resolve(optionsVersion(options))); + }); + }).then(options => { + if (chrome.storage.sync) { + chrome.storage.sync.clear(); + } + + return options; }); } function optionsSave(options) { - if (!chrome.storage.sync) { - chrome.storage.sync = chrome.storage.local; - } - return new Promise((resolve, reject) => { - chrome.storage.sync.set(options, resolve); + chrome.storage.local.set(options, resolve); }).then(() => { instYomi().optionsSet(options); fgOptionsSet(options);