1
1
/* eslint-disable no-new */
2
+ import type { QTResponseType } from '~/types/qt'
2
3
import QWebChannel , { isQtClient } from '~/utils/web-channel.js'
3
4
4
5
interface QtWebSocketOptions {
5
- onDataUpdated ?: ( data : any ) => void
6
+ onDataUpdated ?: ( data : QTResponseType ) => void
6
7
onConnected ?: ( ) => void
7
8
onDisconnected ?: ( ) => void
8
9
onError ?: ( error : any ) => void
9
10
}
10
11
11
- export function useWebChannel ( options : QtWebSocketOptions = { } ) {
12
- const { onDataUpdated } = options
12
+ type CallbackId = string
13
+ type Callback = ( data : QTResponseType ) => void
13
14
14
- const qtObject = ref < any > ( null )
15
- // const isConnected = ref(false )
15
+ let cacheQtObject : any = null
16
+ const globalCallbackMap = new Map < CallbackId , Callback > ( )
16
17
17
- // const { status: wsStatus, send: wsSend, open, close } = useWebSocket('ws://your-websocket-url', {
18
- // autoReconnect: true,
19
- // onConnected: () => {
20
- // isConnected.value = true
21
- // onConnected?.()
22
- // },
23
- // onDisconnected: () => {
24
- // isConnected.value = false
25
- // onDisconnected?.()
26
- // },
27
- // onError,
28
- // })
18
+ export function useWebChannel ( options : QtWebSocketOptions = { } ) {
19
+ const qtObject = ref < any > ( null )
29
20
30
21
const sendMessageToCpp = ( message : any ) => {
31
22
if ( qtObject . value && typeof qtObject . value . receiveMessage === 'function' ) {
@@ -36,20 +27,37 @@ export function useWebChannel(options: QtWebSocketOptions = {}) {
36
27
}
37
28
}
38
29
30
+ const connect = ( callback : Callback ) : CallbackId => {
31
+ const id = `${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . substring ( 2 ) } `
32
+ globalCallbackMap . set ( id , callback )
33
+ return id
34
+ }
35
+
36
+ const disconnect = ( id : CallbackId ) : boolean => {
37
+ return globalCallbackMap . delete ( id )
38
+ }
39
+
40
+ const handleDataUpdated = ( data : QTResponseType ) => {
41
+ window . console . log ( 'DEBUG:未处理的QT原始消息' , data )
42
+ if ( data ) {
43
+ globalCallbackMap . forEach ( ( cb ) => {
44
+ cb ( data )
45
+ } )
46
+ }
47
+ else {
48
+ console . error ( 'data is null' )
49
+ }
50
+ }
51
+
39
52
const initQtWebChannel = ( ) => {
40
53
if ( import . meta. env . DEV || ! isQtClient ) {
41
54
window . qt = {
42
55
webChannelTransport : {
43
56
send ( ) {
44
- window . console . log ( `
45
- QWebChannel simulator activated !
46
- ` )
57
+ window . console . log ( 'QWebChannel simulator activated !' )
47
58
} ,
48
-
49
59
onmessage : ( ) => {
50
- window . console . log ( `
51
- QWebChannel simulator activated !
52
- ` )
60
+ window . console . log ( 'QWebChannel simulator activated !' )
53
61
} ,
54
62
} ,
55
63
}
@@ -59,14 +67,14 @@ export function useWebChannel(options: QtWebSocketOptions = {}) {
59
67
if ( channel . objects . myObject ) {
60
68
window . console . log ( 'myObject is available' )
61
69
qtObject . value = channel . objects . myObject
62
- qtObject . value . dataUpdated . connect ( ( data : any ) => {
63
- if ( data ) {
64
- onDataUpdated ?. ( data )
65
- }
66
- else {
67
- console . error ( 'data is null' )
68
- }
69
- } )
70
+ cacheQtObject = channel . objects . myObject
71
+
72
+ if ( typeof qtObject . value . dataUpdated ?. connect === 'function' ) {
73
+ qtObject . value . dataUpdated . connect ( handleDataUpdated )
74
+ }
75
+ else {
76
+ console . error ( 'myObject dataUpdated connect function not available' )
77
+ }
70
78
}
71
79
else {
72
80
console . error ( 'myObject is not available' )
@@ -75,18 +83,28 @@ export function useWebChannel(options: QtWebSocketOptions = {}) {
75
83
}
76
84
77
85
onMounted ( ( ) => {
78
- initQtWebChannel ( )
86
+ if ( ! cacheQtObject ) {
87
+ initQtWebChannel ( )
88
+ }
89
+ else {
90
+ qtObject . value = cacheQtObject
91
+ // if (typeof qtObject.value.dataUpdated?.connect === 'function') {
92
+ // qtObject.value.dataUpdated.connect(handleDataUpdated)
93
+ // }
94
+ }
79
95
} )
80
96
81
- // onUnmounted(() => {
82
- // close()
83
- // })
97
+ // Watch for changes in the options.onDataUpdated callback
98
+ watch ( ( ) => options . onDataUpdated , ( newCallback ) => {
99
+ if ( newCallback ) {
100
+ connect ( newCallback )
101
+ }
102
+ } , { immediate : true } )
84
103
85
104
return {
86
- // isConnected,
87
- // wsStatus,
88
- // sendWebSocketMessage: wsSend,
89
105
sendMessageToCpp,
106
+ connect,
107
+ disconnect,
90
108
qtObject,
91
109
}
92
110
}
0 commit comments