2018-01-22 01:18:56 +00:00
|
|
|
import { getKnownDbs } from './knownDbs'
|
2018-01-19 17:18:14 +00:00
|
|
|
import debounce from 'lodash/debounce'
|
2018-01-22 01:18:56 +00:00
|
|
|
import { TIMELINE_STORE, getTimelineDatabase } from './timelines'
|
2018-01-19 17:18:14 +00:00
|
|
|
|
|
|
|
const MAX_NUM_STORED_STATUSES = 1000
|
|
|
|
const CLEANUP_INTERVAL = 60000
|
|
|
|
|
|
|
|
async function cleanup(instanceName, timeline) {
|
2018-01-22 01:18:56 +00:00
|
|
|
const db = await getTimelineDatabase(instanceName, timeline)
|
2018-01-19 17:18:14 +00:00
|
|
|
return await new Promise((resolve, reject) => {
|
2018-01-22 01:18:56 +00:00
|
|
|
const tx = db.transaction(TIMELINE_STORE, 'readwrite')
|
|
|
|
const store = tx.objectStore(TIMELINE_STORE)
|
2018-01-19 17:18:14 +00:00
|
|
|
const index = store.index('pinafore_id_as_negative_big_int')
|
|
|
|
|
|
|
|
store.count().onsuccess = (e) => {
|
|
|
|
let count = e.target.result
|
2018-01-20 05:51:22 +00:00
|
|
|
if (count <= MAX_NUM_STORED_STATUSES) {
|
|
|
|
return
|
|
|
|
}
|
2018-01-19 17:18:14 +00:00
|
|
|
let openKeyCursor = index.openKeyCursor || index.openCursor
|
|
|
|
openKeyCursor.call(index, null, 'prev').onsuccess = (e) => {
|
|
|
|
let cursor = e.target.result
|
|
|
|
if (--count < MAX_NUM_STORED_STATUSES || !cursor) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
store.delete(cursor.primaryKey).onsuccess = () => {
|
|
|
|
cursor.continue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.oncomplete = () => resolve()
|
|
|
|
tx.onerror = () => reject(tx.error.name + ' ' + tx.error.message)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const cleanupOldStatuses = debounce(async () => {
|
2018-01-21 04:45:31 +00:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
console.log('cleanupOldStatuses')
|
|
|
|
}
|
2018-01-22 01:18:56 +00:00
|
|
|
|
|
|
|
let knownDbs = await getKnownDbs()
|
|
|
|
let instanceNames = Object.keys(knownDbs)
|
|
|
|
for (let instanceName of instanceNames) {
|
|
|
|
let knownDbsForInstance = knownDbs[instanceName] || []
|
|
|
|
for (let knownDb of knownDbsForInstance) {
|
|
|
|
let {type, dbName} = knownDb
|
|
|
|
if (type !== 'timeline') {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
await cleanup(instanceName, dbName)
|
|
|
|
}
|
2018-01-19 17:18:14 +00:00
|
|
|
}
|
2018-01-21 04:45:31 +00:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
console.log('done cleanupOldStatuses')
|
|
|
|
}
|
2018-01-19 17:18:14 +00:00
|
|
|
}, CLEANUP_INTERVAL)
|