2018-02-11 21:46:57 +00:00
|
|
|
import { updateInstanceInfo, updateVerifyCredentialsForInstance } from '../_actions/instances'
|
|
|
|
import { updateLists } from '../_actions/lists'
|
2018-02-15 17:02:46 +00:00
|
|
|
import { createStream } from '../_actions/streaming'
|
2018-02-11 21:46:57 +00:00
|
|
|
|
|
|
|
export function instanceObservers (store) {
|
2018-02-15 17:02:46 +00: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 21:46:57 +00:00
|
|
|
if (!currentInstance) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updateVerifyCredentialsForInstance(currentInstance)
|
|
|
|
updateInstanceInfo(currentInstance)
|
|
|
|
updateLists()
|
2018-02-15 17:02:46 +00: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 21:46:57 +00:00
|
|
|
})
|
2018-02-11 22:11:03 +00:00
|
|
|
}
|