forked from AkkomaGang/akkoma-fe
replace setInterval for timelne, notifications and follow requests
This commit is contained in:
parent
acc45b49a5
commit
2d914c331e
5 changed files with 34 additions and 9 deletions
|
@ -20,7 +20,7 @@ const api = {
|
||||||
state.fetchers[fetcherName] = fetcher
|
state.fetchers[fetcherName] = fetcher
|
||||||
},
|
},
|
||||||
removeFetcher (state, { fetcherName, fetcher }) {
|
removeFetcher (state, { fetcherName, fetcher }) {
|
||||||
window.clearInterval(fetcher)
|
state.fetchers[fetcherName]()
|
||||||
delete state.fetchers[fetcherName]
|
delete state.fetchers[fetcherName]
|
||||||
},
|
},
|
||||||
setWsToken (state, token) {
|
setWsToken (state, token) {
|
||||||
|
|
23
src/services/fetcher/fetcher.js
Normal file
23
src/services/fetcher/fetcher.js
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
|
||||||
|
export const makeFetcher = (call, interval) => {
|
||||||
|
let stopped = false
|
||||||
|
let timeout = null
|
||||||
|
let func = () => {}
|
||||||
|
|
||||||
|
func = () => {
|
||||||
|
call().finally(() => {
|
||||||
|
console.log('callbacks')
|
||||||
|
if (stopped) return
|
||||||
|
timeout = window.setTimeout(func, interval)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const stopFetcher = () => {
|
||||||
|
stopped = true
|
||||||
|
window.cancelTimeout(timeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
func()
|
||||||
|
|
||||||
|
return stopFetcher
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
import apiService from '../api/api.service.js'
|
import apiService from '../api/api.service.js'
|
||||||
|
import { makeFetcher } from '../fetcher/fetcher.js'
|
||||||
|
|
||||||
const fetchAndUpdate = ({ store, credentials }) => {
|
const fetchAndUpdate = ({ store, credentials }) => {
|
||||||
return apiService.fetchFollowRequests({ credentials })
|
return apiService.fetchFollowRequests({ credentials })
|
||||||
|
@ -10,9 +11,8 @@ const fetchAndUpdate = ({ store, credentials }) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const startFetching = ({ credentials, store }) => {
|
const startFetching = ({ credentials, store }) => {
|
||||||
fetchAndUpdate({ credentials, store })
|
|
||||||
const boundFetchAndUpdate = () => fetchAndUpdate({ credentials, store })
|
const boundFetchAndUpdate = () => fetchAndUpdate({ credentials, store })
|
||||||
return setInterval(boundFetchAndUpdate, 10000)
|
return makeFetcher(boundFetchAndUpdate, 10000)
|
||||||
}
|
}
|
||||||
|
|
||||||
const followRequestFetcher = {
|
const followRequestFetcher = {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import apiService from '../api/api.service.js'
|
import apiService from '../api/api.service.js'
|
||||||
|
import makeFetcher from '../fetcher/fetcher.js'
|
||||||
|
|
||||||
const update = ({ store, notifications, older }) => {
|
const update = ({ store, notifications, older }) => {
|
||||||
store.dispatch('setNotificationsError', { value: false })
|
store.dispatch('setNotificationsError', { value: false })
|
||||||
|
@ -39,6 +40,7 @@ const fetchAndUpdate = ({ store, credentials, older = false }) => {
|
||||||
args['since'] = Math.max(...readNotifsIds)
|
args['since'] = Math.max(...readNotifsIds)
|
||||||
fetchNotifications({ store, args, older })
|
fetchNotifications({ store, args, older })
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,13 +55,12 @@ const fetchNotifications = ({ store, args, older }) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const startFetching = ({ credentials, store }) => {
|
const startFetching = ({ credentials, store }) => {
|
||||||
fetchAndUpdate({ credentials, store })
|
|
||||||
const boundFetchAndUpdate = () => fetchAndUpdate({ credentials, store })
|
|
||||||
// Initially there's set flag to silence all desktop notifications so
|
// Initially there's set flag to silence all desktop notifications so
|
||||||
// that there won't spam of them when user just opened up the FE we
|
// that there won't spam of them when user just opened up the FE we
|
||||||
// reset that flag after a while to show new notifications once again.
|
// reset that flag after a while to show new notifications once again.
|
||||||
setTimeout(() => store.dispatch('setNotificationsSilence', false), 10000)
|
setTimeout(() => store.dispatch('setNotificationsSilence', false), 10000)
|
||||||
return setInterval(boundFetchAndUpdate, 10000)
|
const boundFetchAndUpdate = () => fetchAndUpdate({ credentials, store, refetch: true })
|
||||||
|
return makeFetcher(boundFetchAndUpdate, 10000)
|
||||||
}
|
}
|
||||||
|
|
||||||
const notificationsFetcher = {
|
const notificationsFetcher = {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { camelCase } from 'lodash'
|
import { camelCase } from 'lodash'
|
||||||
|
|
||||||
import apiService from '../api/api.service.js'
|
import apiService from '../api/api.service.js'
|
||||||
|
import { makeFetcher } from '../fetcher/fetcher.js'
|
||||||
|
|
||||||
const update = ({ store, statuses, timeline, showImmediately, userId, pagination }) => {
|
const update = ({ store, statuses, timeline, showImmediately, userId, pagination }) => {
|
||||||
const ccTimeline = camelCase(timeline)
|
const ccTimeline = camelCase(timeline)
|
||||||
|
@ -70,9 +71,9 @@ const startFetching = ({ timeline = 'friends', credentials, store, userId = fals
|
||||||
const timelineData = rootState.statuses.timelines[camelCase(timeline)]
|
const timelineData = rootState.statuses.timelines[camelCase(timeline)]
|
||||||
const showImmediately = timelineData.visibleStatuses.length === 0
|
const showImmediately = timelineData.visibleStatuses.length === 0
|
||||||
timelineData.userId = userId
|
timelineData.userId = userId
|
||||||
|
const boundFetchAndUpdate = () =>
|
||||||
fetchAndUpdate({ timeline, credentials, store, showImmediately, userId, tag })
|
fetchAndUpdate({ timeline, credentials, store, showImmediately, userId, tag })
|
||||||
const boundFetchAndUpdate = () => fetchAndUpdate({ timeline, credentials, store, userId, tag })
|
return makeFetcher(boundFetchAndUpdate, 10000)
|
||||||
return setInterval(boundFetchAndUpdate, 10000)
|
|
||||||
}
|
}
|
||||||
const timelineFetcher = {
|
const timelineFetcher = {
|
||||||
fetchAndUpdate,
|
fetchAndUpdate,
|
||||||
|
|
Loading…
Reference in a new issue