mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.4 KiB
95 lines
2.4 KiB
3 years ago
|
import { defineNuxtPlugin } from 'nuxt/app'
|
||
3 years ago
|
import type { Socket } from 'socket.io-client'
|
||
3 years ago
|
import io from 'socket.io-client'
|
||
2 years ago
|
import type { UseGlobalReturn } from '~/composables/useGlobal/types'
|
||
3 years ago
|
|
||
|
// todo: ignore init if tele disabled
|
||
|
export default defineNuxtPlugin(async (nuxtApp) => {
|
||
|
const router = useRouter()
|
||
2 years ago
|
|
||
3 years ago
|
const route = useRoute()
|
||
|
|
||
2 years ago
|
const { appInfo } = $(useGlobal())
|
||
|
|
||
3 years ago
|
let socket: Socket
|
||
3 years ago
|
|
||
3 years ago
|
const init = async (token: string) => {
|
||
3 years ago
|
try {
|
||
3 years ago
|
if (socket) socket.disconnect()
|
||
3 years ago
|
|
||
2 years ago
|
const url = new URL(appInfo.ncSiteUrl, window.location.href.split(/[?#]/)[0]).href
|
||
|
|
||
3 years ago
|
socket = io(url, {
|
||
|
extraHeaders: { 'xc-auth': token },
|
||
|
})
|
||
|
|
||
|
socket.on('connect_error', () => {
|
||
|
socket.disconnect()
|
||
|
})
|
||
3 years ago
|
} catch {}
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
if (nuxtApp.$state.signedIn.value) {
|
||
|
await init(nuxtApp.$state.token.value)
|
||
|
}
|
||
|
|
||
3 years ago
|
router.afterEach((to, from) => {
|
||
3 years ago
|
if (!socket || (to.path === from.path && (to.query && to.query.type) === (from.query && from.query.type))) return
|
||
3 years ago
|
|
||
|
socket.emit('page', {
|
||
|
path: to.matched[0].path + (to.query && to.query.type ? `?type=${to.query.type}` : ''),
|
||
|
})
|
||
|
})
|
||
3 years ago
|
|
||
|
/**
|
||
|
* unreachable code?
|
||
|
if (socket) {
|
||
|
socket.emit('page', {
|
||
|
path: route.matched[0].path + (route.query && route.query.type ? `?type=${route.query.type}` : ''),
|
||
|
})
|
||
|
}
|
||
|
*/
|
||
3 years ago
|
|
||
|
const tele = {
|
||
3 years ago
|
emit(evt: string, data: Record<string, any>) {
|
||
3 years ago
|
// debugger
|
||
|
if (socket) {
|
||
|
socket.emit('event', {
|
||
|
event: evt,
|
||
|
...(data || {}),
|
||
|
path: route?.matched?.[0]?.path,
|
||
|
})
|
||
|
}
|
||
|
},
|
||
|
}
|
||
|
|
||
|
nuxtApp.vueApp.directive('t', {
|
||
|
created(el, binding, vnode) {
|
||
3 years ago
|
if (vnode.el) vnode.el.addEventListener('click', getListener(binding))
|
||
|
else el.addEventListener('click', getListener(binding))
|
||
3 years ago
|
},
|
||
|
})
|
||
3 years ago
|
|
||
|
function getListener(binding: any) {
|
||
3 years ago
|
return function () {
|
||
3 years ago
|
if (!socket) return
|
||
3 years ago
|
|
||
|
const event = binding.value && binding.value[0]
|
||
|
const data = binding.value && binding.value[1]
|
||
|
const extra = binding.value && binding.value.slice(2)
|
||
3 years ago
|
tele.emit(event, {
|
||
|
data,
|
||
|
extra,
|
||
|
})
|
||
3 years ago
|
}
|
||
|
}
|
||
3 years ago
|
|
||
2 years ago
|
watch((nuxtApp.$state as UseGlobalReturn).token, (newToken, oldToken) => {
|
||
3 years ago
|
if (newToken && newToken !== oldToken) init(newToken)
|
||
|
else if (!newToken) socket.disconnect()
|
||
|
})
|
||
3 years ago
|
|
||
|
nuxtApp.provide('tele', tele)
|
||
|
nuxtApp.provide('e', tele.emit)
|
||
3 years ago
|
})
|