-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSmartStorage.min.js
23 lines (23 loc) · 6.79 KB
/
SmartStorage.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
MIT Licensed - https://raw.github.com/ahume/smart-storage/master/LICENSE
Copyright (c) 2010 Andy Hume (http://andyhume.net, [email protected])
*/
function SmartStorage(a,b){if(SmartStorage.browserIsSupported())this.dbname=a,this.password=b||null;else throw"SmartStorage error: You should catch this and deal with browsers that don't support localStorage.";}
SmartStorage.prototype._setItemForDb=function(a,b,c,e){var d=b,b=JSON.stringify(b);c&&(b=(new Date).getTime()+c+"--cache--"+b);var f=this;if("string"===SmartStorage.typeOf(this.password)){var g=SmartStorage.WorkerPool.getWorker();g.onmessage=function(b){localStorage.setItem(f.dbname+"_"+a,b.data);e(d);SmartStorage.WorkerPool.releaseWorker(g)};g.postMessage({set:!0,password:this.password,value:b})}else return localStorage.setItem(this.dbname+"_"+a,b)};
SmartStorage.prototype._getItemForDb=function(a,b){var c=localStorage.getItem(this.dbname+"_"+a);if("string"===SmartStorage.typeOf(this.password))if(null===c)b(c);else{var e=SmartStorage.WorkerPool.getWorker();e.onmessage=function(a){c=SmartStorage.getCachableValue(a.data);b(JSON.parse(c));SmartStorage.WorkerPool.releaseWorker(e)};e.postMessage({password:this.password,value:c})}else return c=c&&SmartStorage.getCachableValue(c),JSON.parse(c)};
SmartStorage.prototype._removeItemForDb=function(a){return localStorage.removeItem(this.dbname+"_"+a)};SmartStorage.prototype._renameKey=function(a,b){var c=this.dbname+"_"+a,e=this.dbname+"_"+b;if(null===localStorage.getItem(c))throw"SmartStorage error: Cannot rename non-existant key.";localStorage.setItem(e,localStorage.getItem(c));localStorage.removeItem(c)};
SmartStorage.prototype.set=function(a,b,c,e){if(2>arguments.length)throw"SmartStorage error: set() requires at least 2 arguments.";if("function"===SmartStorage.typeOf(b))throw"SmartStorage error: Can't store function reference.";return this._setItemForDb(a,b,c,e)};
SmartStorage.prototype.multiSet=function(a,b,c){if(1>arguments.length)throw"SmartStorage error: multiSet() requires at least 1 arguments.";var e=added=0;if(Object.keys)e=Object.keys(a).length;else for(k in a)a.hasOwnProperty(k)&&e++;for(var d in a)a.hasOwnProperty(d)&&(this.password?this._setItemForDb(d,a[d],b,function(){added++;added===e&&c()}):this._setItemForDb(d,a[d],b))};
SmartStorage.prototype.get=function(a,b){if(1>arguments.length)throw"SmartStorage error: get() requires 1 argument.";return this._getItemForDb(a,b)};SmartStorage.prototype.remove=function(a){if(1>arguments.length)throw"SmartStorage error: remove() requires 1 argument.";return this._removeItemForDb(a)};SmartStorage.prototype.clear=function(){for(var a in localStorage)0===a.indexOf(this.dbname)&&localStorage.removeItem(a)};
SmartStorage.prototype.count=function(){var a=0,b;for(b in localStorage)0===b.indexOf(this.dbname)&&a++;return a};
SmartStorage.prototype.append=function(a,b,c){function e(a){if(null!==a&&"string"!==SmartStorage.typeOf(a))throw"SmartStorage error: Can only append() to a string.";return(a||"")+b}if(2>arguments.length)throw"SmartStorage error: append() requires at least 2 arguments.";if(this.password){var d=this;this._getItemForDb(a,function(g){b=e(g);d._setItemForDb(a,b,null,function(a){c(a.length)})})}else{var f=this._getItemForDb(a),b=e(f);this._setItemForDb(a,b);return b.length}};
SmartStorage.prototype.incr=function(a,b,c){function e(a){"number"!==SmartStorage.typeOf(a)&&(a=0);return a+b}b||(b=1);var d,f=this;if(this.password)this._getItemForDb(a,function(b){d=e(b);f._setItemForDb(a,d,null,function(){c(d)})});else{var g=this._getItemForDb(a);d=e(g);this._setItemForDb(a,d);return d}};SmartStorage.prototype.decr=function(a,b,c){b||(b=1);return this.incr(a,-b,c)};
SmartStorage.prototype.rename=function(a,b){if(a===b)throw"SmartStorage error: Cannot rename key to itself.";this._renameKey(a,b)};SmartStorage.prototype.setnx=function(a,b,c){if(this.password){var e=this;this._getItemForDb(a,function(d){null===d?e.set(a,b,null,function(){c(!0)}):c(!1)})}else return null===this._getItemForDb(a)?(this.set(a,b),!0):!1};
SmartStorage.prototype.getset=function(a,b,c){if(this.password){var e=this;this._getItemForDb(a,function(d){e.set(a,b,null,function(){c(d)})})}else{var d=this._getItemForDb(a);this.set(a,b);return d}};SmartStorage.prototype.expire=function(a,b,c){if(this.password){var e=this;this._getItemForDb(a,function(f){null!==f&&e.set(a,d,b,function(){c(!0)});c(!1)})}else{var d=this._getItemForDb(a);return null!==d?(this.set(a,d,b),!0):!1}};
SmartStorage.prototype.persist=function(a,b){if(this.password){var c=this;this._getItemForDb(a,function(d){null!==d&&c.set(a,d,null,function(){b(!0)});b(!1)})}else{var e=this._getItemForDb(a);return null!==e?(this.set(a,e),!0):!1}};
SmartStorage.prototype.push=function(a,b,c){function e(a,b){if(null===a)a=[];else if("array"!==SmartStorage.typeOf(a))throw"SmartStorage error: Value must be an array to push a value on it.";a.push(b);return a}if(this.password){var d=this;this._getItemForDb(a,function(g){g=e(g,b);d.set(a,g,null,function(a){c(a.length)})})}else{var f=this._getItemForDb(a),f=e(f,b);this.set(a,f);return f.length}};
SmartStorage.prototype.pop=function(a,b){function c(a){if(null===a)throw"SmartStorage error: Cannot pop from non-existant key.";if("array"!==SmartStorage.typeOf(a))throw"SmartStorage error: Value must be an array to pop a value from it.";return a.pop()}if(this.password){var e=this;this._getItemForDb(a,function(d){var f=c(d);e.set(a,d,null,function(){b(f)})})}else{var d=this._getItemForDb(a),f=c(d);this.set(a,d);return f}};
SmartStorage.prototype.unshift=function(a,b,c){function e(a,b){if(null===a)a=[];else if("array"!==SmartStorage.typeOf(a))throw"SmartStorage error: Value must be an array to unshift a value on it.";a.unshift(b);return a}if(this.password){var d=this;this._getItemForDb(a,function(f){f=e(f,b);d.set(a,f,null,function(a){c(a.length)})})}else{var f=this._getItemForDb(a),f=e(f,b);this.set(a,f);return f.length}};
SmartStorage.prototype.shift=function(a,b){function c(a){if(null===a)throw"SmartStorage error: Cannot pop from non-existant key.";if("array"!==SmartStorage.typeOf(a))throw"SmartStorage error: Value must be an array to pop a value from it.";return a.shift()}if(this.password){var e=this;this._getItemForDb(a,function(d){var f=c(d);e.set(a,d,null,function(){b(f)})})}else{var d=this._getItemForDb(a),f=c(d);this.set(a,d);return f}};
SmartStorage.getCachableValue=function(a){-1<a.indexOf("--cache--")&&(a=a.split("--cache--"),a=(new Date).getTime()>a[0]?null:a[1]);return a};SmartStorage.browserIsSupported=function(){try{return"localStorage"in window&&null!==window.localStorage}catch(a){return!1}};SmartStorage.typeOf=function(a){var b=typeof a;"object"===b&&(a?a instanceof Array?b="array":a instanceof String&&(b="string"):b="null");return b};
SmartStorage.WorkerPool=function(){var a=[];return{getWorker:function(){return 0<a.length?a.pop():new Worker("../sjcl.js")},releaseWorker:function(b){a.push(b)},pool:a}}();