pinafore/routes/_store/timelineObservers.js

48 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-02-11 21:46:57 +00:00
import { updateInstanceInfo } from '../_actions/instances'
import { createStream } from '../_actions/streaming'
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
store.observe('currentTimeline', async (currentTimeline) => {
if (!process.browser) {
return
}
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
}
if (!currentTimeline) {
return
}
2018-02-15 17:02:46 +00:00
if (currentTimeline !== 'local' &&
currentTimeline !== 'federated' &&
!currentTimeline.startsWith('list/') &&
!currentTimeline.startsWith('tag/')) {
2018-02-11 21:46:57 +00:00
return
}
let currentInstance = store.get('currentInstance')
await updateInstanceInfo(currentInstance)
let instanceInfo = store.get('currentInstanceInfo')
if (!(instanceInfo &&
store.get('currentInstance') === currentInstance &&
store.get('currentTimeline') === currentTimeline)) {
return
}
2018-02-15 17:02:46 +00:00
let accessToken = store.get('accessToken')
2018-02-11 21:46:57 +00:00
currentTimelineStream = createStream(instanceInfo.urls.streaming_api,
currentInstance, accessToken, currentTimeline)
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
})
}