copay/js/plugins/LocalStorage.js

107 lines
2.2 KiB
JavaScript
Raw Normal View History

2014-09-01 19:44:35 -07:00
'use strict';
var _ = require('lodash');
2014-12-02 06:55:29 -08:00
var preconditions = require('preconditions').singleton();
2014-12-04 12:59:02 -08:00
var isChromeApp = typeof window !== "undefined" && window.chrome && chrome.runtime && chrome.runtime.id;
2014-09-01 19:44:35 -07:00
2014-12-01 06:19:18 -08:00
2014-12-04 12:59:02 -08:00
function LocalStorage(opts) {
2014-09-30 11:28:02 -07:00
this.type = 'DB';
2014-12-04 12:59:02 -08:00
opts = opts || {};
2014-12-02 06:55:29 -08:00
2014-12-04 12:59:02 -08:00
this.ls = opts.ls ||
((typeof localStorage !== "undefined") ? localStorage : null);
if (isChromeApp && !this.ls) {
this.ls = localStorage = chrome.storage.local;
2014-12-01 06:19:18 -08:00
window.localStorage = chrome.storage.local;
}
2014-12-03 04:24:52 -08:00
2014-12-04 12:59:02 -08:00
preconditions.checkState(this.ls,
2014-12-03 04:24:52 -08:00
'localstorage not available, cannot run plugin');
2014-09-01 19:44:35 -07:00
};
2014-12-01 06:19:18 -08:00
LocalStorage.prototype.init = function() {};
2014-09-01 19:44:35 -07:00
LocalStorage.prototype.setCredentials = function(email, password, opts) {
2014-12-13 14:38:39 -08:00
this.email = email;
this.password = password;
};
2014-12-01 06:19:18 -08:00
LocalStorage.prototype.getItem = function(k, cb) {
if (isChromeApp) {
chrome.storage.local.get(k,
function(data) {
//TODO check for errors
return cb(null, data[k]);
});
} else {
2014-12-04 12:59:02 -08:00
return cb(null, this.ls.getItem(k));
2014-12-01 06:19:18 -08:00
}
2014-09-01 19:44:35 -07:00
};
2014-10-28 11:20:43 -07:00
/**
* Same as setItem, but fails if an item already exists
*/
LocalStorage.prototype.createItem = function(name, value, callback) {
2014-12-01 06:19:18 -08:00
var self = this;
self.getItem(name,
function(err, data) {
if (data) {
return callback('EEXISTS');
} else {
return self.setItem(name, value, callback);
}
});
};
LocalStorage.prototype.setItem = function(k, v, cb) {
if (isChromeApp) {
var obj = {};
obj[k] = v;
2014-12-04 12:59:02 -08:00
2014-12-01 06:19:18 -08:00
chrome.storage.local.set(obj, cb);
} else {
2014-12-04 12:59:02 -08:00
this.ls.setItem(k, v);
2014-12-01 06:19:18 -08:00
return cb();
2014-10-28 11:20:43 -07:00
}
2014-12-01 06:19:18 -08:00
2014-10-28 11:20:43 -07:00
};
2014-12-01 06:19:18 -08:00
LocalStorage.prototype.removeItem = function(k, cb) {
if (isChromeApp) {
chrome.storage.local.remove(k, cb);
2014-12-01 06:19:18 -08:00
} else {
2014-12-04 12:59:02 -08:00
this.ls.removeItem(k);
2014-12-01 06:19:18 -08:00
return cb();
}
2014-09-01 19:44:35 -07:00
};
2014-12-01 06:19:18 -08:00
LocalStorage.prototype.clear = function(cb) {
2014-12-10 05:49:25 -08:00
// NOP
2014-09-02 21:25:08 -07:00
return cb();
2014-09-01 19:44:35 -07:00
};
2014-12-01 06:19:18 -08:00
LocalStorage.prototype.allKeys = function(cb) {
if (isChromeApp) {
chrome.storage.local.get(null, function(items) {
return cb(null, _.keys(items));
});
} else {
var ret = [];
2014-12-04 12:59:02 -08:00
var l = this.ls.length;
2014-12-01 06:19:18 -08:00
for (var i = 0; i < l; i++)
2014-12-04 12:59:02 -08:00
ret.push(this.ls.key(i));
2014-12-01 06:19:18 -08:00
return cb(null, ret);
}
};
2014-09-01 19:44:35 -07:00
module.exports = LocalStorage;