pinafore/routes/_store/observers/timelineObservers.js

82 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-03-03 23:15:50 +01:00
import { updateInstanceInfo } from '../../_actions/instances'
import { createStream } from '../../_actions/streaming'
2018-03-19 18:09:05 +01:00
import { getTimeline } from '../../_api/timelines'
import { addStatusesOrNotifications } from '../../_actions/addStatusOrNotification'
2018-02-11 22:46:57 +01:00
export function timelineObservers (store) {
2018-02-15 18:02:46 +01:00
// stream to watch for local/federated/etc. updates. home and notification
// updates are handled in timelineObservers.js
2018-02-11 22:46:57 +01:00
let currentTimelineStream
2018-03-19 18:09:05 +01:00
function shutdownPreviousStream () {
2018-02-11 22:46:57 +01:00
if (currentTimelineStream) {
currentTimelineStream.close()
currentTimelineStream = null
2018-02-15 18:02:46 +01:00
if (process.env.NODE_ENV !== 'production') {
window.currentTimelineStream = null
}
2018-02-11 22:46:57 +01:00
}
2018-03-19 18:09:05 +01:00
}
function shouldObserveTimeline (timeline) {
return timeline &&
!(
timeline !== 'local' &&
timeline !== 'federated' &&
!timeline.startsWith('list/') &&
!timeline.startsWith('tag/')
)
}
store.observe('currentTimeline', async (currentTimeline) => {
if (!process.browser) {
2018-02-11 22:46:57 +01:00
return
}
2018-03-19 18:09:05 +01:00
shutdownPreviousStream()
if (!shouldObserveTimeline(currentTimeline)) {
2018-02-11 22:46:57 +01:00
return
}
let currentInstance = store.get('currentInstance')
2018-03-19 18:09:05 +01:00
let accessToken = store.get('accessToken')
2018-02-11 22:46:57 +01:00
await updateInstanceInfo(currentInstance)
2018-03-19 18:09:05 +01:00
let checkInstanceAndTimelineAreUnchanged = () => (
store.get('currentInstance') === currentInstance &&
store.get('currentTimeline') === currentTimeline
)
if (!checkInstanceAndTimelineAreUnchanged()) {
2018-02-11 22:46:57 +01:00
return
}
2018-03-19 18:09:05 +01:00
let timelineItemIds = store.getForTimeline(currentInstance,
currentTimeline, 'timelineItemIds')
let firstTimelineItemId = timelineItemIds && timelineItemIds[0]
if (firstTimelineItemId) {
// fill in the "streaming gap" i.e. fetch the most recent 20 items so that there isn't
// a big gap in the timeline if you haven't looked at it in awhile
// TODO: race condition here, should start streaming while this request is ongoing
let newTimelineItems = await getTimeline(currentInstance, accessToken, currentTimeline,
null, firstTimelineItemId)
if (newTimelineItems.length) {
addStatusesOrNotifications(currentInstance, currentTimeline, newTimelineItems)
}
if (!checkInstanceAndTimelineAreUnchanged()) {
return
}
}
let instanceInfo = store.get('currentInstanceInfo')
2018-02-11 22:46:57 +01:00
currentTimelineStream = createStream(instanceInfo.urls.streaming_api,
currentInstance, accessToken, currentTimeline)
2018-02-15 18:02:46 +01:00
if (process.env.NODE_ENV !== 'production') {
window.currentTimelineStream = currentTimelineStream
}
2018-02-11 22:46:57 +01:00
})
}