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

26 lines
610 B
TypeScript
Raw Normal View History

2024-08-20 12:11:31 +00:00
import { computed, onUnmounted, unref } from 'vue'
import actions from '@/shared/action'
export default function(onmessage: {(data: any):void}, store: any) {
const webSocket = computed(() => store.state.app.$webSocket);
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
})
}
})
}