feat: multi-listener bridge + subscribe/unsubscribe in index page
This commit is contained in:
+10
-5
@@ -1,12 +1,12 @@
|
|||||||
// DSHWatch bridge: @system.interconnect wrapper (real shell glue).
|
// DSHWatch bridge: @system.interconnect wrapper.
|
||||||
// Sends/receives JSON envelopes to the DSHWatch phone APP over the Xiaomi
|
// Sends/receives JSON envelopes to the DSHWatch phone APP over the Vela BLE channel.
|
||||||
// Vela interconnect BLE channel. Protocol per DSHWatch_Server/docs/PROTOCOL.md.
|
// Protocol per DSHWatch_Server/docs/PROTOCOL.md.
|
||||||
|
|
||||||
import interconnect from '@system.interconnect'
|
import interconnect from '@system.interconnect'
|
||||||
|
|
||||||
let conn = null
|
let conn = null
|
||||||
let seq = 0
|
let seq = 0
|
||||||
let listeners = []
|
const listeners = new Set()
|
||||||
let currentStatus = '未连接'
|
let currentStatus = '未连接'
|
||||||
|
|
||||||
function getConn() {
|
function getConn() {
|
||||||
@@ -48,8 +48,13 @@ function emit(type, payload) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function subscribe(callbacks) {
|
||||||
|
listeners.add(callbacks)
|
||||||
|
return () => listeners.delete(callbacks)
|
||||||
|
}
|
||||||
|
|
||||||
export function init(callbacks) {
|
export function init(callbacks) {
|
||||||
listeners = [callbacks]
|
subscribe(callbacks)
|
||||||
refreshConn()
|
refreshConn()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
onReady() {
|
onReady() {
|
||||||
bridge.init({
|
this._unsub = bridge.subscribe({
|
||||||
status: (s) => {
|
status: (s) => {
|
||||||
this.statusText = s
|
this.statusText = s
|
||||||
this.connText = s
|
this.connText = s
|
||||||
@@ -80,6 +80,11 @@ export default {
|
|||||||
this.handleBridgeMessage(msg)
|
this.handleBridgeMessage(msg)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
bridge.refreshConn()
|
||||||
|
},
|
||||||
|
|
||||||
|
onDestroy() {
|
||||||
|
if (this._unsub) this._unsub()
|
||||||
},
|
},
|
||||||
|
|
||||||
onSwipeChange(e) {
|
onSwipeChange(e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user