2016-10-28 23:26:51 +11:00
|
|
|
import { camelCase } from 'lodash'
|
2016-10-27 04:03:55 +11:00
|
|
|
|
2016-10-28 23:26:51 +11:00
|
|
|
import apiService from '../api/api.service.js'
|
2016-10-27 04:03:55 +11:00
|
|
|
|
2016-10-28 23:26:51 +11:00
|
|
|
const update = ({store, statuses, timeline, showImmediately}) => {
|
|
|
|
const ccTimeline = camelCase(timeline)
|
2016-10-27 04:03:55 +11:00
|
|
|
|
2017-03-08 07:38:55 +11:00
|
|
|
setError({store, timeline, value: false})
|
|
|
|
|
2016-11-19 08:56:20 +11:00
|
|
|
store.dispatch('addNewStatuses', {
|
2016-10-28 23:26:51 +11:00
|
|
|
timeline: ccTimeline,
|
|
|
|
statuses,
|
|
|
|
showImmediately
|
|
|
|
})
|
|
|
|
}
|
2016-10-27 04:03:55 +11:00
|
|
|
|
2017-03-08 03:27:12 +11:00
|
|
|
const setError = ({store, timeline, value}) => {
|
|
|
|
const ccTimeline = camelCase(timeline)
|
|
|
|
|
|
|
|
store.dispatch('setError', {
|
|
|
|
timeline: ccTimeline,
|
|
|
|
value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-28 23:26:51 +11:00
|
|
|
const fetchAndUpdate = ({store, credentials, timeline = 'friends', older = false, showImmediately = false}) => {
|
|
|
|
const args = { timeline, credentials }
|
2016-11-07 03:44:05 +11:00
|
|
|
const rootState = store.rootState || store.state
|
|
|
|
const timelineData = rootState.statuses.timelines[camelCase(timeline)]
|
2016-10-27 04:03:55 +11:00
|
|
|
|
2016-10-28 23:26:51 +11:00
|
|
|
if (older) {
|
|
|
|
args['until'] = timelineData.minVisibleId
|
|
|
|
} else {
|
|
|
|
args['since'] = timelineData.maxId
|
|
|
|
}
|
2016-10-27 04:03:55 +11:00
|
|
|
|
2016-11-07 03:44:05 +11:00
|
|
|
return apiService.fetchTimeline(args)
|
2017-03-08 07:38:55 +11:00
|
|
|
.then((statuses) => update({store, statuses, timeline, showImmediately}),
|
|
|
|
() => setError({store, timeline, value: true}))
|
2016-10-28 23:26:51 +11:00
|
|
|
}
|
2016-10-27 04:03:55 +11:00
|
|
|
|
2016-10-28 23:26:51 +11:00
|
|
|
const startFetching = ({ timeline = 'friends', credentials, store }) => {
|
|
|
|
fetchAndUpdate({timeline, credentials, store, showImmediately: true})
|
|
|
|
const boundFetchAndUpdate = () => fetchAndUpdate({ timeline, credentials, store })
|
2017-02-16 21:17:47 +11:00
|
|
|
return setInterval(boundFetchAndUpdate, 10000)
|
2016-10-28 23:26:51 +11:00
|
|
|
}
|
|
|
|
const timelineFetcher = {
|
2016-11-07 03:44:05 +11:00
|
|
|
fetchAndUpdate,
|
2016-10-28 23:26:51 +11:00
|
|
|
startFetching
|
|
|
|
}
|
2016-10-27 04:03:55 +11:00
|
|
|
|
2016-10-28 23:26:51 +11:00
|
|
|
export default timelineFetcher
|