change chats to use custom makeFetcher

This commit is contained in:
Shpuld Shpuldson 2020-09-02 21:01:31 +03:00
parent 2d914c331e
commit 1b6eee0497
4 changed files with 10 additions and 11 deletions

View File

@ -5,6 +5,7 @@ import ChatMessage from '../chat_message/chat_message.vue'
import PostStatusForm from '../post_status_form/post_status_form.vue'
import ChatTitle from '../chat_title/chat_title.vue'
import chatService from '../../services/chat_service/chat_service.js'
import { makeFetcher } from '../../services/fetcher/fetcher.js'
import { getScrollPosition, getNewTopPosition, isBottomedOut, scrollableContainerHeight } from './chat_layout_utils.js'
const BOTTOMED_OUT_OFFSET = 10
@ -246,7 +247,7 @@ const Chat = {
const fetchOlderMessages = !!maxId
const sinceId = fetchLatest && chatMessageService.lastMessage && chatMessageService.lastMessage.id
this.backendInteractor.chatMessages({ id: chatId, maxId, sinceId })
return this.backendInteractor.chatMessages({ id: chatId, maxId, sinceId })
.then((messages) => {
// Clear the current chat in case we're recovering from a ws connection loss.
if (isFirstFetch) {
@ -287,7 +288,7 @@ const Chat = {
},
doStartFetching () {
this.$store.dispatch('startFetchingCurrentChat', {
fetcher: () => setInterval(() => this.fetchChat({ fetchLatest: true }), 5000)
fetcher: () => makeFetcher(() => this.fetchChat({ fetchLatest: true }), 5000)
})
this.fetchChat({ isFirstFetch: true })
},

View File

@ -3,6 +3,7 @@ import { find, omitBy, orderBy, sumBy } from 'lodash'
import chatService from '../services/chat_service/chat_service.js'
import { parseChat, parseChatMessage } from '../services/entity_normalizer/entity_normalizer.service.js'
import { maybeShowChatNotification } from '../services/chat_utils/chat_utils.js'
import { makeFetcher } from '../services/fetcher/fetcher.js'
const emptyChatList = () => ({
data: [],
@ -42,12 +43,10 @@ const chats = {
actions: {
// Chat list
startFetchingChats ({ dispatch, commit }) {
const fetcher = () => {
dispatch('fetchChats', { latest: true })
}
const fetcher = () => dispatch('fetchChats', { latest: true })
fetcher()
commit('setChatListFetcher', {
fetcher: () => setInterval(() => { fetcher() }, 5000)
fetcher: () => makeFetcher(fetcher, 5000)
})
},
stopFetchingChats ({ commit }) {
@ -113,14 +112,14 @@ const chats = {
setChatListFetcher (state, { commit, fetcher }) {
const prevFetcher = state.chatListFetcher
if (prevFetcher) {
clearInterval(prevFetcher)
prevFetcher()
}
state.chatListFetcher = fetcher && fetcher()
},
setCurrentChatFetcher (state, { fetcher }) {
const prevFetcher = state.fetcher
if (prevFetcher) {
clearInterval(prevFetcher)
prevFetcher()
}
state.fetcher = fetcher && fetcher()
},

View File

@ -6,7 +6,6 @@ export const makeFetcher = (call, interval) => {
func = () => {
call().finally(() => {
console.log('callbacks')
if (stopped) return
timeout = window.setTimeout(func, interval)
})
@ -14,7 +13,7 @@ export const makeFetcher = (call, interval) => {
const stopFetcher = () => {
stopped = true
window.cancelTimeout(timeout)
window.clearTimeout(timeout)
}
func()

View File

@ -1,5 +1,5 @@
import apiService from '../api/api.service.js'
import makeFetcher from '../fetcher/fetcher.js'
import { makeFetcher } from '../fetcher/fetcher.js'
const update = ({ store, notifications, older }) => {
store.dispatch('setNotificationsError', { value: false })