2018-03-03 22:15:50 +00:00
|
|
|
|
import { updateInstanceInfo, updateVerifyCredentialsForInstance } from '../../_actions/instances'
|
2018-12-05 08:21:54 +00:00
|
|
|
|
import { updateListsForInstance } from '../../_actions/lists'
|
2018-03-03 22:15:50 +00:00
|
|
|
|
import { createStream } from '../../_actions/streaming'
|
2018-10-06 20:06:10 +00:00
|
|
|
|
import { updatePushSubscriptionForInstance } from '../../_actions/pushSubscription'
|
2018-03-25 19:24:38 +00:00
|
|
|
|
import { updateCustomEmojiForInstance } from '../../_actions/emoji'
|
2018-04-03 04:14:12 +00:00
|
|
|
|
import { addStatusesOrNotifications } from '../../_actions/addStatusOrNotification'
|
|
|
|
|
import { getTimeline } from '../../_api/timelines'
|
2018-08-30 05:49:14 +00:00
|
|
|
|
import { TIMELINE_BATCH_SIZE } from '../../_static/timelines'
|
2018-12-05 08:21:54 +00:00
|
|
|
|
import { scheduleIdleTask } from '../../_utils/scheduleIdleTask'
|
|
|
|
|
import { mark, stop } from '../../_utils/marks'
|
2018-02-11 21:46:57 +00:00
|
|
|
|
|
2018-12-05 08:21:54 +00:00
|
|
|
|
// stream to watch for home timeline updates and notifications
|
|
|
|
|
let currentInstanceStream
|
|
|
|
|
|
|
|
|
|
async function refreshInstanceDataAndStream (store, instanceName) {
|
|
|
|
|
mark(`refreshInstanceDataAndStream-${instanceName}`)
|
|
|
|
|
await doRefreshInstanceDataAndStream(store, instanceName)
|
|
|
|
|
stop(`refreshInstanceDataAndStream-${instanceName}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function currentInstanceChanged (store, instanceName) {
|
|
|
|
|
return store.get().currentInstance !== instanceName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function doRefreshInstanceDataAndStream (store, instanceName) {
|
|
|
|
|
if (currentInstanceChanged(store, instanceName)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await refreshInstanceData(instanceName)
|
|
|
|
|
|
|
|
|
|
if (currentInstanceChanged(store, instanceName)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let { currentInstanceInfo } = store.get()
|
|
|
|
|
if (!currentInstanceInfo) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream(store, instanceName, currentInstanceInfo)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function refreshInstanceData (instanceName) {
|
|
|
|
|
// these are all low-priority
|
|
|
|
|
scheduleIdleTask(() => updateVerifyCredentialsForInstance(instanceName))
|
|
|
|
|
scheduleIdleTask(() => updateCustomEmojiForInstance(instanceName))
|
|
|
|
|
scheduleIdleTask(() => updateListsForInstance(instanceName))
|
|
|
|
|
scheduleIdleTask(() => updatePushSubscriptionForInstance(instanceName))
|
|
|
|
|
|
|
|
|
|
// this is the only critical one
|
|
|
|
|
await updateInstanceInfo(instanceName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stream (store, instanceName, currentInstanceInfo) {
|
|
|
|
|
let homeTimelineItemIds = store.getForTimeline(instanceName,
|
|
|
|
|
'home', 'timelineItemIds')
|
|
|
|
|
let firstHomeTimelineItemId = homeTimelineItemIds && homeTimelineItemIds[0]
|
|
|
|
|
let notificationItemIds = store.getForTimeline(instanceName,
|
|
|
|
|
'notifications', 'timelineItemIds')
|
|
|
|
|
let firstNotificationTimelineItemId = notificationItemIds && notificationItemIds[0]
|
|
|
|
|
|
|
|
|
|
let { accessToken } = store.get()
|
|
|
|
|
let streamingApi = currentInstanceInfo.urls.streaming_api
|
|
|
|
|
|
|
|
|
|
function onOpenStream () {
|
|
|
|
|
if (currentInstanceChanged(store, instanceName)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* no await */ fillGap(instanceName, accessToken, 'home', firstHomeTimelineItemId)
|
|
|
|
|
/* no await */ fillGap(instanceName, accessToken, 'notifications', firstNotificationTimelineItemId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentInstanceStream = createStream(streamingApi, instanceName, accessToken, 'home', onOpenStream)
|
|
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
window.currentInstanceStream = currentInstanceStream
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fill in the "streaming gap" – i.e. fetch the most recent 20 items so that there isn't
|
|
|
|
|
// a big gap in the timeline if you haven't looked at it in awhile
|
|
|
|
|
async function fillGap (instanceName, accessToken, timelineName, firstTimelineItemId) {
|
|
|
|
|
if (!firstTimelineItemId) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let newTimelineItems = await getTimeline(instanceName, accessToken,
|
|
|
|
|
timelineName, null, firstTimelineItemId, TIMELINE_BATCH_SIZE)
|
|
|
|
|
if (newTimelineItems.length) {
|
|
|
|
|
addStatusesOrNotifications(instanceName, timelineName, newTimelineItems)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-15 17:02:46 +00:00
|
|
|
|
|
2018-12-05 08:21:54 +00:00
|
|
|
|
export function instanceObservers (store) {
|
2018-02-15 17:02:46 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
2018-02-15 17:02:46 +00:00
|
|
|
|
|
2018-12-05 08:21:54 +00:00
|
|
|
|
scheduleIdleTask(() => refreshInstanceDataAndStream(store, currentInstance))
|
2018-02-11 21:46:57 +00:00
|
|
|
|
})
|
2018-02-11 22:11:03 +00:00
|
|
|
|
}
|