Pinafore-Web-Client-Frontend/routes/_store/observers/instanceObservers.js

42 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-03-03 23:15:50 +01:00
import { updateInstanceInfo, updateVerifyCredentialsForInstance } from '../../_actions/instances'
import { updateLists } from '../../_actions/lists'
import { createStream } from '../../_actions/streaming'
2018-02-11 22:46:57 +01:00
export function instanceObservers (store) {
2018-02-15 18:02:46 +01:00
// stream to watch for home timeline updates and notifications
let currentInstanceStream
store.observe('currentInstance', async (currentInstance) => {
if (!process.browser) {
return
}
if (currentInstanceStream) {
currentInstanceStream.close()
currentInstanceStream = null
if (process.env.NODE_ENV !== 'production') {
window.currentInstanceStream = null
}
}
2018-02-11 22:46:57 +01:00
if (!currentInstance) {
return
}
updateVerifyCredentialsForInstance(currentInstance)
updateInstanceInfo(currentInstance)
updateLists()
2018-02-15 18:02:46 +01:00
await updateInstanceInfo(currentInstance)
let instanceInfo = store.get('currentInstanceInfo')
if (!(instanceInfo && store.get('currentInstance') === currentInstance)) {
return
}
let accessToken = store.get('accessToken')
currentInstanceStream = createStream(instanceInfo.urls.streaming_api,
currentInstance, accessToken, 'home')
if (process.env.NODE_ENV !== 'production') {
window.currentInstanceStream = currentInstanceStream
}
2018-02-11 22:46:57 +01:00
})
2018-02-11 23:11:03 +01:00
}