pinafore/routes/_store/observers/timelineObservers.js

86 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-03-03 22:15:50 +00:00
import { updateInstanceInfo } from '../../_actions/instances'
import { createStream } from '../../_actions/streaming'
2018-03-19 17:09:05 +00:00
import { getTimeline } from '../../_api/timelines'
import { addStatusesOrNotifications } from '../../_actions/addStatusOrNotification'
2018-02-11 21:46:57 +00:00
export function timelineObservers (store) {
2018-02-15 17:02:46 +00:00
// stream to watch for local/federated/etc. updates. home and notification
// updates are handled in timelineObservers.js
2018-02-11 21:46:57 +00:00
let currentTimelineStream
2018-03-19 17:09:05 +00:00
function shutdownPreviousStream () {
2018-02-11 21:46:57 +00:00
if (currentTimelineStream) {
currentTimelineStream.close()
currentTimelineStream = null
2018-02-15 17:02:46 +00:00
if (process.env.NODE_ENV !== 'production') {
window.currentTimelineStream = null
}
2018-02-11 21:46:57 +00:00
}
2018-03-19 17:09:05 +00:00
}
function shouldObserveTimeline (timeline) {
return timeline &&
!(
2018-04-19 03:43:13 +00:00
timeline !== 'local' &&
2018-03-19 17:09:05 +00:00
timeline !== 'federated' &&
!timeline.startsWith('list/') &&
!timeline.startsWith('tag/')
2018-04-19 03:43:13 +00:00
)
2018-03-19 17:09:05 +00:00
}
store.observe('currentTimeline', async (currentTimeline) => {
if (!process.browser) {
2018-02-11 21:46:57 +00:00
return
}
2018-03-19 17:09:05 +00:00
shutdownPreviousStream()
if (!shouldObserveTimeline(currentTimeline)) {
2018-02-11 21:46:57 +00:00
return
}
let { currentInstance } = store.get()
let { accessToken } = store.get()
2018-02-11 21:46:57 +00:00
await updateInstanceInfo(currentInstance)
2018-03-19 17:09:05 +00:00
let currentTimelineIsUnchanged = () => {
let {
currentInstance: newCurrentInstance,
currentTimeline: newCurrentTimeline
} = store.get()
return newCurrentInstance === currentInstance &&
newCurrentTimeline === currentTimeline
}
2018-03-19 17:09:05 +00:00
if (!currentTimelineIsUnchanged()) {
2018-02-11 21:46:57 +00:00
return
}
2018-03-19 17:09:05 +00:00
let timelineItemIds = store.getForTimeline(currentInstance,
currentTimeline, 'timelineItemIds')
let firstTimelineItemId = timelineItemIds && timelineItemIds[0]
let onOpenStream = async () => {
if (!firstTimelineItemId || !currentTimelineIsUnchanged()) {
return
}
2018-03-19 17:09:05 +00:00
// 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
let newTimelineItems = await getTimeline(currentInstance, accessToken,
currentTimeline, null, firstTimelineItemId)
2018-03-19 17:09:05 +00:00
if (newTimelineItems.length) {
addStatusesOrNotifications(currentInstance, currentTimeline, newTimelineItems)
}
}
let { currentInstanceInfo } = store.get()
let streamingApi = currentInstanceInfo.urls.streaming_api
currentTimelineStream = createStream(streamingApi, currentInstance, accessToken,
currentTimeline, onOpenStream)
2018-02-15 17:02:46 +00:00
if (process.env.NODE_ENV !== 'production') {
window.currentTimelineStream = currentTimelineStream
}
2018-02-11 21:46:57 +00:00
})
}