2018-01-22 02:18:56 +01:00
|
|
|
import { addKnownDb } from './knownDbs'
|
2018-01-19 18:18:14 +01:00
|
|
|
|
|
|
|
const databaseCache = {}
|
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) {
|
2018-01-19 18:18:14 +01:00
|
|
|
const key = `${instanceName}_${timeline}`
|
|
|
|
if (databaseCache[key]) {
|
|
|
|
return Promise.resolve(databaseCache[key])
|
|
|
|
}
|
|
|
|
|
2018-01-22 02:18:56 +01:00
|
|
|
let dbName = createTimelineDbName(instanceName, timeline)
|
2018-01-19 18:18:14 +01:00
|
|
|
|
2018-01-22 02:18:56 +01:00
|
|
|
addKnownDb(instanceName, 'timeline', dbName)
|
2018-01-19 18:18:14 +01:00
|
|
|
|
|
|
|
databaseCache[key] = new Promise((resolve, reject) => {
|
|
|
|
let req = indexedDB.open(dbName, 1)
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
return databaseCache[key]
|
|
|
|
}
|