pinafore/routes/_actions/streaming.js

110 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-02-11 21:46:57 +00:00
import { TimelineStream } from '../_api/TimelineStream'
import identity from 'lodash/identity'
import { database } from '../_database/database'
import { store } from '../_store/store'
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
import throttle from 'lodash/throttle'
import { mark, stop } from '../_utils/marks'
2018-02-11 21:46:57 +00:00
2018-02-16 06:21:42 +00:00
async function getReblogIds (instanceName, statusIds) {
2018-02-15 17:02:46 +00:00
let reblogIds = await Promise.all(statusIds.map(async timelineItemId => {
2018-02-11 21:46:57 +00:00
let status = await database.getStatus(instanceName, timelineItemId)
return status.reblog && status.reblog.id
2018-02-15 17:02:46 +00:00
}))
return reblogIds.filter(identity)
}
2018-02-16 06:21:42 +00:00
async function getExistingItemIdsSet (instanceName, timelineName) {
2018-02-15 17:02:46 +00:00
let timelineItemIds = store.getForTimeline(instanceName, timelineName, 'timelineItemIds') || []
if (timelineName === 'notifications') {
return new Set(timelineItemIds)
}
let reblogIds = await getReblogIds(instanceName, timelineItemIds)
return new Set([].concat(timelineItemIds).concat(reblogIds))
}
async function removeDuplicates (instanceName, timelineName, updates) {
// remove duplicates, including duplicates due to reblogs
let existingItemIds = await getExistingItemIdsSet(instanceName, timelineName)
2018-02-11 21:46:57 +00:00
return updates.filter(update => !existingItemIds.has(update.id))
}
2018-02-15 17:02:46 +00:00
async function processFreshUpdates (instanceName, timelineName) {
mark('processFreshUpdates')
let freshUpdates = store.getForTimeline(instanceName, timelineName, 'freshUpdates')
if (freshUpdates && freshUpdates.length) {
let updates = freshUpdates.slice()
store.setForTimeline(instanceName, timelineName, {freshUpdates: []})
2018-02-11 21:46:57 +00:00
updates = await removeDuplicates(instanceName, timelineName, updates)
await database.insertTimelineItems(instanceName, timelineName, updates)
2018-02-12 00:18:53 +00:00
let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd') || []
if (updates && updates.length) {
2018-02-12 00:10:39 +00:00
itemIdsToAdd = itemIdsToAdd.concat(updates.map(_ => _.id))
console.log('adding ', itemIdsToAdd.length, 'items to itemIdsToAdd')
2018-02-12 00:10:39 +00:00
store.setForTimeline(instanceName, timelineName, {itemIdsToAdd: itemIdsToAdd})
}
2018-02-15 17:02:46 +00:00
stop('processFreshUpdates')
2018-02-11 21:46:57 +00:00
}
}
2018-02-15 17:02:46 +00:00
const lazilyProcessFreshUpdates = throttle((instanceName, timelineName) => {
scheduleIdleTask(() => {
2018-02-15 17:02:46 +00:00
/* no await */ processFreshUpdates(instanceName, timelineName)
})
}, 5000)
2018-02-16 06:21:42 +00:00
function processUpdate (instanceName, timelineName, update) {
2018-02-15 17:02:46 +00:00
let freshUpdates = store.getForTimeline(instanceName, timelineName, 'freshUpdates') || []
freshUpdates.push(update)
store.setForTimeline(instanceName, timelineName, {freshUpdates: freshUpdates})
lazilyProcessFreshUpdates(instanceName, timelineName)
}
2018-02-16 06:21:42 +00:00
function processDelete (instanceName, deletion) {
2018-02-15 17:02:46 +00:00
// TODO
}
function processMessage (instanceName, timelineName, message) {
mark('processMessage')
2018-02-11 21:46:57 +00:00
let { event, payload } = message
2018-02-15 17:02:46 +00:00
let parsedPayload = JSON.parse(payload)
switch (event) {
case 'delete':
processDelete(instanceName, parsedPayload)
break
case 'update':
processUpdate(instanceName, timelineName, parsedPayload)
break
case 'notification':
processUpdate(instanceName, 'notifications', parsedPayload)
break
}
stop('processMessage')
2018-02-11 21:46:57 +00:00
}
export function createStream (streamingApi, instanceName, accessToken, timelineName) {
2018-02-11 22:11:03 +00:00
return new TimelineStream(streamingApi, accessToken, timelineName, {
onMessage (msg) {
2018-02-15 17:02:46 +00:00
if (msg.event !== 'update' && msg.event !== 'delete' && msg.event !== 'notification') {
2018-02-11 21:46:57 +00:00
console.error("don't know how to handle event", msg)
return
}
scheduleIdleTask(() => {
2018-02-15 17:02:46 +00:00
processMessage(instanceName, timelineName, msg)
2018-02-11 21:46:57 +00:00
})
},
2018-02-11 22:11:03 +00:00
onOpen () {
2018-02-11 21:46:57 +00:00
console.log('opened stream for timeline', timelineName)
},
2018-02-11 22:11:03 +00:00
onClose () {
2018-02-11 21:46:57 +00:00
console.log('closed stream for timeline', timelineName)
},
2018-02-11 22:11:03 +00:00
onReconnect () {
2018-02-11 21:46:57 +00:00
console.log('reconnected stream for timeline', timelineName)
}
})
2018-02-14 03:35:46 +00:00
}