Last active
December 19, 2015 07:40
-
-
Save nexpr/7a1610bd7e8d7e93562a to your computer and use it in GitHub Desktop.
localStorage風indexedDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ILS(store_name, db_name){ | |
var self = this | |
store_name = store_name || "default" | |
db_name = db_name || "ILS" | |
Object.defineProperties(this, { | |
"store_name": { | |
value: store_name, | |
writable: false | |
}, | |
"db_name": { | |
value: db_name, | |
writable: false | |
} | |
}) | |
this._lock = true | |
this._q = [] | |
init() | |
function init(version){ | |
var args = [db_name] | |
version && args.push(version) | |
var opreq = indexedDB.open(...args); | |
opreq.onupgradeneeded = eve => { | |
var db = eve.target.result | |
db.createObjectStore(store_name); | |
} | |
opreq.onsuccess = eve => { | |
var db = eve.target.result | |
if(db.objectStoreNames.contains(store_name)){ | |
self._db = db | |
self._lock = false | |
self._next() | |
}else{ | |
db.close() | |
init(db.version + 1) | |
} | |
} | |
opreq.onerror = eve => {console.error(eve)} | |
} | |
} | |
ILS.prototype._next = function(){ | |
var nx = this._q.shift() | |
this._lock = !!nx | |
nx && this[nx.method_name].apply(this, nx.args) | |
} | |
ILS.prototype._common = function(obj, tr_routine){ | |
var tr = this._db.transaction(this.store_name, obj.mode) | |
tr.oncomplete = () => { | |
obj.success ? obj.success(this._next.bind(this), obj.result) : this._next() | |
} | |
tr.onfail = obj.fail || (err => {console.error(err)}) | |
tr_routine(tr) | |
} | |
ILS.prototype._get = function(key, success, fail){ | |
var obj = { | |
success, | |
fail, | |
mode: "readonly" | |
} | |
this._common(obj, (tr) => { | |
var st = tr.objectStore(this.store_name) | |
st.get(key).onsuccess = eve => { | |
obj.result = eve.target.result | |
} | |
}) | |
} | |
ILS.prototype._set = function(key, value, success, fail){ | |
var obj = { | |
success, | |
fail, | |
mode: "readwrite" | |
} | |
this._common(obj, (tr) => { | |
var st = tr.objectStore(this.store_name) | |
st.put(value, key).onsuccess = eve => { | |
obj.result = eve.target.result | |
} | |
}) | |
} | |
ILS.prototype._remove = function(key, success, fail){ | |
var obj = { | |
success, | |
fail, | |
mode: "readwrite" | |
} | |
this._common(obj, (tr) => { | |
var st = tr.objectStore(this.store_name) | |
st.delete(key).onsuccess = eve =>{ | |
obj.result = eve.target.result | |
} | |
}) | |
} | |
ILS.prototype._clear = function(success, fail){ | |
var obj = { | |
success, | |
fail, | |
mode: "readwrite" | |
} | |
this._common(obj, (tr) => { | |
var st = tr.objectStore(this.store_name) | |
st.clear().onsuccess = eve =>{ | |
obj.result = eve.target.result | |
} | |
}) | |
} | |
ILS.prototype._length = function(success, fail){ | |
var obj = { | |
success, | |
fail, | |
mode: "readwrite" | |
} | |
this._common(obj, (tr) => { | |
var st = tr.objectStore(this.store_name) | |
st.count().onsuccess = eve =>{ | |
obj.result = eve.target.result | |
} | |
}) | |
} | |
ILS.prototype._keys = function(success, fail){ | |
var obj = { | |
success, | |
fail, | |
mode: "readonly", | |
result: [] | |
} | |
this._common(obj, (tr) => { | |
var st = tr.objectStore(this.store_name) | |
st.openCursor().onsuccess = eve =>{ | |
var cursor = eve.target.result | |
if(cursor){ | |
obj.result.push(cursor.key) | |
cursor.continue() | |
} | |
} | |
}) | |
} | |
ILS.prototype._registerAction = function(method_name, args){ | |
this._q.push({method_name, args}) | |
if(!this._lock) this._next() | |
return this | |
} | |
ILS.prototype.get = function(...args){ | |
return this._registerAction("_get", args) | |
} | |
ILS.prototype.set = function(...args){ | |
return this._registerAction("_set", args) | |
} | |
ILS.prototype.remove = function(...args){ | |
return this._registerAction("_remove", args) | |
} | |
ILS.prototype.delete = function(...args){ | |
return this._registerAction("_remove", args) | |
} | |
ILS.prototype.clear = function(...args){ | |
return this._registerAction("_clear", args) | |
} | |
ILS.prototype.reset = function(...args){ | |
return this._registerAction("_clear", args) | |
} | |
ILS.prototype.length = function(...args){ | |
return this._registerAction("_length", args) | |
} | |
ILS.prototype.count = function(...args){ | |
return this._registerAction("_length", args) | |
} | |
ILS.prototype.keys = function(...args){ | |
return this._registerAction("_keys", args) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 基本な使い方 | |
var ils = new ILS() | |
ils.set("a", "d") | |
ils.get("a", function(end, e){console.log(e),end()}) | |
// メソッドチェーンでもいい | |
// コールバック関数を定義したら第一引数を return 代わりに実行しないとダメ | |
new ILS() | |
.set("x", {a:1,b:2}, function(end){console.log("set x"), end()}) | |
.set("y", {c:true,d:false}, function(end){console.log("set y"), end()}) | |
.get("x", function(end, e){console.log(e), end()}) | |
.get("y", function(end, e){console.log(e), end()}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment