2016-10-28 05:26:51 -07:00
|
|
|
import { camelCase } from 'lodash'
|
2016-10-26 10:03:55 -07:00
|
|
|
|
2016-10-28 05:26:51 -07:00
|
|
|
import apiService from '../api/api.service.js'
|
2016-10-26 10:03:55 -07:00
|
|
|
|
2016-10-28 05:26:51 -07:00
|
|
|
const update = ({store, statuses, timeline, showImmediately}) => {
|
|
|
|
const ccTimeline = camelCase(timeline)
|
2016-10-26 10:03:55 -07:00
|
|
|
|
2016-10-28 05:26:51 -07:00
|
|
|
store.commit('addNewStatuses', {
|
|
|
|
timeline: ccTimeline,
|
|
|
|
statuses,
|
|
|
|
showImmediately
|
|
|
|
})
|
2016-10-26 10:03:55 -07:00
|
|
|
|
2016-10-28 06:59:49 -07:00
|
|
|
store.commit('updateTimestamps')
|
2016-10-28 05:26:51 -07:00
|
|
|
}
|
2016-10-26 10:03:55 -07:00
|
|
|
|
2016-10-28 05:26:51 -07:00
|
|
|
const fetchAndUpdate = ({store, credentials, timeline = 'friends', older = false, showImmediately = false}) => {
|
|
|
|
const args = { timeline, credentials }
|
|
|
|
const timelineData = store.rootState.statuses.timelines[camelCase(timeline)]
|
2016-10-26 10:03:55 -07:00
|
|
|
|
2016-10-28 05:26:51 -07:00
|
|
|
if (older) {
|
|
|
|
args['until'] = timelineData.minVisibleId
|
|
|
|
} else {
|
|
|
|
args['since'] = timelineData.maxId
|
|
|
|
}
|
2016-10-26 10:03:55 -07:00
|
|
|
|
2016-10-28 05:26:51 -07:00
|
|
|
apiService.fetchTimeline(args)
|
|
|
|
.then((statuses) => update({store, statuses, timeline, showImmediately}))
|
|
|
|
}
|
2016-10-26 10:03:55 -07:00
|
|
|
|
2016-10-28 05:26:51 -07:00
|
|
|
const startFetching = ({ timeline = 'friends', credentials, store }) => {
|
|
|
|
fetchAndUpdate({timeline, credentials, store, showImmediately: true})
|
|
|
|
const boundFetchAndUpdate = () => fetchAndUpdate({ timeline, credentials, store })
|
2016-10-26 10:03:55 -07:00
|
|
|
|
2016-10-28 05:26:51 -07:00
|
|
|
setInterval(boundFetchAndUpdate, 10000)
|
|
|
|
}
|
|
|
|
const timelineFetcher = {
|
|
|
|
startFetching
|
|
|
|
}
|
2016-10-26 10:03:55 -07:00
|
|
|
|
2016-10-28 05:26:51 -07:00
|
|
|
export default timelineFetcher
|
2016-10-26 10:03:55 -07:00
|
|
|
|
2016-10-28 05:26:51 -07:00
|
|
|
// const timelineFetcherServiceFactory = ($ngRedux, apiService, $interval) => {
|
|
|
|
// let fetcher;
|
2016-10-26 10:03:55 -07:00
|
|
|
|
2016-10-28 05:26:51 -07:00
|
|
|
// const update = ({statuses, timeline, showImmediately}) => {
|
|
|
|
// const ccTimeline = camelCase(timeline);
|
2016-10-26 10:03:55 -07:00
|
|
|
|
2016-10-28 05:26:51 -07:00
|
|
|
// const action = {
|
|
|
|
// type: 'ADD_NEW_STATUSES',
|
|
|
|
// data: {
|
|
|
|
// statuses,
|
|
|
|
// timeline: ccTimeline,
|
|
|
|
// showImmediately
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
|
|
|
|
// $ngRedux.dispatch(action);
|
|
|
|
// $ngRedux.dispatch({type: 'UPDATE_TIMESTAMPS'});
|
|
|
|
// };
|
|
|
|
|
|
|
|
// const fetchAndUpdate = ({timeline = 'friends', older = false, showImmediately = false}) => {
|
|
|
|
// const args = { timeline };
|
|
|
|
// const timelineData = $ngRedux.getState().statuses.timelines[camelCase(timeline)];
|
|
|
|
|
|
|
|
// if(older) {
|
|
|
|
// args['until'] = timelineData.minVisibleId;
|
|
|
|
// } else {
|
|
|
|
// args['since'] = timelineData.maxId;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// apiService.fetchTimeline(args).
|
|
|
|
// then((statuses) => update({statuses, timeline, showImmediately}));
|
|
|
|
// };
|
|
|
|
|
|
|
|
// const startFetching = ({timeline = 'friends'}) => {
|
|
|
|
// fetchAndUpdate({timeline, showImmediately: true});
|
|
|
|
|
|
|
|
// const boundFetchAndUpdate = () => fetchAndUpdate({timeline});
|
|
|
|
// fetcher = $interval(boundFetchAndUpdate, 10000);
|
|
|
|
// };
|
|
|
|
|
|
|
|
// const timelineFetcherService = {
|
|
|
|
// startFetching,
|
|
|
|
// fetchAndUpdate
|
|
|
|
// };
|
|
|
|
|
|
|
|
// return timelineFetcherService;
|
|
|
|
// };
|
|
|
|
|
|
|
|
// timelineFetcherServiceFactory.$inject = ['$ngRedux', 'apiService', '$interval'];
|
|
|
|
|
|
|
|
// export default timelineFetcherServiceFactory;
|