2018-01-23 17:03:31 +00:00
|
|
|
import {
|
|
|
|
META_STORE,
|
2018-02-04 02:06:02 +00:00
|
|
|
STATUS_TIMELINES_STORE,
|
2018-01-23 17:21:21 +00:00
|
|
|
STATUSES_STORE,
|
2018-01-28 20:51:48 +00:00
|
|
|
ACCOUNTS_STORE,
|
2018-02-04 02:06:02 +00:00
|
|
|
RELATIONSHIPS_STORE,
|
|
|
|
NOTIFICATIONS_STORE,
|
2018-02-11 18:35:25 +00:00
|
|
|
NOTIFICATION_TIMELINES_STORE,
|
2018-02-14 03:34:37 +00:00
|
|
|
PINNED_STATUSES_STORE,
|
2018-03-09 07:18:18 +00:00
|
|
|
TIMESTAMP,
|
|
|
|
REBLOG_ID,
|
2018-03-11 00:21:10 +00:00
|
|
|
THREADS_STORE,
|
2018-03-25 01:04:54 +00:00
|
|
|
STATUS_ID,
|
|
|
|
USERNAME_LOWERCASE
|
2018-01-23 17:03:31 +00:00
|
|
|
} from './constants'
|
|
|
|
|
2018-03-11 04:24:07 +00:00
|
|
|
import forEach from 'lodash/forEach'
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
const openReqs = {}
|
|
|
|
const databaseCache = {}
|
|
|
|
|
2018-03-25 01:04:54 +00:00
|
|
|
const DB_VERSION_INITIAL = 9
|
|
|
|
const DB_VERSION_SEARCH_ACCOUNTS = 10
|
|
|
|
const DB_VERSION_CURRENT = 10
|
2018-02-14 03:34:37 +00:00
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export function getDatabase (instanceName) {
|
2018-01-27 16:22:23 +00:00
|
|
|
if (!instanceName) {
|
2018-01-25 08:01:56 +00:00
|
|
|
throw new Error('instanceName is undefined in getDatabase()')
|
|
|
|
}
|
2018-01-23 17:03:31 +00:00
|
|
|
if (databaseCache[instanceName]) {
|
|
|
|
return Promise.resolve(databaseCache[instanceName])
|
|
|
|
}
|
|
|
|
|
|
|
|
databaseCache[instanceName] = new Promise((resolve, reject) => {
|
2018-03-25 01:04:54 +00:00
|
|
|
let req = indexedDB.open(instanceName, DB_VERSION_CURRENT)
|
2018-01-23 17:03:31 +00:00
|
|
|
openReqs[instanceName] = req
|
|
|
|
req.onerror = reject
|
|
|
|
req.onblocked = () => {
|
|
|
|
console.log('idb blocked')
|
|
|
|
}
|
2018-01-28 20:51:48 +00:00
|
|
|
req.onupgradeneeded = (e) => {
|
2018-02-09 06:29:29 +00:00
|
|
|
let db = req.result
|
2018-03-25 01:04:54 +00:00
|
|
|
let tx = e.currentTarget.transaction
|
2018-03-11 04:24:07 +00:00
|
|
|
|
|
|
|
function createObjectStore (name, init, indexes) {
|
|
|
|
let store = init
|
|
|
|
? db.createObjectStore(name, init)
|
|
|
|
: db.createObjectStore(name)
|
|
|
|
if (indexes) {
|
|
|
|
forEach(indexes, (indexValue, indexKey) => {
|
|
|
|
store.createIndex(indexKey, indexValue)
|
|
|
|
})
|
|
|
|
}
|
2018-03-09 02:31:59 +00:00
|
|
|
}
|
2018-03-11 04:24:07 +00:00
|
|
|
|
2018-03-25 01:04:54 +00:00
|
|
|
if (e.oldVersion < DB_VERSION_INITIAL) {
|
2018-03-11 04:24:07 +00:00
|
|
|
createObjectStore(STATUSES_STORE, {keyPath: 'id'}, {
|
|
|
|
[TIMESTAMP]: TIMESTAMP,
|
|
|
|
[REBLOG_ID]: REBLOG_ID
|
|
|
|
})
|
|
|
|
createObjectStore(STATUS_TIMELINES_STORE, null, {
|
|
|
|
'statusId': ''
|
|
|
|
})
|
|
|
|
createObjectStore(NOTIFICATIONS_STORE, {keyPath: 'id'}, {
|
|
|
|
[TIMESTAMP]: TIMESTAMP,
|
|
|
|
[STATUS_ID]: STATUS_ID
|
|
|
|
})
|
|
|
|
createObjectStore(NOTIFICATION_TIMELINES_STORE, null, {
|
|
|
|
'notificationId': ''
|
|
|
|
})
|
|
|
|
createObjectStore(ACCOUNTS_STORE, {keyPath: 'id'}, {
|
|
|
|
[TIMESTAMP]: TIMESTAMP
|
|
|
|
})
|
|
|
|
createObjectStore(RELATIONSHIPS_STORE, {keyPath: 'id'}, {
|
|
|
|
[TIMESTAMP]: TIMESTAMP
|
|
|
|
})
|
|
|
|
createObjectStore(THREADS_STORE, null, {
|
|
|
|
'statusId': ''
|
|
|
|
})
|
|
|
|
createObjectStore(PINNED_STATUSES_STORE, null, {
|
|
|
|
'statusId': ''
|
|
|
|
})
|
|
|
|
createObjectStore(META_STORE)
|
2018-03-11 00:21:10 +00:00
|
|
|
}
|
2018-03-25 01:04:54 +00:00
|
|
|
if (e.oldVersion < DB_VERSION_SEARCH_ACCOUNTS) {
|
|
|
|
tx.objectStore(ACCOUNTS_STORE)
|
|
|
|
.createIndex(USERNAME_LOWERCASE, USERNAME_LOWERCASE)
|
|
|
|
}
|
2018-01-23 17:03:31 +00:00
|
|
|
}
|
|
|
|
req.onsuccess = () => resolve(req.result)
|
|
|
|
})
|
|
|
|
return databaseCache[instanceName]
|
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export async function dbPromise (db, storeName, readOnlyOrReadWrite, cb) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-01-23 17:03:31 +00:00
|
|
|
const tx = db.transaction(storeName, readOnlyOrReadWrite)
|
2018-02-09 06:29:29 +00:00
|
|
|
let store = typeof storeName === 'string'
|
|
|
|
? tx.objectStore(storeName)
|
|
|
|
: storeName.map(name => tx.objectStore(name))
|
2018-01-23 17:03:31 +00:00
|
|
|
let res
|
|
|
|
cb(store, (result) => {
|
|
|
|
res = result
|
|
|
|
})
|
|
|
|
|
|
|
|
tx.oncomplete = () => resolve(res)
|
2018-02-09 06:29:29 +00:00
|
|
|
tx.onerror = () => reject(tx.error)
|
2018-01-23 17:03:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export function deleteDatabase (instanceName) {
|
2018-01-23 17:03:31 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// close any open requests
|
2018-02-09 06:29:29 +00:00
|
|
|
let openReq = openReqs[instanceName]
|
2018-01-23 17:03:31 +00:00
|
|
|
if (openReq && openReq.result) {
|
|
|
|
openReq.result.close()
|
|
|
|
}
|
|
|
|
delete openReqs[instanceName]
|
|
|
|
delete databaseCache[instanceName]
|
|
|
|
let req = indexedDB.deleteDatabase(instanceName)
|
|
|
|
req.onsuccess = () => resolve()
|
2018-02-09 06:29:29 +00:00
|
|
|
req.onerror = () => reject(req.error)
|
2018-01-23 17:03:31 +00:00
|
|
|
})
|
2018-02-09 06:29:29 +00:00
|
|
|
}
|