aboutsummaryrefslogtreecommitdiffstats
path: root/library/controllers/index-db-controller.js
diff options
context:
space:
mode:
Diffstat (limited to 'library/controllers/index-db-controller.js')
-rw-r--r--library/controllers/index-db-controller.js25
1 files changed, 13 insertions, 12 deletions
diff --git a/library/controllers/index-db-controller.js b/library/controllers/index-db-controller.js
index 6bcae9845..041ddae2e 100644
--- a/library/controllers/index-db-controller.js
+++ b/library/controllers/index-db-controller.js
@@ -23,20 +23,25 @@ module.exports = class IndexDbController extends EventEmitter {
}
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()
+ this.db.createObjectStore('dataStore')
}
})
+ .then((openRequest) => {
+ return this.get('dataStore')
+ })
+ .then((data) => {
+ if (!data) {
+ return this._add('dataStore', this.initialState)
+ .then(() => this.get('dataStore'))
+ .then((versionedData) => Promise.resolve(versionedData.data))
+ }
+ return Promise.resolve(data)
+ })
}
requestObjectStore (key, type = 'readonly') {
@@ -47,7 +52,7 @@ module.exports = class IndexDbController extends EventEmitter {
})
}
- get (key) {
+ get (key = 'dataStore') {
return this.requestObjectStore(key)
.then((dataObject)=> {
return new Promise((resolve, reject) => {
@@ -67,10 +72,6 @@ module.exports = class IndexDbController extends EventEmitter {
})
}
- migrate () {
- this.db.createObjectStore('dataStore')
- }
-
_add (key, objStore, cb = logger) {
return this.requestObjectStore(key, 'readwrite')
.then((dataObject)=> {