fix: lazy-load @system.interconnect so simulator renders without the module
This commit is contained in:
+17
-3
@@ -1,8 +1,21 @@
|
||||
// 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.
|
||||
// The interconnect module is loaded lazily (guarded) so the app can still render
|
||||
// on simulators/environments where the system module is unavailable.
|
||||
|
||||
import interconnect from '@system.interconnect'
|
||||
let interconnectModule = null;
|
||||
let interconnectTried = false;
|
||||
|
||||
function loadInterconnect() {
|
||||
if (interconnectTried) return interconnectModule;
|
||||
interconnectTried = true;
|
||||
try {
|
||||
interconnectModule = require('@system.interconnect');
|
||||
} catch (e) {
|
||||
interconnectModule = null;
|
||||
}
|
||||
return interconnectModule;
|
||||
}
|
||||
|
||||
let conn = null
|
||||
let seq = 0
|
||||
@@ -10,6 +23,8 @@ const listeners = new Set()
|
||||
let currentStatus = '未连接'
|
||||
|
||||
function getConn() {
|
||||
const interconnect = loadInterconnect();
|
||||
if (!interconnect) return null;
|
||||
if (!conn) {
|
||||
try {
|
||||
conn = interconnect.instance()
|
||||
@@ -29,7 +44,6 @@ function getConn() {
|
||||
emit('status', currentStatus)
|
||||
}
|
||||
conn.onmessage = (data) => {
|
||||
// interconnect 回调: data.data 是手机端发来的内容
|
||||
let msg = data && data.data !== undefined ? data.data : data
|
||||
if (typeof msg === 'string') {
|
||||
try { msg = JSON.parse(msg) } catch (e) { return }
|
||||
|
||||
Reference in New Issue
Block a user