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 PostStatusForm from '../post_status_form/post_status_form.vue'
import ChatTitle from '../chat_title/chat_title.vue' import ChatTitle from '../chat_title/chat_title.vue'
import chatService from '../../services/chat_service/chat_service.js' 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' import { getScrollPosition, getNewTopPosition, isBottomedOut, scrollableContainerHeight } from './chat_layout_utils.js'
const BOTTOMED_OUT_OFFSET = 10 const BOTTOMED_OUT_OFFSET = 10
@ -246,7 +247,7 @@ const Chat = {
const fetchOlderMessages = !!maxId const fetchOlderMessages = !!maxId
const sinceId = fetchLatest && chatMessageService.lastMessage && chatMessageService.lastMessage.id 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) => { .then((messages) => {
// Clear the current chat in case we're recovering from a ws connection loss. // Clear the current chat in case we're recovering from a ws connection loss.
if (isFirstFetch) { if (isFirstFetch) {
@ -287,7 +288,7 @@ const Chat = {
}, },
doStartFetching () { doStartFetching () {
this.$store.dispatch('startFetchingCurrentChat', { this.$store.dispatch('startFetchingCurrentChat', {
fetcher: () => setInterval(() => this.fetchChat({ fetchLatest: true }), 5000) fetcher: () => makeFetcher(() => this.fetchChat({ fetchLatest: true }), 5000)
}) })
this.fetchChat({ isFirstFetch: true }) 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 chatService from '../services/chat_service/chat_service.js'
import { parseChat, parseChatMessage } from '../services/entity_normalizer/entity_normalizer.service.js' import { parseChat, parseChatMessage } from '../services/entity_normalizer/entity_normalizer.service.js'
import { maybeShowChatNotification } from '../services/chat_utils/chat_utils.js' import { maybeShowChatNotification } from '../services/chat_utils/chat_utils.js'
import { makeFetcher } from '../services/fetcher/fetcher.js'
const emptyChatList = () => ({ const emptyChatList = () => ({
data: [], data: [],
@ -42,12 +43,10 @@ const chats = {
actions: { actions: {
// Chat list // Chat list
startFetchingChats ({ dispatch, commit }) { startFetchingChats ({ dispatch, commit }) {
const fetcher = () => { const fetcher = () => dispatch('fetchChats', { latest: true })
dispatch('fetchChats', { latest: true })
}
fetcher() fetcher()
commit('setChatListFetcher', { commit('setChatListFetcher', {
fetcher: () => setInterval(() => { fetcher() }, 5000) fetcher: () => makeFetcher(fetcher, 5000)
}) })
}, },
stopFetchingChats ({ commit }) { stopFetchingChats ({ commit }) {
@ -113,14 +112,14 @@ const chats = {
setChatListFetcher (state, { commit, fetcher }) { setChatListFetcher (state, { commit, fetcher }) {
const prevFetcher = state.chatListFetcher const prevFetcher = state.chatListFetcher
if (prevFetcher) { if (prevFetcher) {
clearInterval(prevFetcher) prevFetcher()
} }
state.chatListFetcher = fetcher && fetcher() state.chatListFetcher = fetcher && fetcher()
}, },
setCurrentChatFetcher (state, { fetcher }) { setCurrentChatFetcher (state, { fetcher }) {
const prevFetcher = state.fetcher const prevFetcher = state.fetcher
if (prevFetcher) { if (prevFetcher) {
clearInterval(prevFetcher) prevFetcher()
} }
state.fetcher = fetcher && fetcher() state.fetcher = fetcher && fetcher()
}, },

View file

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

View file

@ -1,5 +1,5 @@
import apiService from '../api/api.service.js' import apiService from '../api/api.service.js'
import makeFetcher from '../fetcher/fetcher.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 })