NW v0.16. Migrate localstorage data to chrome.storage
This commit is contained in:
parent
fa39cee3b6
commit
a50f3fb50c
4 changed files with 61 additions and 8 deletions
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.services')
|
||||
.factory('localStorageService', function(platformInfo, $timeout) {
|
||||
.factory('localStorageService', function(platformInfo, $timeout, $log) {
|
||||
var isNW = platformInfo.isNW;
|
||||
var isChromeApp = platformInfo.isChromeApp;
|
||||
var root = {};
|
||||
|
|
@ -11,11 +11,12 @@ angular.module('copayApp.services')
|
|||
ls = chrome.storage.local;
|
||||
}
|
||||
|
||||
|
||||
if (!ls)
|
||||
throw new Error('localstorage not available');
|
||||
|
||||
root.get = function(k, cb) {
|
||||
if (isChromeApp && !isNW) {
|
||||
if (isChromeApp || isNW) {
|
||||
chrome.storage.local.get(k,
|
||||
function(data) {
|
||||
//TODO check for errors
|
||||
|
|
@ -41,7 +42,7 @@ angular.module('copayApp.services')
|
|||
};
|
||||
|
||||
root.set = function(k, v, cb) {
|
||||
if (isChromeApp && !isNW) {
|
||||
if (isChromeApp || isNW) {
|
||||
var obj = {};
|
||||
obj[k] = v;
|
||||
|
||||
|
|
@ -54,7 +55,7 @@ angular.module('copayApp.services')
|
|||
};
|
||||
|
||||
root.remove = function(k, cb) {
|
||||
if (isChromeApp && !isNW) {
|
||||
if (isChromeApp || isNW) {
|
||||
chrome.storage.local.remove(k, cb);
|
||||
} else {
|
||||
ls.removeItem(k);
|
||||
|
|
@ -63,5 +64,37 @@ angular.module('copayApp.services')
|
|||
|
||||
};
|
||||
|
||||
|
||||
if (isNW) {
|
||||
$log.info('Overwritting localstorage with chrome storage for NW.JS');
|
||||
|
||||
var ts = ls.getItem('migrationToChromeStorage');
|
||||
var p = ls.getItem('profile');
|
||||
|
||||
// Need migration?
|
||||
if (!ts && p) {
|
||||
$log.info('### MIGRATING DATA! TO CHROME STORAGE');
|
||||
|
||||
var j = 0;
|
||||
for (var i = 0; i < localStorage.length; i++) {
|
||||
var k = ls.key(i);
|
||||
var v = ls.getItem(k);
|
||||
|
||||
$log.debug(' Key: ' + k);
|
||||
root.set(k, v, function() {
|
||||
j++;
|
||||
if (j == localStorage.length) {
|
||||
$log.info('### MIGRATION DONE');
|
||||
ls.setItem('migrationToChromeStorage', Date.now())
|
||||
ls = chrome.storage.local;
|
||||
}
|
||||
})
|
||||
}
|
||||
} else if (p) {
|
||||
$log.info('# Data already migrated to Chrome storage on ' + ts);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return root;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue