aboutsummaryrefslogtreecommitdiff
path: root/src/services/timeline_fetcher/timeline_fetcher.service.js
blob: 8a39eeb50a565f9066eec03d81ded1dce9274225 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { camelCase } from 'lodash'

import apiService from '../api/api.service.js'

const update = ({store, statuses, timeline, showImmediately}) => {
  const ccTimeline = camelCase(timeline)

  store.commit('addNewStatuses', {
    timeline: ccTimeline,
    statuses,
    showImmediately
  })

  store.commit('updateTimestamps')
}

const fetchAndUpdate = ({store, credentials, timeline = 'friends', older = false, showImmediately = false}) => {
  const args = { timeline, credentials }
  const rootState = store.rootState || store.state
  const timelineData = rootState.statuses.timelines[camelCase(timeline)]

  if (older) {
    args['until'] = timelineData.minVisibleId
  } else {
    args['since'] = timelineData.maxId
  }

  return apiService.fetchTimeline(args)
    .then((statuses) => update({store, statuses, timeline, showImmediately}))
}

const startFetching = ({ timeline = 'friends', credentials, store }) => {
  fetchAndUpdate({timeline, credentials, store, showImmediately: true})
  const boundFetchAndUpdate = () => fetchAndUpdate({ timeline, credentials, store })

  setInterval(boundFetchAndUpdate, 10000)
}
const timelineFetcher = {
  fetchAndUpdate,
  startFetching
}

export default timelineFetcher

// const timelineFetcherServiceFactory = ($ngRedux, apiService, $interval) => {
//   let fetcher;

//   const update = ({statuses, timeline, showImmediately}) => {
//     const ccTimeline = camelCase(timeline);

//     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;