Fetch Public and TWKN timelines when viewed.

This commit is contained in:
Roger Braun 2017-02-16 11:17:47 +01:00
parent 14f07dde5d
commit 832bd3cdd2
6 changed files with 46 additions and 8 deletions

View file

@ -5,6 +5,12 @@ const PublicAndExternalTimeline = {
},
computed: {
timeline () { return this.$store.state.statuses.timelines.publicAndExternal }
},
created () {
this.$store.dispatch('startFetching', 'publicAndExternal')
},
destroyed () {
this.$store.dispatch('stopFetching', 'publicAndExternal')
}
}

View file

@ -5,7 +5,14 @@ const PublicTimeline = {
},
computed: {
timeline () { return this.$store.state.statuses.timelines.public }
},
created () {
this.$store.dispatch('startFetching', 'public')
},
destroyed () {
this.$store.dispatch('stopFetching', 'public')
}
}
export default PublicTimeline

View file

@ -2,11 +2,32 @@ import backendInteractorService from '../services/backend_interactor_service/bac
const api = {
state: {
backendInteractor: backendInteractorService()
backendInteractor: backendInteractorService(),
fetchers: {}
},
mutations: {
setBackendInteractor (state, backendInteractor) {
state.backendInteractor = backendInteractor
},
addFetcher (state, {timeline, fetcher}) {
state.fetchers[timeline] = fetcher
},
removeFetcher (state, {timeline}) {
delete state.fetchers[timeline]
}
},
actions: {
startFetching (store, timeline) {
// Don't start fetching if we already are.
if (!store.state.fetchers[timeline]) {
const fetcher = store.state.backendInteractor.startFetching({timeline, store})
store.commit('addFetcher', {timeline, fetcher})
}
},
stopFetching (store, timeline) {
const fetcher = store.state.fetchers[timeline]
window.clearInterval(fetcher)
store.commit('removeFetcher', {timeline})
}
}
}

View file

@ -1,4 +1,3 @@
import timelineFetcher from '../services/timeline_fetcher/timeline_fetcher.service.js'
import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js'
import { compact, map, each, find, merge } from 'lodash'
import { set } from 'vue'
@ -74,12 +73,12 @@ const users = {
commit('setCurrentUser', user)
commit('addNewUsers', [user])
// Start getting fresh tweets.
timelineFetcher.startFetching({store, credentials: userCredentials})
// Set our new backend interactor
commit('setBackendInteractor', backendInteractorService(userCredentials))
// Start getting fresh tweets.
store.dispatch('startFetching', 'friends')
// Fetch our friends
store.rootState.api.backendInteractor.fetchFriends()
.then((friends) => commit('addNewUsers', friends))

View file

@ -1,4 +1,5 @@
import apiService from '../api/api.service.js'
import timelineFetcherService from '../timeline_fetcher/timeline_fetcher.service.js'
const backendInteractorService = (credentials) => {
const fetchStatus = ({id}) => {
@ -29,6 +30,10 @@ const backendInteractorService = (credentials) => {
return apiService.unfollowUser({credentials, id})
}
const startFetching = ({timeline, store}) => {
return timelineFetcherService.startFetching({timeline, store, credentials})
}
const backendInteractorServiceInstance = {
fetchStatus,
fetchConversation,
@ -37,7 +42,8 @@ const backendInteractorService = (credentials) => {
followUser,
unfollowUser,
fetchAllFollowing,
verifyCredentials: apiService.verifyCredentials
verifyCredentials: apiService.verifyCredentials,
startFetching
}
return backendInteractorServiceInstance

View file

@ -30,8 +30,7 @@ const fetchAndUpdate = ({store, credentials, timeline = 'friends', older = false
const startFetching = ({ timeline = 'friends', credentials, store }) => {
fetchAndUpdate({timeline, credentials, store, showImmediately: true})
const boundFetchAndUpdate = () => fetchAndUpdate({ timeline, credentials, store })
setInterval(boundFetchAndUpdate, 10000)
return setInterval(boundFetchAndUpdate, 10000)
}
const timelineFetcher = {
fetchAndUpdate,