aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfrankiebee <frankie.diamond@gmail.com>2017-03-24 10:14:08 +0800
committerfrankiebee <frankie.diamond@gmail.com>2017-03-24 10:14:08 +0800
commit282775c52f558bbd90d4d0a1706a835dde6c3202 (patch)
tree5539676e8940496e6b10c906157f302d1a6c4a62
parent8e30acb41a2cd97efd5428c60c2a5c910758b412 (diff)
downloadtangerine-wallet-browser-282775c52f558bbd90d4d0a1706a835dde6c3202.tar
tangerine-wallet-browser-282775c52f558bbd90d4d0a1706a835dde6c3202.tar.gz
tangerine-wallet-browser-282775c52f558bbd90d4d0a1706a835dde6c3202.tar.bz2
tangerine-wallet-browser-282775c52f558bbd90d4d0a1706a835dde6c3202.tar.lz
tangerine-wallet-browser-282775c52f558bbd90d4d0a1706a835dde6c3202.tar.xz
tangerine-wallet-browser-282775c52f558bbd90d4d0a1706a835dde6c3202.tar.zst
tangerine-wallet-browser-282775c52f558bbd90d4d0a1706a835dde6c3202.zip
add versioning and migrateing data
-rw-r--r--library/controllers/index-db-controller.js25
-rw-r--r--library/sw-core.js54
2 files changed, 28 insertions, 51 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)=> {
diff --git a/library/sw-core.js b/library/sw-core.js
index 8c7de2c32..46ef95e21 100644
--- a/library/sw-core.js
+++ b/library/sw-core.js
@@ -59,12 +59,6 @@ const dbController = new DbController({
key: STORAGE_KEY,
global: self,
version: 2,
- initialState: {
- dataStore: {
- meta: 2,
- data: firstTimeState,
- },
- },
})
asyncQ.waterfall([
() => loadStateFromPersistence(),
@@ -87,36 +81,12 @@ function loadStateFromPersistence() {
const initialState = migrator.generateInitialState(firstTimeState)
dbController.initialState = initialState
return dbController.open()
- .then((openRequest) => {
- return dbController.get('dataStore')
+ .then((versionedData) => migrator.migrateData(versionedData))
+ .then((versionedData) => {
+ dbController.put(versionedData)
+ return Promise.resolve(versionedData)
})
- .then((data) => {
- if (!data) {
- return dbController._add('dataStore', initialState)
- .then(() => dbController.get('dataStore'))
- .then((versionedData) => Promise.resolve(versionedData.data))
- }
-
- return Promise.resolve(data.data)
- })
- .catch((err) => console.error(err))
- /*
- need to get migrations working
- */
-
- // return asyncQ.waterfall([
- // // read from disk
- // () => Promise.resolve(diskStore || initialState),
- // // migrate data
- // (versionedData) => migrator.migrateData(versionedData),
- // // write to disk
- // (versionedData) => {
- // diskStore.put(versionedData)
- // return Promise.resolve(versionedData)
- // },
- // // resolve to just data
- // (versionedData) => Promise.resolve(versionedData.data),
- // ])
+ .then((versionedData) => Promise.resolve(versionedData.data))
}
function setupController (initState, client) {
@@ -142,14 +112,20 @@ function setupController (initState, client) {
// diskStore
// )
controller.store.subscribe((state) => {
- dbController.put(versionifyData(state))
+ versionifyData(state)
+ .then((versionedData) => dbController.put(versionedData))
.catch((err) => {console.error(err)})
})
function versionifyData(state) {
- // let versionedData = diskStore.getState()
+ // let versionedData
// versionedData.data = state
- let versionedData = {data: state}
- return versionedData
+ return dbController.get()
+ .then((rawData) => {
+ return Promise.resolve({
+ data: state,
+ meta: rawData.meta,
+ })}
+ )
}
//