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
|
|
|
|
|
2018-03-20 02:00:49 +01:00
|
|
|
|
let currentTimelineIsUnchanged = () => (
|
2018-03-19 18:09:05 +01:00
|
|
|
|
store.get('currentInstance') === currentInstance &&
|
|
|
|
|
store.get('currentTimeline') === currentTimeline
|
|
|
|
|
)
|
|
|
|
|
|
2018-03-20 02:00:49 +01:00
|
|
|
|
if (!currentTimelineIsUnchanged()) {
|
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]
|
|
|
|
|
|
2018-03-20 02:00:49 +01:00
|
|
|
|
let onOpenStream = async () => {
|
|
|
|
|
if (!firstTimelineItemId || !currentTimelineIsUnchanged()) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-03-19 18:09:05 +01: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
|
2018-03-20 02:00:49 +01:00
|
|
|
|
let newTimelineItems = await getTimeline(currentInstance, accessToken,
|
|
|
|
|
currentTimeline, null, firstTimelineItemId)
|
2018-03-19 18:09:05 +01:00
|
|
|
|
if (newTimelineItems.length) {
|
|
|
|
|
addStatusesOrNotifications(currentInstance, currentTimeline, newTimelineItems)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let instanceInfo = store.get('currentInstanceInfo')
|
2018-03-20 02:00:49 +01:00
|
|
|
|
let streamingApi = instanceInfo.urls.streaming_api
|
|
|
|
|
currentTimelineStream = createStream(streamingApi, currentInstance, accessToken,
|
|
|
|
|
currentTimeline, onOpenStream)
|
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
|
|
|
|
})
|
|
|
|
|
}
|