Browse Source

refactor: ignore socket event emit if undefined

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/1668/head
Pranav C 3 years ago
parent
commit
55a19bdcab
  1. 12
      packages/nc-gui/plugins/tele.js

12
packages/nc-gui/plugins/tele.js

@ -9,7 +9,7 @@ export default function({
}, inject) { }, inject) {
let socket let socket
const init = (token) => { const init = async(token) => {
if (socket) { if (socket) {
socket.disconnect() socket.disconnect()
} }
@ -30,7 +30,7 @@ export default function({
app.router.onReady(() => { app.router.onReady(() => {
app.router.afterEach(function(to, from) { app.router.afterEach(function(to, from) {
if (socket || (to.path === from.path && (to.query && to.query.type) === (from.query && from.query.type))) { if (!socket || (to.path === from.path && (to.query && to.query.type) === (from.query && from.query.type))) {
return return
} }
socket.emit('page', { socket.emit('page', {
@ -38,10 +38,12 @@ export default function({
path: to.matched[0].path + (to.query && to.query.type ? `?type=${to.query.type}` : '') path: to.matched[0].path + (to.query && to.query.type ? `?type=${to.query.type}` : '')
}) })
}) })
if (socket) {
socket.emit('page', { socket.emit('page', {
id: store.state.users.user && store.state.users.user.id, id: store.state.users.user && store.state.users.user.id,
path: route.matched[0].path + (route.query && route.query.type ? `?type=${route.query.type}` : '') path: route.matched[0].path + (route.query && route.query.type ? `?type=${route.query.type}` : '')
}) })
}
}) })
const tele = { const tele = {
@ -61,11 +63,11 @@ export default function({
function getListener(binding) { function getListener(binding) {
return function(e) { return function(e) {
if (!socket) { return }
const cat = window.location.hash.replace(/\d+\/(?=dashboard)/, '') const cat = window.location.hash.replace(/\d+\/(?=dashboard)/, '')
const event = binding.value && binding.value[0] const event = binding.value && binding.value[0]
const data = binding.value && binding.value[1] const data = binding.value && binding.value[1]
const extra = binding.value && binding.value.slice(2) const extra = binding.value && binding.value.slice(2)
tele.emit(event, tele.emit(event,
{ {
cat, cat,
@ -87,14 +89,14 @@ export default function({
store.watch(state => state.project.projectInfo && state.project.projectInfo.teleEnabled && state.users.token, (token) => { store.watch(state => state.project.projectInfo && state.project.projectInfo.teleEnabled && state.users.token, (token) => {
if (token) { if (token) {
init(token) init(token).then(() => {})
} else if (socket) { } else if (socket) {
socket.disconnect() socket.disconnect()
socket = null socket = null
} }
}) })
if (store.state.project.projectInfo && store.state.project.projectInfo.teleEnabled && store.state.users.token) { if (store.state.project.projectInfo && store.state.project.projectInfo.teleEnabled && store.state.users.token) {
init(store.state.users.token) init(store.state.users.token).then(() => {})
} }
} }

Loading…
Cancel
Save