forked from jakearchibald/idb-keyval
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b0f18e
commit 3edd130
Showing
7 changed files
with
251 additions
and
172 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,73 @@ | ||
'use strict'; | ||
|
||
var db; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
function getDB() { | ||
if (!db) { | ||
db = new Promise(function(resolve, reject) { | ||
var openreq = indexedDB.open('keyval-store', 1); | ||
|
||
openreq.onerror = function() { | ||
reject(openreq.error); | ||
}; | ||
|
||
openreq.onupgradeneeded = function() { | ||
// First time setup: create an empty object store | ||
openreq.result.createObjectStore('keyval'); | ||
}; | ||
|
||
openreq.onsuccess = function() { | ||
resolve(openreq.result); | ||
}; | ||
}); | ||
} | ||
return db; | ||
class Store { | ||
constructor(dbName = 'keyval-store', storeName = 'keyval') { | ||
this.storeName = storeName; | ||
this._dbp = new Promise((resolve, reject) => { | ||
const openreq = indexedDB.open(dbName, 1); | ||
openreq.onerror = () => reject(openreq.error); | ||
openreq.onsuccess = () => resolve(openreq.result); | ||
// First time setup: create an empty object store | ||
openreq.onupgradeneeded = () => { | ||
openreq.result.createObjectStore(storeName); | ||
}; | ||
}); | ||
} | ||
_withIDBStore(type, callback) { | ||
return this._dbp.then(db => new Promise((resolve, reject) => { | ||
const transaction = db.transaction(this.storeName, type); | ||
transaction.oncomplete = () => resolve(); | ||
transaction.onabort = transaction.onerror = () => reject(transaction.error); | ||
callback(transaction.objectStore(this.storeName)); | ||
})); | ||
} | ||
} | ||
let store; | ||
function getDefaultStore() { | ||
if (!store) | ||
store = new Store(); | ||
return store; | ||
} | ||
function get(key, store = getDefaultStore()) { | ||
let req; | ||
return store._withIDBStore('readonly', store => { | ||
req = store.get(key); | ||
}).then(() => req.result); | ||
} | ||
function set(key, value, store = getDefaultStore()) { | ||
return store._withIDBStore('readwrite', store => { | ||
store.put(value, key); | ||
}); | ||
} | ||
function del(key, store = getDefaultStore()) { | ||
return store._withIDBStore('readwrite', store => { | ||
store.delete(key); | ||
}); | ||
} | ||
function clear(store = getDefaultStore()) { | ||
return store._withIDBStore('readwrite', store => { | ||
store.clear(); | ||
}); | ||
} | ||
function keys(store = getDefaultStore()) { | ||
const keys = []; | ||
return store._withIDBStore('readonly', store => { | ||
// This would be store.getAllKeys(), but it isn't supported by Edge or Safari. | ||
// And openKeyCursor isn't supported by Safari. | ||
(store.openKeyCursor || store.openCursor).call(store).onsuccess = function () { | ||
if (!this.result) | ||
return; | ||
keys.push(this.result.key); | ||
this.result.continue(); | ||
}; | ||
}).then(() => keys); | ||
} | ||
|
||
function withStore(type, callback) { | ||
return getDB().then(function(db) { | ||
return new Promise(function(resolve, reject) { | ||
var transaction = db.transaction('keyval', type); | ||
transaction.oncomplete = function() { | ||
resolve(); | ||
}; | ||
transaction.onerror = function() { | ||
reject(transaction.error); | ||
}; | ||
callback(transaction.objectStore('keyval')); | ||
}); | ||
}); | ||
} | ||
|
||
var idbKeyval = { | ||
get: function(key) { | ||
var req; | ||
return withStore('readonly', function(store) { | ||
req = store.get(key); | ||
}).then(function() { | ||
return req.result; | ||
}); | ||
}, | ||
set: function(key, value) { | ||
return withStore('readwrite', function(store) { | ||
store.put(value, key); | ||
}); | ||
}, | ||
delete: function(key) { | ||
return withStore('readwrite', function(store) { | ||
store.delete(key); | ||
}); | ||
}, | ||
clear: function() { | ||
return withStore('readwrite', function(store) { | ||
store.clear(); | ||
}); | ||
}, | ||
keys: function() { | ||
var keys = []; | ||
return withStore('readonly', function(store) { | ||
// This would be store.getAllKeys(), but it isn't supported by Edge or Safari. | ||
// And openKeyCursor isn't supported by Safari. | ||
(store.openKeyCursor || store.openCursor).call(store).onsuccess = function() { | ||
if (!this.result) return; | ||
keys.push(this.result.key); | ||
this.result.continue(); | ||
}; | ||
}).then(function() { | ||
return keys; | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = idbKeyval; | ||
exports.Store = Store; | ||
exports.get = get; | ||
exports.set = set; | ||
exports.del = del; | ||
exports.clear = clear; | ||
exports.keys = keys; |
Oops, something went wrong.