pinafore/routes/_store/observers/timelineObservers.js

48 lines
1.5 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-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
store.observe('currentTimeline', async (currentTimeline) => {
if (!process.browser) {
return
}
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
}
if (!currentTimeline) {
return
}
2018-02-15 18:02:46 +01:00
if (currentTimeline !== 'local' &&
currentTimeline !== 'federated' &&
!currentTimeline.startsWith('list/') &&
!currentTimeline.startsWith('tag/')) {
2018-02-11 22:46:57 +01: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 18:02:46 +01:00
let accessToken = store.get('accessToken')
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
})
}