Solving chrome app problems
This commit is contained in:
parent
bbd00cf2d6
commit
40910c3b34
11 changed files with 204 additions and 45 deletions
|
|
@ -1,58 +1,133 @@
|
|||
'use strict';
|
||||
var _ = require('lodash');
|
||||
var preconditions = require('preconditions').singleton();
|
||||
var isChromeApp = window.chrome && chrome.runtime && chrome.runtime.id;
|
||||
|
||||
function LocalStorage(opts) {
|
||||
|
||||
function LocalStorage() {
|
||||
this.type = 'DB';
|
||||
opts = opts || {};
|
||||
|
||||
|
||||
this.ls = opts.ls
|
||||
|| ( (typeof localStorage !== 'undefined') ? localStorage : null );
|
||||
if (isChromeApp) {
|
||||
localStorage = chrome.storage.local;
|
||||
window.localStorage = chrome.storage.local;
|
||||
}
|
||||
|
||||
preconditions.checkState(this.ls,
|
||||
preconditions.checkState(typeof localStorage !== 'undefined',
|
||||
'localstorage not available, cannot run plugin');
|
||||
};
|
||||
|
||||
LocalStorage.prototype.init = function() {
|
||||
};
|
||||
LocalStorage.prototype.init = function() {};
|
||||
|
||||
LocalStorage.prototype.setCredentials = function(email, password, opts) {
|
||||
// NOP
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
};
|
||||
|
||||
LocalStorage.prototype.getItem = function(k,cb) {
|
||||
preconditions.checkArgument(_.isFunction(cb));
|
||||
return cb(null, this.ls.getItem(k));
|
||||
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 {
|
||||
return cb(null, localStorage.getItem(k));
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Same as setItem, but fails if an item already exists
|
||||
*/
|
||||
LocalStorage.prototype.createItem = function(name, value, callback) {
|
||||
preconditions.checkArgument(_.isFunction(callback));
|
||||
if (this.ls.getItem(name)) {
|
||||
return callback('EEXISTS');
|
||||
var self = this;
|
||||
console.log('createItem ');
|
||||
self.getItem(name,
|
||||
function(err, data) {
|
||||
console.log('error ', err);
|
||||
console.log('data ', data);
|
||||
if (data) {
|
||||
return callback('EEXISTS');
|
||||
} else {
|
||||
console.log('calling setitem ');
|
||||
return self.setItem(name, value, callback);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
LocalStorage.prototype.setItem = function(k, v, cb) {
|
||||
if (isChromeApp) {
|
||||
console.log('.............key', k);
|
||||
console.log('.............value', v);
|
||||
|
||||
var obj = {};
|
||||
obj[k] = v;
|
||||
|
||||
chrome.storage.local.set(obj, cb);
|
||||
} else {
|
||||
localStorage.setItem(k, v);
|
||||
return cb();
|
||||
}
|
||||
return this.setItem(name, value, callback);
|
||||
|
||||
};
|
||||
|
||||
LocalStorage.prototype.setItem = function(k,v,cb) {
|
||||
preconditions.checkArgument(_.isFunction(cb));
|
||||
this.ls.setItem(k,v);
|
||||
LocalStorage.prototype.removeItem = function(k, cb) {
|
||||
if (isChromeApp) {
|
||||
chrome.storage.remove(k, cb);
|
||||
} else {
|
||||
localStorage.removeItem(k);
|
||||
return cb();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
LocalStorage.prototype.clear = function(cb) {
|
||||
if (isChromeApp) {
|
||||
chrome.storage.clear();
|
||||
} else {
|
||||
localStorage.clear();
|
||||
}
|
||||
return cb();
|
||||
};
|
||||
|
||||
LocalStorage.prototype.removeItem = function(k,cb) {
|
||||
preconditions.checkArgument(_.isFunction(cb));
|
||||
this.ls.removeItem(k);
|
||||
return cb();
|
||||
LocalStorage.prototype.allKeys = function(cb) {
|
||||
|
||||
if (isChromeApp) {
|
||||
chrome.storage.local.get(null, function(items) {
|
||||
return cb(null, _.keys(items));
|
||||
});
|
||||
} else {
|
||||
var ret = [];
|
||||
var l = localStorage.length;
|
||||
|
||||
for (var i = 0; i < l; i++)
|
||||
ret.push(localStorage.key(i));
|
||||
|
||||
return cb(null, ret);
|
||||
}
|
||||
};
|
||||
|
||||
LocalStorage.prototype.clear = function(cb) {
|
||||
preconditions.checkArgument(_.isFunction(cb));
|
||||
this.ls.clear();
|
||||
return cb();
|
||||
LocalStorage.prototype.getFirst = function(prefix, opts, cb) {
|
||||
opts = opts || {};
|
||||
var that = this;
|
||||
|
||||
this.allKeys(function(err, allKeys) {
|
||||
var keys = _.filter(allKeys, function(k) {
|
||||
if ((k === prefix) || k.indexOf(prefix) === 0) return true;
|
||||
});
|
||||
|
||||
if (keys.length === 0)
|
||||
return cb(new Error('not found'));
|
||||
|
||||
if (opts.onlyKey)
|
||||
return cb(null, null, keys[0]);
|
||||
|
||||
that.getItem(keys[0], function(err, data) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
return cb(null, data, keys[0]);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = LocalStorage;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue