2018-01-22 01:18:56 +00:00
|
|
|
import { addKnownDb } from './knownDbs'
|
2018-01-22 02:36:40 +00:00
|
|
|
import { openReqs, databaseCache } from './cache'
|
2018-01-19 17:18:14 +00:00
|
|
|
|
2018-01-22 01:18:56 +00:00
|
|
|
export const TIMELINE_STORE = 'statuses'
|
2018-01-19 17:18:14 +00:00
|
|
|
|
2018-01-22 01:18:56 +00:00
|
|
|
export function createTimelineDbName(instanceName, timeline) {
|
|
|
|
return `${instanceName}_timeline_${timeline}`
|
2018-01-19 17:18:14 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 01:18:56 +00:00
|
|
|
export function getTimelineDatabase(instanceName, timeline) {
|
|
|
|
let dbName = createTimelineDbName(instanceName, timeline)
|
2018-01-19 17:18:14 +00:00
|
|
|
|
2018-01-22 02:36:40 +00:00
|
|
|
if (databaseCache[dbName]) {
|
|
|
|
return Promise.resolve(databaseCache[dbName])
|
|
|
|
}
|
|
|
|
|
2018-01-22 01:18:56 +00:00
|
|
|
addKnownDb(instanceName, 'timeline', dbName)
|
2018-01-19 17:18:14 +00:00
|
|
|
|
2018-01-22 02:36:40 +00:00
|
|
|
databaseCache[dbName] = new Promise((resolve, reject) => {
|
2018-01-19 17:18:14 +00:00
|
|
|
let req = indexedDB.open(dbName, 1)
|
2018-01-22 02:36:40 +00:00
|
|
|
openReqs[dbName] = req
|
2018-01-19 17:18:14 +00:00
|
|
|
req.onerror = reject
|
|
|
|
req.onblocked = () => {
|
|
|
|
console.log('idb blocked')
|
|
|
|
}
|
|
|
|
req.onupgradeneeded = () => {
|
|
|
|
let db = req.result;
|
2018-01-22 01:18:56 +00:00
|
|
|
let oStore = db.createObjectStore(TIMELINE_STORE, {
|
2018-01-19 17:18:14 +00:00
|
|
|
keyPath: 'id'
|
|
|
|
})
|
|
|
|
oStore.createIndex('pinafore_id_as_negative_big_int', 'pinafore_id_as_negative_big_int')
|
|
|
|
}
|
|
|
|
req.onsuccess = () => resolve(req.result)
|
|
|
|
})
|
2018-01-22 02:36:40 +00:00
|
|
|
return databaseCache[dbName]
|
2018-01-19 17:18:14 +00:00
|
|
|
}
|