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