2018-08-30 07:03:29 +02:00
|
|
|
import { database } from '../_database/database'
|
2019-08-17 23:36:13 +02:00
|
|
|
import { decode as decodeBlurhash, init as initBlurhash } from '../_utils/blurhash'
|
|
|
|
import { mark, stop } from '../_utils/marks'
|
2019-08-18 00:35:31 +02:00
|
|
|
import { get } from '../_utils/lodash-lite'
|
2018-08-30 07:03:29 +02:00
|
|
|
|
|
|
|
async function getNotification (instanceName, timelineType, timelineValue, itemId) {
|
|
|
|
return {
|
|
|
|
timelineType,
|
|
|
|
timelineValue,
|
|
|
|
notification: await database.getNotification(instanceName, itemId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getStatus (instanceName, timelineType, timelineValue, itemId) {
|
|
|
|
return {
|
|
|
|
timelineType,
|
|
|
|
timelineValue,
|
|
|
|
status: await database.getStatus(instanceName, itemId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 23:36:13 +02:00
|
|
|
function tryInitBlurhash () {
|
|
|
|
try {
|
|
|
|
initBlurhash()
|
|
|
|
} catch (err) {
|
|
|
|
console.error('could not start blurhash worker', err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function decodeAllBlurhashes (statusOrNotification) {
|
|
|
|
const status = statusOrNotification.status || statusOrNotification.notification.status
|
2019-08-20 05:23:05 +02:00
|
|
|
const mediaWithBlurhashes = get(status, ['media_attachments'], [])
|
|
|
|
.concat(get(status, ['reblog', 'media_attachments'], []))
|
|
|
|
.filter(_ => _.blurhash)
|
2019-08-18 00:35:31 +02:00
|
|
|
if (mediaWithBlurhashes.length) {
|
2019-08-17 23:36:13 +02:00
|
|
|
mark(`decodeBlurhash-${status.id}`)
|
2019-08-18 00:35:31 +02:00
|
|
|
await Promise.all(mediaWithBlurhashes.map(async media => {
|
|
|
|
try {
|
|
|
|
media.decodedBlurhash = await decodeBlurhash(media.blurhash)
|
|
|
|
} catch (err) {
|
|
|
|
console.warn('Could not decode blurhash, ignoring', err)
|
2019-08-17 23:36:13 +02:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
stop(`decodeBlurhash-${status.id}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 07:03:29 +02:00
|
|
|
export function createMakeProps (instanceName, timelineType, timelineValue) {
|
2020-04-27 01:54:00 +02:00
|
|
|
let promiseChain = Promise.resolve()
|
2018-08-30 07:03:29 +02:00
|
|
|
|
2019-08-17 23:36:13 +02:00
|
|
|
tryInitBlurhash() // start the blurhash worker a bit early to save time
|
|
|
|
|
|
|
|
async function fetchFromIndexedDB (itemId) {
|
|
|
|
mark(`fetchFromIndexedDB-${itemId}`)
|
|
|
|
try {
|
|
|
|
const res = await (timelineType === 'notifications'
|
|
|
|
? getNotification(instanceName, timelineType, timelineValue, itemId)
|
|
|
|
: getStatus(instanceName, timelineType, timelineValue, itemId))
|
|
|
|
return res
|
|
|
|
} finally {
|
|
|
|
stop(`fetchFromIndexedDB-${itemId}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 01:54:00 +02:00
|
|
|
async function getStatusOrNotification (itemId) {
|
|
|
|
const statusOrNotification = await fetchFromIndexedDB(itemId)
|
|
|
|
await decodeAllBlurhashes(statusOrNotification)
|
|
|
|
return statusOrNotification
|
|
|
|
}
|
2018-08-30 07:03:29 +02:00
|
|
|
|
2020-04-27 01:54:00 +02:00
|
|
|
// The results from IndexedDB or the worker thread can return in random order,
|
|
|
|
// so we ensure consistent ordering based on the order this function is called in.
|
|
|
|
return itemId => {
|
|
|
|
const getStatusOrNotificationPromise = getStatusOrNotification(itemId) // start the promise ASAP
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
promiseChain = promiseChain
|
|
|
|
.then(() => getStatusOrNotificationPromise)
|
|
|
|
.then(resolve, reject)
|
|
|
|
})
|
2018-08-30 07:03:29 +02:00
|
|
|
}
|
|
|
|
}
|