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
|
import { upperFirst, camelCase } from 'lodash';
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;
|