aboutsummaryrefslogtreecommitdiffstats
path: root/library/controllers/index-db-controller.js
diff options
context:
space:
mode:
authorfrankiebee <frankie.diamond@gmail.com>2017-03-10 10:24:41 +0800
committerfrankiebee <frankie.diamond@gmail.com>2017-03-10 10:24:41 +0800
commit9f3445252c766053b7a773d75dceb1c1bbfdc6f5 (patch)
tree9722cb40e5799cbda5a356d79ff6737254c50b9f /library/controllers/index-db-controller.js
parente602cb13c5266516aadeb1c6908575d43afe4164 (diff)
downloadtangerine-wallet-browser-9f3445252c766053b7a773d75dceb1c1bbfdc6f5.tar
tangerine-wallet-browser-9f3445252c766053b7a773d75dceb1c1bbfdc6f5.tar.gz
tangerine-wallet-browser-9f3445252c766053b7a773d75dceb1c1bbfdc6f5.tar.bz2
tangerine-wallet-browser-9f3445252c766053b7a773d75dceb1c1bbfdc6f5.tar.lz
tangerine-wallet-browser-9f3445252c766053b7a773d75dceb1c1bbfdc6f5.tar.xz
tangerine-wallet-browser-9f3445252c766053b7a773d75dceb1c1bbfdc6f5.tar.zst
tangerine-wallet-browser-9f3445252c766053b7a773d75dceb1c1bbfdc6f5.zip
put background in service worker
Diffstat (limited to 'library/controllers/index-db-controller.js')
-rw-r--r--library/controllers/index-db-controller.js96
1 files changed, 96 insertions, 0 deletions
diff --git a/library/controllers/index-db-controller.js b/library/controllers/index-db-controller.js
new file mode 100644
index 000000000..8b90db066
--- /dev/null
+++ b/library/controllers/index-db-controller.js
@@ -0,0 +1,96 @@
+const EventEmitter = require('events')
+module.exports = class IndexDbController extends EventEmitter {
+
+ constructor (opts) {
+ super()
+ this.migrations = opts.migrations
+ this.key = opts.key
+ this.dbObject = opts.global.indexedDB
+ this.IDBTransaction = opts.global.IDBTransaction || opts.global.webkitIDBTransaction || opts.global.msIDBTransaction || {READ_WRITE: "readwrite"}; // This line should only be needed if it is needed to support the object's constants for older browsers
+ this.IDBKeyRange = opts.global.IDBKeyRange || opts.global.webkitIDBKeyRange || opts.global.msIDBKeyRange;
+ this.version = opts.version
+ this.logging = opts.logging
+ this.initialState = opts.initialState
+ if (this.logging) this.on('log', logger)
+ }
+
+ // Opens the database connection and returns a promise
+ open (version = this.version) {
+ return new Promise((resolve, reject) => {
+ const dbOpenRequest = this.dbObject.open(this.key, version)
+ dbOpenRequest.onerror = (event) => {
+ return reject(event)
+ }
+ dbOpenRequest.onsuccess = (event) => {
+ this.db = dbOpenRequest.result
+ if (!this.db.objectStoreNames.length) {
+ Object.keys(this.initialState).forEach((key) => {
+ this._add(key, this.initialState[key])
+ })
+ }
+ this.emit('success')
+ resolve(this.db)
+ }
+ dbOpenRequest.onupgradeneeded = (event) => {
+ // if (this.migrators)
+ this.db = event.target.result
+ this.migrate()
+ }
+ })
+ }
+
+ requestObjectStore (key, type = 'readonly') {
+ return new Promise((resolve, reject) => {
+ const dbReadWrite = this.db.transaction(key, type)
+ const dataStore = dbReadWrite.objectStore(key)
+ resolve(dataStore)
+ })
+ }
+
+ get (key) {
+ return this.requestObjectStore(key)
+ .then((dataObject)=> {
+ return new Promise((resolve, reject) => {
+ const getRequest = dataObject.get(key)
+ getRequest.onsuccess = (event) => resolve(event.currentTarget.result)
+ getRequest.onerror = (event) => reject(event)
+ })
+ })
+ }
+
+ put (key, store) {
+ return this.requestObjectStore(key, 'readwrite')
+ .then((dataObject)=> {
+ const putRequest = dataObject.put(store)
+ putRequest.onsuccess = (event) => Promise.resolve(event.currentTarget.result)
+ putRequest.onerror = (event) => Promise.reject(event)
+ })
+ }
+
+ update (key, value) {
+
+ }
+
+ migrate () {
+ // this.migrations
+
+ // Place holder for future migrations eg:
+ this.db.deleteObjectStore('meta')
+ this.db.deleteObjectStore('data')
+ this.db.createObjectStore('dataStore')
+ }
+
+ _add (key, objStore, cb = logger) {
+ return this.requestObjectStore(key, 'readwrite')
+ .then((dataObject)=> {
+ const addRequest = dataObject.add(objStore, key)
+ addRequest.onsuccess = (event) => Promise.resolve(event.currentTarget.result)
+ addRequest.onerror = (event) => Promise.reject(event)
+ })
+ }
+
+}
+
+function logger (err, ress) {
+ err ? console.error(`Logger says: ${err}`) : console.dir(`Logger says: ${ress}`)
+}