1
0
mirror of https://github.com/nolanlawson/pinafore synced 2024-12-12 18:28:03 +01:00
Pinafore-Web-Client-Frontend/routes/_store/instanceObservers.js
2018-02-15 09:02:46 -08:00

44 lines
1.3 KiB
JavaScript

import { updateInstanceInfo, updateVerifyCredentialsForInstance } from '../_actions/instances'
import { updateLists } from '../_actions/lists'
import { createStream } from '../_actions/streaming'
export function instanceObservers (store) {
// 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
}
}
if (!currentInstance) {
return
}
updateVerifyCredentialsForInstance(currentInstance)
updateInstanceInfo(currentInstance)
updateLists()
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
}
})
}