cmc-web/packages/common/hooks/useGlobalWebsocket.ts

26 lines
613 B
TypeScript
Raw Permalink Normal View History

2024-08-20 12:11:31 +00:00
import { computed, onUnmounted, unref } from 'vue'
import actions from '@/shared/action'
2024-08-21 01:17:14 +00:00
export default function (onmessage: { (data: any): void }, store: any) {
const webSocket = computed(() => store.state.app.$webSocket)
2024-08-20 12:11:31 +00:00
console.log(webSocket)
if (unref(webSocket)) {
webSocket.value.onmessage = onmessage
} else {
actions.setGlobalState({
name: 'cop-web',
onmessage
})
}
onUnmounted(() => {
if (unref(webSocket)) {
webSocket.value.onmessage = null
} else {
actions.setGlobalState({
name: 'cop-web',
onmessage: null
})
}
})
}