Introduce debounce to reduce duplicated backend API calls
This commit is contained in:
parent
13f6bc73ab
commit
336c31a345
@ -1,5 +1,6 @@
|
|||||||
/* eslint-disable no-prototype-builtins */
|
/* eslint-disable no-prototype-builtins */
|
||||||
import { i18n } from '@/lang'
|
import { i18n } from '@/lang'
|
||||||
|
import { debounce } from 'lodash';
|
||||||
import { WsConnectionFactory } from '@/utils/backend/websocket'
|
import { WsConnectionFactory } from '@/utils/backend/websocket'
|
||||||
import { MessageHubApi } from '@/utils/backend/messageHub'
|
import { MessageHubApi } from '@/utils/backend/messageHub'
|
||||||
|
|
||||||
@ -22,7 +23,7 @@ const checkUnreadRulesBy = (message, rules = []) => {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateConversations = (state, { token, cb }, data) => {
|
const updateConversations = debounce((state, { token, cb }, data) => {
|
||||||
MessageHubApi.fetchConversations(GWT, token)
|
MessageHubApi.fetchConversations(GWT, token)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
const conversations = response.data.conversations || []
|
const conversations = response.data.conversations || []
|
||||||
@ -60,8 +61,8 @@ const updateConversations = (state, { token, cb }, data) => {
|
|||||||
if (cb && cb instanceof Function) {
|
if (cb && cb instanceof Function) {
|
||||||
cb(err)
|
cb(err)
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}, 300);
|
||||||
|
|
||||||
const basicStore = {
|
const basicStore = {
|
||||||
namespaced: true,
|
namespaced: true,
|
||||||
@ -126,6 +127,7 @@ const basicStore = {
|
|||||||
console.log('downstream_web_socket error')
|
console.log('downstream_web_socket error')
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
|
console.log('downstream_web_socket closed')
|
||||||
if (timer) {
|
if (timer) {
|
||||||
clearInterval(timer)
|
clearInterval(timer)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,12 +3,12 @@
|
|||||||
class WsConnectionFactory {
|
class WsConnectionFactory {
|
||||||
static CreateWebSocket(jwt, onOpen, onMessage, onError, onClose) {
|
static CreateWebSocket(jwt, onOpen, onMessage, onError, onClose) {
|
||||||
// let jwt = userUtils.getJwtToken()
|
// let jwt = userUtils.getJwtToken()
|
||||||
this.socket = new WebSocket(`/ws/downstream/online_platform_notification?token=${jwt}`)
|
const socket = new WebSocket(`/ws/downstream/online_platform_notification?token=${jwt}`)
|
||||||
this.socket.onopen = onOpen
|
socket.onopen = onOpen
|
||||||
this.socket.onmessage = onMessage
|
socket.onmessage = onMessage
|
||||||
this.socket.onerror = onError
|
socket.onerror = onError
|
||||||
this.socket.onclose = onClose
|
socket.onclose = onClose
|
||||||
return this.socket
|
return socket
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user