From e577181300328624f04371ec919a36858c6c9e20 Mon Sep 17 00:00:00 2001 From: jiangchen <1703935164@qq.com> Date: Tue, 1 Sep 2026 19:01:20 +0800 Subject: [PATCH] feat: multi-listener bridge + subscribe/unsubscribe in index page --- src/common/bridge.js | 15 ++++++++++----- src/pages/index/index.ux | 7 ++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/common/bridge.js b/src/common/bridge.js index 2d821a6..261ad4b 100644 --- a/src/common/bridge.js +++ b/src/common/bridge.js @@ -1,12 +1,12 @@ -// DSHWatch bridge: @system.interconnect wrapper (real shell glue). -// Sends/receives JSON envelopes to the DSHWatch phone APP over the Xiaomi -// Vela interconnect BLE channel. Protocol per DSHWatch_Server/docs/PROTOCOL.md. +// DSHWatch bridge: @system.interconnect wrapper. +// Sends/receives JSON envelopes to the DSHWatch phone APP over the Vela BLE channel. +// Protocol per DSHWatch_Server/docs/PROTOCOL.md. import interconnect from '@system.interconnect' let conn = null let seq = 0 -let listeners = [] +const listeners = new Set() let currentStatus = '未连接' 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) { - listeners = [callbacks] + subscribe(callbacks) refreshConn() } diff --git a/src/pages/index/index.ux b/src/pages/index/index.ux index 2586367..9626547 100644 --- a/src/pages/index/index.ux +++ b/src/pages/index/index.ux @@ -71,7 +71,7 @@ export default { }, onReady() { - bridge.init({ + this._unsub = bridge.subscribe({ status: (s) => { this.statusText = s this.connText = s @@ -80,6 +80,11 @@ export default { this.handleBridgeMessage(msg) } }) + bridge.refreshConn() + }, + + onDestroy() { + if (this._unsub) this._unsub() }, onSwipeChange(e) {